返回顶部

POJ - 1125 - Stockbroker Grapevine = Floyd

http://poj.org/problem?id=1125

题意:给一个有向图,选一个起点,这个起点满足到达其他起点的时间的最大值最小。

图的顶点个数不超过100个,直接Floyd就完事。

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;

int dis[105][105];

int main() {
#ifdef Yinku
    freopen("Yinku.in", "r", stdin);
#endif // Yinku
    int n;
    while(~scanf("%d", &n)) {
        if(n == 0)
            break;
        for(int i = 1; i <= n; ++i) {
            for(int j = 1; j <= n; ++j) {
                dis[i][j] = ((i == j) ? 0 : 0x3f3f3f3f);
            }
        }
        for(int i = 1; i <= n; ++i) {
            int x;
            scanf("%d", &x);
            while(x--) {
                int v, w;
                scanf("%d%d", &v, &w);
                dis[i][v] = min(dis[i][v], w);
                //dis[v][i] = min(dis[v][i], w);
            }
        }
        for(int k = 1; k <= n; ++k) {
            for(int i = 1; i <= n; ++i) {
                for(int j = 1; j <= n; ++j) {
                    dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
                }
            }
        }
        int ansv = 0x3f3f3f3f;
        int ansi = 0;
        for(int i = 1; i <= n; ++i) {
            int maxv = 0;
            for(int j = 1; j <= n; ++j) {
                if(dis[i][j] > maxv) {
                    maxv = dis[i][j];
                }
            }
            if(maxv < ansv) {
                ansv = maxv;
                ansi = i;
            }
        }

        if(ansv >= 1000000) {
            puts("disjoint");
        } else {
            printf("%d %d\n", ansi, ansv);
        }
    }
}
posted @ 2019-10-22 18:29  Inko  阅读(89)  评论(0编辑  收藏  举报