hdu 1596 find the safest road (最短路)
2012-10-07 15:19
399 查看
/* floyd直接水过 */ #include<cstdio> #include<cstring> #include<iostream> #define N 1001 using namespace std; int n,x,y; double road ,t; int main() { while(scanf("%d",&n)!=EOF) { memset(road,0,sizeof(road)); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { scanf("%lf",&t); if(t>road[i][j]) { road[i][j]=t; road[j][i]=t; } } } for(int k=1;k<=n;k++) { for(int i=1;i<=n;i++) { if(road[i][k]) for(int j=1;j<=n;j++) { if(road[i][j]<road[i][k]*road[k][j]) { road[i][j]=road[i][k]*road[k][j]; } } } } scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d%d",&x,&y); if(road[x][y]!=0) printf("%.3lf\n",road[x][y]); else printf("What a pity!\n"); } } } /*spfa*/ #include<iostream> #include<cstring> #include<cstdio> #include<queue> using namespace std; double map[1001][1001],dis[1001]; int visit[1001],s,e,n; void SPFA(int s) { int p,i; queue<int> qu; qu.push(s); visit[s]=1; dis[s]=1; while(!qu.empty()) { p=qu.front(); qu.pop(); visit[p]=0; for(i=1;i<=n;i++) { if(map[p][i]==0||p==i) continue; if(dis[p]*map[p][i]>dis[i]) { dis[i]=dis[p]*map[p][i]; if(visit[i]==0) { visit[i]=1; qu.push(i); } } } } } int main() { freopen("test.txt","r",stdin); int i,j,m; while(scanf("%d",&n)!=EOF) { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { scanf("%lf",&map[i][j]); } } scanf("%d",&m); while(m--) { scanf("%d%d",&s,&e); for(i=1;i<=n;i++) { visit[i]=0; dis[i]=0; } SPFA(s); if(dis[e]!=0) printf("%.3lf\n",dis[e]); else puts("What a pity!"); } } return 0; }
相关文章推荐
- HDU 1596(find the safest road) 最短路(SPFA&FLOYD&Dijkstra)
- hdu 1596 find the safest road(最短路)
- HDU 1596 find the safest road (最短路)
- HDU-1596-find the safest road(最短路)
- HDU 1596 find the safest road 最短路
- hdu 1596 find the safest road(最短路求最大值的题目,有两种稍微不同的处理方式)
- HDU - 1596 find the safest road (最短路水题)
- hdu1596 find the safest road 最短路dijkstra
- HDU-1596 find the safest road(最短路[Dijkstra])
- HDU 1596 find the safest road (最短路)
- hdu 1596 find the safest road(乘积最短路)
- 两道最短路裸题(HDU 2544 最短路 / HDU 1596 find the safest road)
- HDU 1596 find the safest road (最短路)
- (阶段三 dijkstra1.4)HDU 1596 find the safest road(最短路的变形题:求乘积,求最大值)
- hdu 1596 find the safest road(最短路,模版题)
- HDU 1596 find the safest road
- Hdu 1596 find the safest road 变相最短路径 Dijstra算法
- HDU1596 find the safest road 解题报告--Dijkstra
- HDU 1596 find the safest road
- find the safest road HDU - 1596