24h購物| | PChome| 登入
2012-06-02 20:00:04| 人氣1,290| 回應0 | 上一篇 | 下一篇

[UVA][最大生成樹] 1234 - RACING

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

Singapore will host a Formula One race in 2008. The race will be held on a 5.067km long street circuit, consisting of 14 left hand turns and 10 right hand turns. In the run up to the F1 race, the number of illegal night street racing activities have been on the rise. Such races consists of several rounds around a designated street circuit.

epsfbox{p4110a.eps}

The authorities would like to deploy a new vehicle monitoring system in order to catch these illegal Saint Andrew's Road, part of the Formula One circuit racers. The system consists of a (Kenny Pek, Piccom) number of cameras mounted along various roads. For the system to be effective, there should be at least one camera along each of the possible circuits.

The Singapore road system can be represented as a series of junctions and connecting bidirectional roads (see Figure 5). A possible racing circuit consists of a start junction followed by a path consisting of three or more roads that eventually leads back to the start junction. Each road in a racing circuit can be traversed only in one direction, and only once.

Your task is to write a program that computes the optimal placement of the vehicle-monitoring cameras. You will be provided with a description of a connected road network to be monitored in terms of the roads and junctions. The junctions are identified by the bigger numbers in Figure 5. A camera can be deployed on the roads (and not the junctions).

epsfbox{p4110b.eps}

The cost of deploying a camera depends on the road on which it is placed. The smaller numbers by the roads in Figure 5 indicate the cost of deploying a camera on that road. Your job is to select a set of roads that minimizes the total cost of deployment while ensuring that there is at least one camera along every possible racing circuit (i.e. loop in the road network).

Input 

The input consists of a line containing the number c of datasets, followed by c datasets, followed by a line containing the number `0'.

The first line of each dataset contains two positive integers, n and m , separated by a blank, which represent the number of junctions and number of roads, respectively. You may assume that 0 < n < 10000 and 0 < m < 100000 . For simplicity, we label each of the n junctions from 1 to n . The following m lines of each dataset each describes one road. Each line consists of three positive integers which are the labels of two different junctions and the cost of deploying a camera on this road. The cost of deploying a camera is between 1 and 1000.

Output 

The output consists of one line for each dataset. The c -th line contains one single number, representing the minimal cost of setting up the vehicle monitoring system such that there is at least one camera along every possible circuit.


Note: This data set depicts the situation shown in Figure 5. The two cameras show where cameras might be placed in order to monitor each circuit at minimal cost. Since each of the cameras have a cost of 3, the total minimal cost is 6.

Sample Input 

1 
6 7 
1 2 5
2 3 3
1 4 5
4 5 4
5 6 4
6 3 3
5 2 3
0

Sample Output 

6


題目的意思為賽道是一個環道, 但我們不知道賽道是怎麼分布的, 因此希望每個環道至少有一台監視器,
希望總成本最低, 那如果把環道剔除的話, 所剩的是一棵樹, 成本最低, 也就是希望產生的樹成本最高

#include <stdio.h>
struct edge {
int x, y, v;
};
edge D[100000], E[100000];
void sort(int n) {
int w[1001] = {}, i;
for(i = 0; i < n; i++)
w[D[i].v]++;
for(i = 999; i >= 0; i--)
w[i] += w[i+1];
for(i = 0; i < n; i++) {
E[--w[D[i].v]] = D[i];
}
}
int p[10001], r[10001];
void init(int n) {
int i;
for(i = 0; i <= n; i++)
p[i] = i, r[i] = 1;
}
int find(int x) {
return p[x] == x ? x : (p[x]=find(p[x]));
}
int joint(int x, int y) {
x = find(x), y = find(y);
if(x != y) {
if(r[x] > r[y])
r[x] += r[y], p[y] = x;
else
r[y] += r[x], p[x] = y;
return 1;
}
return 0;
}
int main() {
int t, n, m, i;
scanf("%d", &t);
while(t--) {
scanf("%d %d", &n, &m);
int sum = 0;
for(i = 0; i < m; i++) {
scanf("%d %d %d", &D[i].x, &D[i].y, &D[i].v);
sum += D[i].v;
}
sort(m);
init(n);
for(i = 0; i < m; i++) {
if(joint(E[i].x, E[i].y))
sum -= E[i].v;
}
printf("%d\n", sum);
}
scanf("%d", &t);
return 0;
}
 

台長: Morris
人氣(1,290) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][gcd變形] 10104 - Euclid Problem
此分類上一篇:[UVA] 10701 - Pre, in and post

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