HDU 1596

HDU 1596

Floyd 变形

根据题目中的定义改写新的最短路更新条件

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long LL;
const int N = 1e3 + 10;
const double eps = 1e-6;
int n,a,b,m;
double g[N][N],dist[N][N];
void floyd() {
    for(int k = 1;k <= n; ++k)
        for(int i = 1;i <= n; ++i)
            for(int j = 1;j <= n; ++j)
                g[i][j] = max(g[i][j],g[i][k] * g[k][j]);
}
int main() {
    //ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    while(cin >> n) {
        for(int i = 1;i <= n; ++i)
            for(int j = 1;j <= n; ++j)
                scanf("%lf",&g[i][j]);
        floyd();
        cin >> m;
        while(m --) {
            cin >> a >> b;
            if(g[a][b] < eps) printf("What a pity!\n");
            else printf("%.3lf\n",g[a][b]);
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/lukelmouse/p/12424110.html
今日推荐