HDU 1596 find the safest road
[0,1]
区间乘积最长路。和这个类似,不过这道题没有大于1
的边,所以没有“负环”(在一个环上走不会越来越大)。
这道题已经给了邻接矩阵(所以连初始化都不用了),按有向图算,不连通的边都给了0
。所以最后查询的时候还是0
就意味着不连通。
这道题用不着考虑floyd
里面那三个相不相等。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <string>
#include <queue>
using namespace std;
const double INF = 1e9; // double...然而没用到
const int MAX = 1001;
int N, Q;
double g[MAX][MAX];
void floyd()
{
for (int k = 1; k <= N; k++)
{
for (int i = 1; i <= N; i++)
{
for (int j = 1; j <= N; j++)
{
if (g[i][k] != 0.0 && g[k][j] != 0.0 && g[i][j] < g[i][k] * g[k][j])
g[i][j] = g[i][k] * g[k][j];
}
}
}
}
int main()
{
int a, b;
for (; ~scanf("%d", &N);)
{
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++)
scanf("%lf", &g[i][j]);
floyd();
scanf("%d", &Q);
for (; Q--;)
{
scanf("%d%d", &a, &b);
if (g[a][b] == 0.0)
printf("What a pity!\n");
else printf("%.3lf\n", g[a][b]);
}
}
return 0;
}