提交时间:2023-02-04 16:52:02
运行 ID: 68339
#include<bits/stdc++.h> using namespace std; const int N=514,M=114514; int h[N],e[M],ne[M],w[M],idx; int state[N]; int dist[N]; int n,m; void add(int a,int b,int c){ e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++; } void Dijkstra(int x,int y){ memset(dist,0x3f,sizeof dist); memset(state,0x3f,sizeof state); dist[x]=0; for(int i=1;i<=n;i++){ int t=-1; for(int j=1;j<=n;j++){ if(!state[j]&&(t==-1||dist[j]<dist[t])) t=j; } if(t==y) return; state[t]=1; for(int j=h[t];j!=-1;j=ne[j]){ int v=e[j]; dist[v]=min(dist[v],dist[t]+w[j]); } } } int main(){ while(~scanf("%d%d",&n,&m)){ memset(h,-1,sizeof h); while(m--){ int a,b,w; cin>>a>>b>>w; add(a,b,w),add(b,a,w); } int x,y; cin>>x>>y; Dijkstra(x,y); if(dist[y]!=0x3f3f3f3f) cout<<dist[y]; else cout<<"No path"; cout<<endl; } return 0; }