hdu 1596 find the safest road(最短路,模版题)

题目

这是用Dijsktra做的,稍加改动就好,1000ms。。好水。。

#define  _CRT_SECURE_NO_WARNINGS

#include<string.h>
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std; const int MAXN=; #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=;i<=n;i++)
{
lowcost[i]=cost[beg][i];vis[i]=false;
}
for(int i=;i<=n;i++)
{
typec temp=;
int k=-;
for(int j=;j<=n;j++)
if(!vis[j]&&lowcost[j]>temp)
{
temp=lowcost[j];
k=j;
}
vis[k]=true;
for(int l=;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=;i<=n;i++)
for(j=;j<=n;j++)
scanf("%lf",&cost[i][j]); scanf("%d",&m);
while(m--)
{
scanf("%d%d",&s,&t);
Dijkstra(n,s);
if(lowcost[t]==)
printf("What a pity!\n");
else
printf("%.3lf\n",lowcost[t]);
}
}
return ;
}

听说floyd也能过,但我就这就不写了

上一篇:java作业7


下一篇:特殊集合 Stack Queue Hashtable