24h購物| | PChome| 登入
2012-12-20 09:15:33| 人氣884| 回應0 | 上一篇 | 下一篇

[UVA][floyd] 1056 - Degrees of Separation

推薦 0 收藏 0 轉貼0 訂閱站台

In our increasingly interconnected world, it has been speculated that everyone on Earth is related to everyone else by no more than six degrees of separation. In this problem, you must write a program to find the maximum degree of separation for a network of people.

For any two people, the degree of separation is the minimum number of relationships that must be traversed to connect the two people. For a network, the maximum degree of separation is the largest degree of separation between any two people in the network. If there is a pair of people in the network who are not connected by a chain of relationships, the network is disconnected.

As shown below, a network can be described as a set of symmetric relationships each of which connects two people. A line represents a relationship between two people. Network A illustrates a network with 2 as the maximum degree of separation. Network B is disconnected.

epsfbox{p3569.eps} Network A: Max. degree of separation = 2 Network B: Disconnected

Input 

The input consists of data sets that describe networks of people. For each data set, the first line has two integers: P (2$ le$P$ le$50), the number of people in the network, and R (R$ ge$1), the number of network relationships. Following that first line are R relationships. Each relationship consists of two strings that are names of people in the network who are related. Names are unique and contain no blank spaces. Because a person may be related to more than one other person, a name may appear multiple times in a data set.

The final test case is followed by a line containing two zeroes.

Output 

For each network, display the network number followed by the maximum degree of separation. If the network is disconnected, display `DISCONNECTED'. Display a blank line after the output for each network. Use the format illustrated in the sample output.

Sample Input 

4  4 
Ashok  Kiyoshi   Ursala  Chun   Ursala  Kiyoshi 
Kiyoshi  Chun 
4  2 
Ashok  Chun   Ursala  Kiyoshi 
6 5 
Bubba  Cooter   Ashok  Kiyoshi   Ursala  Chun 
Ursala  Kiyoshi   Kiyoshi  Chun 
0  0

Sample Output 

Network 1: 2 

Network 2: DISCONNECTED 

Network 3: DISCONNECTED

Claimer: The data used in this problem is unofficial data prepared by Derek Kisman. So any mistake here does not imply mistake in the offcial judge data. Only Derek Kisman is responsible for the mistakes. Report mistakes to dkisman@acm.org



all pairs 全局最短路的最大值。


#include <stdio.h>
#include <iostream>
#include <map>
#define oo 0xffff
using namespace std;

int main() {
    int n, m, cases = 0, i, j, k;
    char a[50], b[50];
    while(scanf("%d %d", &n, &m) == 2) {
        if(n == 0 && m == 0)
            break;
        int g[51][51], x, y;
        for(i = 1; i <= n; i++) {
            for(j = 1; j <= n; j++)
                g[i][j] = oo;
            g[i][i] = 0;
        }
        map<string, int> R;
        map<string, int>::iterator it;
        int size = 0;
        while(m--) {
            scanf("%s %s", a, b);
            it = R.find(a);
            if(it == R.end())
                R[a] = ++size, x = size;
            else    x = it->second;
            it = R.find(b);
            if(it == R.end())
                R[b] = ++size, y = size;
            else    y = it->second;
            g[x][y] = g[y][x] = 1;
        }
        for(k = 1; k <= n; k++) {
            for(i = 1; i <= n; i++) {
                for(j = 1; j <= n; j++) {
                    if(g[i][j] > g[i][k] + g[k][j])
                        g[i][j] = g[i][k] + g[k][j];
                }
            }
        }
        int mx = 0;
        for(i = 1; i <= n; i++)
            for(j = 1; j <= n; j++)
                if(g[i][j] > mx)
                    mx = g[i][j];
        printf("Network %d: ", ++cases);
        if(mx == oo)
            puts("DISCONNECTED");
        else
            printf("%d\n", mx);
        puts("");
    }
    return 0;
}

台長: Morris
人氣(884) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][井字遊戲盤面判斷] 10363 - Tic Tac Toe
此分類上一篇:[UVA][骰子同構] 11959 - Dice

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文