24h購物| | PChome| 登入
2012-05-12 10:34:24| 人氣655| 回應0 | 上一篇 | 下一篇

[UVA][path] 11015 - 05-2 Rendezvous

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

Problem D
05-2 Rendezvous
Input: Standard Input

Output:Standard Output

Our team wants to meet together in one place to doour homework. Our 05-2 team has 22 members. Because of that we are a bit confusedon what the best place is to meet one another. So we have decided to meet inthe place that has the least total minimum cost. Total minimum cost is definedas the sum of the distance from all source places to destination place. In thisproblem you are asked to write a program that determines the possible placethat has the least total minimum cost. The house of each member is consideredas a place here.

Input

The input consists of at most 105 test cases.

 

The first line of each test case consist two integers N (1 ≤ N ≤22) and M (1 ≤ M ≤ (N^2-N)/2), specifying the number of members andthe number of path. Then it is followed by N lines each consisting of member’sname, each member’s name contains at most 10 lower case characters. It is followedby M lines contains three integers i, j (1≤ i, j ≤ N) and k (1 ≤ k ≤ 1000). It meansthe distance from place i to place j have k cost.

If the place i to place j have k cost thenfrom place j to place i have k cost too. The input N= 0 mark as the end of the input.

The member number starts from 1.

Output

For each test cases you should output one line contain like this:

 

Case #i : XXX”(without quote)

 

where i replacedwith the case number and XXX replaced with the member name that have the leasttotal minimum cost. If there are two or more members that have the same leasttotal minimum cost you should output the name of member that appear first.

 

SampleInput                          Output forSample Input

4 3

timotius

harry

richard

januar

1 2 10

1 3 8

1 4 6

4 3

rocky

herwin

gaston

jefry

1 2 5

1 3 5

1 4 5

0 0

 

Case #1 : timotius

Case #2 : rocky

 




之前在 ZJ 寫的沒過, 重新寫一次,
求某個人到其他人的路徑總合最小

#include <stdio.h>
#include <string.h>
#define min(x, y) ((x) < (y) ? (x) : (y))
int main() {
    int n, m, t = 0;
    while(scanf("%d %d", &n, &m) == 2) {
        if(n == 0 && m == 0)
            break;
        char name[30][30];
        int map[30][30], x, y, v;
        int i, j, k;
        memset(map, 63, sizeof(map));
        for(i = 0; i < n; i++)
            scanf("%s", name[i]);
        while(m--) {
            scanf("%d %d %d", &x, &y, &v);
            x--, y--;
            map[x][y] = min(v, map[x][y]);
            map[y][x] = min(v, map[y][x]);
        }
        for(k = 0; k < n; k++)
            for(i = 0; i < n; i++)
                for(j = 0; j < n; j++)
                    map[i][j] = min(map[i][j], map[i][k]+map[k][j]);
        int ans = 0xfffffff, idx = 0, tmp;
        for(i = 0; i < n; i++) {
            tmp = 0;
            for(j = 0; j < n; j++)
                if(i != j)
                    tmp += map[i][j];
            if(tmp < ans)
                ans =  tmp, idx = i;
        }
        printf("Case #%d : %s\n", ++t, name[idx]);
    }
    return 0;
}

台長: Morris
人氣(655) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][二維線段樹] 11297 - Census
此分類上一篇:[UVA][ConvexHull] 109 - SCUD Busters

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