#include<cstdio>
#include<cstring>
#include<queue>
#define INF 100000000
using namespace std;
double map[1005][1005];
double d[1005];
int vis[1005];
struct cmp
{
bool operator()(int lh,int rh)
{
return d[lh]<d[rh];
}
};
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
scanf("%lf",&map[i][j]);
}
int m;
scanf("%d",&m);
for(i=0;i<m;i++)
{
int s,e;
for(j=1;j<=n;j++)
d[j]=0;
scanf("%d%d",&s,&e);
memset(vis,0,sizeof(vis));
priority_queue<int,vector<int>,cmp> q;
d[s]=1;
q.push(s);
while(!q.empty())
{
int t;
t=q.top();
q.pop();
if(vis[t])
continue;
vis[t]=1;
int k;
for(k=1;k<=n;k++)
{
if(k==t)
continue;
if(!vis[k]&&d[k]<d[t]*map[t][k])
{
d[k]=d[t]*map[t][k];
q.push(k);
}
}
}
if(!d[e])
printf("What a pity!\n");
else
printf("%.3f\n",d[e]);
}
}
}
其实还是最短路问题~~第一次一次ac最短路哇·~~~
hdu 1596最短路
最新推荐文章于 2018-03-12 18:08:52 发布