hdu 1596 find the safest road(最短路,模版题)
2014-02-10 23:13
253 查看
题目
这是用Dijsktra做的,稍加改动就好,1000ms。。好水。。
View Code
听说floyd也能过,但我就这就不写了
这是用Dijsktra做的,稍加改动就好,1000ms。。好水。。
#define _CRT_SECURE_NO_WARNINGS #include<string.h> #include<stdio.h> #include<math.h> #include<algorithm> using namespace std; const int MAXN=1010; #define typec double const typec INF=0x3f3f3f3f;//防止后面溢出,这个不能太大 bool vis[MAXN]; typec cost[MAXN][MAXN]; typec lowcost[MAXN]; void Dijkstra(int n,int beg) { for(int i=1;i<=n;i++) { lowcost[i]=cost[beg][i];vis[i]=false; } for(int i=1;i<=n;i++) { typec temp=0; int k=-1; for(int j=1;j<=n;j++) if(!vis[j]&&lowcost[j]>temp) { temp=lowcost[j]; k=j; } vis[k]=true; for(int l=1;l<=n;l++) if(!vis[l]) if(lowcost[l]<lowcost[k]*cost[k][l]) lowcost[l]=lowcost[k]*cost[k][l]; } } int main() { int n,m,i,j,s,t; while(scanf("%d",&n)!=EOF) { for(i=1;i<=n;i++) for(j=1;j<=n;j++) scanf("%lf",&cost[i][j]); scanf("%d",&m); while(m--) { scanf("%d%d",&s,&t); Dijkstra(n,s); if(lowcost[t]==0) printf("What a pity!\n"); else printf("%.3lf\n",lowcost[t]); } } return 0; }
View Code
听说floyd也能过,但我就这就不写了
相关文章推荐
- hdu 1596 find the safest road(最短路求最大值的题目,有两种稍微不同的处理方式)
- 两道最短路裸题(HDU 2544 最短路 / 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 最短路
- 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) 最短路(SPFA&FLOYD&Dijkstra)
- HDU-1596 find the safest road(最短路[Dijkstra])
- (阶段三 dijkstra1.4)HDU 1596 find the safest road(最短路的变形题:求乘积,求最大值)
- hdu1596 find the safest road 最短路dijkstra
- HDU 1596 find the safest road
- HDU-1596-find the safest road
- HDU 1596:find the safest road【Dijkstra & SPFA】
- HDU 1596 find the safest road
- hdu1596 find the safest road(最短路径floyd)