hdu 1596 find the safest road
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1596
题目分类:dijkstra算法
错误点:不知道点的范围
代码:
#include<bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f const int maxv=1100; double cost[maxv][maxv]; double d[maxv]; bool vis[maxv]; int n,m; int st,en; int path[1100]; void dijkstra(int n,int st) { int i,j,pre; double minn; memset(vis,0,sizeof(vis)); vis[st]=1; for(i=1;i<=n;i++) { d[i]=cost[st][i]; path[i]=st; } path[st]=-1; pre=st; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(vis[j]==0&&d[pre]*cost[pre][j]>d[j]) d[j]=d[pre]*cost[pre][j],path[j]=pre; } minn=-1000000000.00; for(j=1;j<=n;j++) { if(vis[j]==0&&d[j]>minn) minn=d[j],pre=j; } vis[pre]=1; } return ; } int main() { while(scanf("%d",&n)!=EOF) { for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { scanf("%lf",&cost[i][j]); } } scanf("%d",&m); while(m--) { scanf("%d %d",&st,&en); //printf("aJDS\n"); dijkstra(n,st); /*for(int i=1;i<=n;i++) { printf("%.10lf\n",d[i]); }*/ if(d[en]==0) printf("What a pity!\n"); else printf("%.3lf\n",d[en]); } } return 0; }
anytime you feel the pain.hey,refrain.don't carry the world upon your shoulders