poj 1125 Stockbroker Grapevine

Floyd算法。0ms水过。

代码如下:

/*
Poj: Stockbroker Grapevine
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>

#define MAX_TIME (1 << 20)
#define MIN(a, b) ((a) > (b) ? (b) : (a))

using namespace std;

const int M = 115;

int map[M][M];
int min_index, min_time;
int n;

void Floyd()
{
    for(int k = 1; k <= n; k++) {
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                if(map[i][j] > map[i][k] + map[k][j])
                    map[i][j] = map[i][k] + map[k][j];
            }
        }
    }
}

int main()
{
    //freopen("data.in", "rb", stdin);
    while(scanf("%d", &n), n != 0) {
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                if(i == j) {
                    map[i][j] = 0;
                }
                else {
                    map[i][j] = MAX_TIME;
                }
            }
        }
        
        for(int i = 1; i <= n; i++) {
            int a;
            scanf("%d", &a);
            while(a--) {
                int b, c;
                scanf("%d%d", &b, &c);
                map[i][b] = c;
            }
        }
        
        Floyd();
        
        min_index = 0; 
        min_time = MAX_TIME;
        for(int i = 1; i <= n; i++) {
            int tmp = 0;
            for(int j = 1; j <= n; j++) {
                tmp = tmp > map[i][j] ? tmp : map[i][j];
            }
            if(tmp < min_time) {
                min_index = i;
                min_time = tmp;
            }
        }
            
        if(min_time != MAX_TIME) {
            printf("%d %d\n", min_index, min_time);
        }
        else {
            printf("disjoint\n");
        }
    }
    
    return 0;
}


你可能感兴趣的:(poj 1125 Stockbroker Grapevine)