Hdu oj 1874 畅通工程续

来源:互联网 发布:羽绒被牌子 知乎 编辑:程序博客网 时间:2024/05/29 03:23

题目:点击打开链接

#include<stdio.h>#include<algorithm>#define INF 0x3f3f3f3fusing namespace std;bool used[210];int cost[210][210],d[210];  int m,n;int o,p;void dijkstra(int x){for(int i=0;i<m;i++){d[i]=INF;used[i]=false;}d[x]=0;int u;while(true){int v=-1;for(u=0;u<m;u++)//不能把m换成P,尽管p为终点,d[u]要持续更新 if(!used[u]&&(v==-1||d[u]<d[v]))v=u;if(v==-1)break;used[v]=true;for(u=0;u<m;u++)d[u]=min(d[u],d[v]+cost[v][u]);}if(d[p]>=INF)printf("-1\n");else    printf("%d\n",d[p]);}int main(){while(~scanf("%d%d",&m,&n)){int i,j;int a,b,c;for(i=0;i<m;i++)   for(j=0;j<m;j++)   cost[i][j]=INF;for(i=0;i<n;i++){scanf("%d%d%d",&a,&b,&c);if(c<cost[a][b])//  !!! {     cost[a][b]=c;   cost[b][a]=c;}}scanf("%d%d",&o,&p);dijkstra(o);}return 0;}

0 0