POJ - 1125 - Stockbroker Grapevine = Floyd

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

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

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

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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);
        }
    }
}

你可能感兴趣的:(POJ - 1125 - Stockbroker Grapevine = Floyd)