传送门:http://poj.org/problem?id=1797
不想吐槽了,弄了好久才AC
实现代码:
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <cstdio> #include <iostream> using namespace std; const int maxn=1005; const int INF=1<<20; int w[maxn][maxn]; int dis[maxn]; int vis[maxn]; void dijktra(int n){ for(int i=1;i<=n;i++){ dis[i]=w[1][i]; vis[i]=0; } vis[1]=1; for(int k=1;k<=n;k++){ int t=-1; int m=-INF; for(int i=1;i<=n;i++){ if(!vis[i]&&m<dis[i]){ t=i; m=dis[i]; } } if(t==-1) return; vis[t]=1; for(int i=1;i<=n;i++){ if(!vis[i]&&w[i][t]!=0){ if(min(dis[t],w[i][t])>dis[i]) dis[i]=min(dis[t],w[i][t]); } } } } int main(){ int t; scanf("%d",&t); for(int k=1;k<=t;k++ ){ int n,m; scanf("%d%d",&n,&m); memset(w,0,sizeof(w)); for(int i=0;i<m;i++){ int u,v,h; scanf("%d%d%d",&u,&v,&h); w[u][v]=h; w[v][u]=h; } dijktra(n); printf("Scenario #%d: ",k); printf("%d ",dis[n]); } return 0; }