H2230823078


私信TA

用户名:dotcpp0618148

访问量:965

签 名:

https://devcpp.gitee.io/

等  级
排  名 1046
经  验 3164
参赛次数 0
文章发表 10
年  龄 0
在职情况 学生
学  校 贺州学院
专  业

  自我简介:

https://royqh1979.gitee.io/redpandacpp/

TA的其他文章

记忆化搜索
浏览:15

解题思路:

注意事项:

参考代码:

#include<bits/stdc++.h>
using namespace std;
const int N=55;
int mp[N][N];
int n,s;
int dist[N];
bool st[N];
typedef pair<int,int> PII;

void dijkstra(){
    memset(dist,0x3f,sizeof dist);
    dist[s]=0;
    priority_queue<PII,vector<PII>,greater<PII> >heap;
    heap.push({0,s});
    while(heap.size()){
        auto t=heap.top();
        heap.pop();
        int ver=t.second,dis=t.first;
        if(st[ver])    continue;
        st[ver]=1;
        for(int i=1;i<=n;i++){
            if(mp[ver][i]){
                if(dist[i]>dis+mp[ver][i]){
                    dist[i]=dis+mp[ver][i];
                    heap.push({dist[i],i});
                }
            }
        }
    }
    
}
int main(){
    
    cin>>n>>s;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            
            scanf("%d ",&mp[i][j]);
            //cin>>mp[i][j];
            //if(mp[i][j]=='-')   mp[i][j]=0;
        }
    }
    
    dijkstra();
    for(int i=1;i<=n;i++){
        if(i==s)    continue;
        
        printf("(%d -> %d) = %d\n",s,i,dist[i]);
    }
    
}


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区