提交时间:2023-02-03 11:15:56
运行 ID: 68208
#include<bits/stdc++.h> using namespace std; const int N=200; int m,n,u,v; int h[N],e[N],ne[N],w[N],idx=1; int dist[N],st[N]; void add(int a,int b,int wi){ e[idx]=b,ne[idx]=h[a],w[idx]=wi,h[a]=idx++; } void dijikstra(){ for(int i=1;i<=n;i++){ int y=0; for(int j=1;j<=n;j++){ if(st[j]==0&&(y==0||dist[j]<dist[y])){ y=j; } } //cout<<y<<endl; st[y]=1; for(int j=h[y];j!=-1;j=ne[j]){ int k=e[j]; if(dist[k]>dist[y]+w[j]){ dist[k]=dist[y]+w[j]; } // cout<<k<<" "<<dist[k]<<" "; } //cout<<endl; } } int main(){ while(cin>>n>>m){ memset(e,-1,sizeof(e)); memset(ne,-1,sizeof(ne)); memset(h,-1,sizeof(h)); memset(st,0,sizeof(st)); memset(dist,0x3f,sizeof(dist)); while(m--){ int a,b,wi; cin>>a>>b>>wi; add(a,b,wi); add(b,a,wi); } cin>>u>>v; dist[u]=0; dijikstra(); if(dist[v]!=0x3f3f3f3f){ cout<<dist[v]; } else{ cout<<"No path"; } cout<<endl; } return 0; }