24h購物| | PChome| 登入
2012-02-25 21:35:15| 人氣1,274| 回應0 | 上一篇 | 下一篇

[UVA] 1208 - Oreon

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

In the 25th century, civilization is struck by a series of calamities that eventually led mankind to build walled cities interconnected by tunnel bridges to facilitate transportation. Each walled city possesses a unique ore required to build and repair all infrastructure including the tunnels. This material which when combined with other ores from all others cities form an almost indestructible material called ``oreon".

Outside the walled cities are uncivilized barbarians armed with antiquated but destructive weaponry which can effectively shoot down any air transport, but only damage and not penetrate tunnel bridges. Thus each city is interconnected to more than one city in order to have access redundancy in case one of its interconnecting tunnels is damaged.

If a tunnel is damaged, it becomes impassable and would require a substantial amount of ``oreon" to repair the damage. When a single city is made isolated, meaning all of its interconnections are damaged, ``oreon" cannot be manufactured which may lead to the eventual destruction of the wall fortifying the city. You, being the head of the homeland defense unit, are tasked to ensure that all cities remain accessible even by at least a single interconnecting tunnel at all times. Faced with only a limited manpower in the defense unit, you have to determine which tunnel to protect using the least number of people and ensure that no city will be isolated.

Figure 2 shows a map of the walled cities, their interconnecting tunnels and the number of security personnel.

=6in epsfbox{p3171.eps}

Figure 2: Map of six cities and its interconnecting tunnels

Input 

The input will contain several test cases. The first line will indicate the number of test cases. Each test case begins with a number representing the number of walled cities. Cities are labeled alphabetically using the letters in the English alphabet. The subsequent lines contain the number of security personnel needed to protect the tunnel connecting each city to all other cities. A value of zero implies no security personnel needed since no tunnel exists. You are to output which tunnel should be protected and how many personnel are needed for each tunnel.

Output 

The output shows the tunnel connection which is named after the cities that it connects (in alphabetical order) and the number of personel needed to protect the tunnel. Order the records in increasing order of presonal. In case two tunnels have the same number of personel, write them in lexicographycal order.

Sample Input 

1 
6 
0, 8, 12, 0, 0, 7 
8, 0, 0, 3, 0, 0 
12, 0, 0, 0, 6, 0 
0, 3, 0, 0, 0, 4 
0, 0, 6, 0, 0, 5 
7, 0, 0, 4, 5, 0

Sample Output 

Case 1: 
B-D 3 
D-F 4 
E-F 5 
C-E 6 
A-F 7



做法 : 最小生成樹
題目比較麻煩點的就是輸出時要按照字典順序

#include <stdio.h>
#include <stdlib.h>
int P[30], R[30];
int MakeInit(int N) {
int i;
for(i = 0; i <= N; i++)
P[i] = i, R[i] = 1;
}
int FindP(int x) {
if(P[x] != x)
P[x] = FindP(P[x]);
return P[x];
}
int Link(int x, int y) {
if(R[x] > R[y])
P[y] = x, R[x] += R[y];
else
P[x] = y, R[y] += R[x];
}
int Union(int x, int y) {
x = FindP(x), y = FindP(y);
if(x != y) {
Link(x, y);
return 1;
}
return 0;
}
typedef struct {
int x, y, v;
}Path;
int cmp(const void *i, const void *j) {
Path *a, *b;
a = (Path *)i, b = (Path *)j;
if(a->v != b->v)
return a->v - b->v;
if(a->x != b->x)
return a->x - b->x;
return a->y - b->y;
}
int main() {
int T, N, C = 0, i, j, tmp;
char cha;
scanf("%d", &T);
while(T--) {
scanf("%d", &N);
Path D[1000];
int M = 0;
for(i = 0; i < N; i++) {
for(j = 0; j < N; j++) {
scanf("%d%c", &tmp, &cha);
if(i < j && tmp)
D[M].x = i, D[M].y = j, D[M].v = tmp, M++;
}
}
MakeInit(N);
qsort(D, M, sizeof(Path), cmp);
printf("Case %d:\n", ++C);
for(i = 0; i < M; i++) {
if(Union(D[i].x, D[i].y) == 1)
printf("%c-%c %d\n", D[i].x+'A', D[i].y+'A', D[i].v);
}
}
return 0;
}

 

台長: Morris
人氣(1,274) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 112 - Tree Summing
此分類上一篇:[UVA] 11995 - I Can Guess the Data Structure!

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