uva 1056

floyd 算法 用了stl 的map 存名字的时候比较方便

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <map>
#include <string>
#include <cstring>
#include <algorithm>
#define maxn 100010
#define INF 0x7fffffff
#define inf 10000000
#define ull unsigned long long
#define ll long long
using namespace std;

map<string, int> people;
int g[55][55], P, R;

void init()
{
    people.clear();
    for(int i = 0; i < 55; ++ i)
        for(int j = 0; j < 55; ++ j)
            g[i][j] = inf;
}

void read()
{
    string name1, name2;
    char n1[100], n2[100];
    int id = 0;
    for(int i = 0; i < R; ++i)
    {
        scanf("%s%s", n1, n2);
        name1 = string(n1), name2 = string(n2);
        if(people.count(name1) == 0) people[name1] = id++;
        if(people.count(name2) == 0) people[name2] = id++;
        g[people[name1]][people[name2]] = g[people[name2]][people[name1]] = 1;
    }
}

int solve()
{
    int _min = 0;
    for(int k = 0; k < P; ++ k)
        for(int i = 0; i < P; ++ i)
            for(int j = 0; j < P; ++ j)
                g[i][j] = min(g[i][j], g[i][k]+g[k][j]);
    for(int i = 0; i < P; ++ i)
        for(int j = i+1; j < P; ++ j)
            _min = max(_min, g[i][j]);
    return _min;
}

int main()
{
    int ca = 1;
    while(scanf("%d%d", &P, &R) == 2 && P+R)
    {
        init();
        read();
        int ans = solve();
        printf("Network %d: ", ca++);
        if(ans < inf)
            printf("%d\n", ans);
        else
            puts("DISCONNECTED");
        puts("");
    }
    return 0;
}


posted @ 2014-01-14 12:51  xlc2845  阅读(169)  评论(0)    收藏  举报