Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9122 Accepted Submission(s):
3213
#include<stdio.h> #include<string.h> #define MAX 1010 #define INF 0x3f3f3f #define DD double int n,m; DD map[MAX][MAX],low[MAX]; int vis[MAX]; void getmap() { int i,j; DD a; memset(map,0,sizeof(map)); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { scanf("%lf",&a); map[i][j]=a; } } } void dijkstra(int a,int b) { int i,j,next; DD max; for(i=1;i<=n;i++) low[i]=map[a][i]; memset(vis,0,sizeof(vis)); vis[a]=1; for(i=1;i<n;i++) { max=0; next=a; for(j=1;j<=n;j++) { if(!vis[j]&&max<low[j]) { next=j; max=low[j]; } } vis[next]=1; for(j=1;j<=n;j++) { if(!vis[j]&&low[j]<low[next]*map[next][j]) //此处的求法有所不同 low[j]=low[next]*map[next][j]; } } if(low[b]==0) printf("What a pity!\n"); else printf("%.3lf\n",low[b]); } void solve() { scanf("%d",&m); for(int i=1;i<=m;i++) { int a,b; scanf("%d%d",&a,&b); if(a==b) printf("1.000\n");//注意自己到自己的情况 else dijkstra(a,b); } } int main() { int i,j; while(scanf("%d",&n)!=EOF) { getmap(); solve(); } return 0; }
hdoj 1596 find the safest road【最短路变形,求最大安全系数】
原文:http://www.cnblogs.com/tonghao/p/4737576.html