1 条题解

  • 0
    @ 2024-4-30 21:00:57
    #include <bits/stdc++.h>
    using namespace std;
    const int INF=0x3f3f3f3f;
    const int N=60;
    int n,s;
    int a[N][N];
    int dis[N];
    bool vis[N];
    int main(){
    	cin>>n>>s;
    	for (int i=1;i<=n;i++){
    		for (int j=1;j<=n;j++)cin>>a[i][j];
    	}
    	memset(dis,0x3f,sizeof(dis));
    	dis[s]=0;
    	for (int i=1;i<=n;i++){
    		int mi=-1;
    		for (int j=1;j<=n;j++){
    			if (!vis[j]&&(mi==-1||dis[mi]>dis[j]))mi=j;
    		}
    		vis[mi]=true;
    		for (int j=1;j<=n;j++){
    			if (!vis[j]&&a[mi][j]!=0&&dis[mi]+a[mi][j]<dis[j])dis[j]=a[mi][j]+dis[mi];
    		}
    	}
    	for (int i=1;i<=n;i++){
    		if (i!=s){
    			if (vis[i]&&dis[i]!=INF)cout<<dis[i]<<" ";
    			else cout<<"-1 ";
    		}
    	}
    	return 0;
    }
    
    • 1

    信息

    ID
    1044
    时间
    1000ms
    内存
    32MiB
    难度
    6
    标签
    递交数
    41
    已通过
    12
    上传者