//最短路,只不过这里是乘法,和加法做法一样
//这题精度不强
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define MAXN 1001
#define INF 0x3f3f3f3f
int in_queue[MAXN];
double dis[MAXN], each_dis[MAXN][MAXN], ans;
double spfa(int src, int des, int vertex)
{
int u, v;
queue<double> q;
memset(dis, 0, sizeof(dis));
memset(in_queue, 0, sizeof(in_queue));
q.push(src); in_queue[src] = 1;
while( !q.empty() ) {
u = q.front(); q.pop(); in_queue[u] = 0;
for(v = 1; v <= vertex; v ++) {
if( 0.0 == each_dis[u][v] ) {
continue;
}
if( dis[v] < ((0.0 == dis[u])?1 : dis[u])*each_dis[u][v] ) {
dis[v] = ((0.0 == dis[u])?1 : dis[u])*each_dis[u][v];
if( !in_queue[v] ) {
q.push(v);
}
}
}
}
return (0.0 == dis[des])? -1 : dis[des];
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
int vertex, ask, u, v;
while( ~scanf("%d", &vertex) ) {
memset(dis, 0, sizeof(dis));
for(int i = 1; i <= vertex; i ++) {
for(int j = 1; j <= vertex; j ++) {
scanf("%lf", &each_dis[i][j]);
}
}
scanf("%d", &ask);
for( ; ask; ask --) {
scanf("%d %d", &u, &v);
ans = spfa(u, v, vertex);
if( -1.0 == ans ) {
printf("What a pity!\n");
}
else {
printf("%.3lf\n", ans);
}
}
}
return 0;
}
hdu_1596
最新推荐文章于 2019-08-21 02:58:31 发布