【POJ】Jungle Roads
来源:互联网 发布:python配置文件怎么写 编辑:程序博客网 时间:2024/06/07 04:10
Problem here
Description
The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to maintain. The Council of Elders must choose to stop maintaining some roads. The map above on the left shows all the roads in use now and the cost in aacms per month to maintain them. Of course there needs to be some way to get between all the villages on maintained roads, even if the route is not as short as before. The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages. The villages are labeled A through I in the maps above. The map on the right shows the roads that could be maintained most cheaply, for 216 aacms per month. Your task is to write a program that will solve such problems.
Input
The input consists of one to 100 data sets, followed by a final line containing only 0. Each data set starts with a line containing only a number n, which is the number of villages, 1 < n < 27, and the villages are labeled with the first n letters of the alphabet, capitalized. Each data set is completed with n-1 lines that start with village labels in alphabetical order. There is no line for the last village. Each line for a village starts with the village label followed by a number, k, of roads from this village to villages with labels later in the alphabet. If k is greater than 0, the line continues with data for each of the k roads. The data for each road is the village label for the other end of the road followed by the monthly maintenance cost in aacms for the road. Maintenance costs will be positive integers less than 100. All data fields in the row are separated by single blanks. The road network will always allow travel between all the villages. The network will never have more than 75 roads. No village will have more than 15 roads going to other villages (before or after in the alphabet). In the sample input below, the first data set goes with the map above.
Output
The output is one integer per line for each data set: the minimum cost in aacms per month to maintain a road system that connect all the villages. Caution: A brute force solution that examines every possible set of roads will not finish within the one minute time limit.
Sample Input
9
A 2 B 12 I 25
B 3 C 10 H 40 I 8
C 2 D 18 G 55
D 1 E 44
E 2 F 60 G 38
F 0
G 1 H 35
H 1 I 35
3
A 2 B 10 C 40
B 1 C 20
0
Sample Output
216
30
Solution
Kruskal
#include <iostream>#include <stdio.h>#include <memory.h>#include <vector>#include <utility>#include <queue>#define INF_NUM 0xfffffffusing namespace std;vector<pair<pair<int, int>, int > >adj; //((頂點1, 頂點2), cost)vector<pair<int, int> >ans;int pre[100];int n;void init(){ adj.clear(); ans.clear(); memset(pre, INF_NUM, sizeof(pre));}bool cmp(pair<pair<int, int>, int > &a, pair<pair<int, int>, int >&b){ return a.second < b.second;}int find(int x){ int r = x; while(pre[r] != r){ r = pre[r]; } int i = x; int j; while(pre[i] != r){ j = pre[i]; pre[i] = r; i = j; } return r;}void join(int x, int y){ int p1 = find(x); int p2 = find(y); if(p1 != p2){ pre[p1] = p2; }}int kruskal(){ int ret = 0; sort(adj.begin(), adj.end(), cmp); for(int i = 0; i < n; i++) pre[i] = i; for(int i = 0; i < adj.size(); ++i){ int cost = adj[i].second; int u = adj[i].first.first; int v = adj[i].first.second; if(find(u) == find(v)) continue; join(u, v); ans.push_back(make_pair(u,v)); ret += cost; } return ret;}int main(){ while(cin >> n){ if(n == 0) break; init(); for(int cnt = 1; cnt <= n-1; cnt++){ char c; int k; cin >> c >> k; if(k > 0){ for(int i = 0; i < k; i++){ char cc; int kk; cin >> cc >> kk; int x = c - 'A'; int y = cc - 'A'; adj.push_back(make_pair(make_pair(x, y), kk)); } } } cout << kruskal() << endl; } return 0;}
- POJ 1251 Jungle Roads
- poj 1251 Jungle Roads
- poj 1251 Jungle Roads
- POJ-1251 jungle roads
- poj 1251 Jungle Roads
- poj 1301 Jungle Roads
- POJ-1251-Jungle Roads
- POJ 1251 Jungle Roads
- Poj 1251 Jungle Roads
- POJ-1251-Jungle Roads
- POJ 1251 Jungle Roads
- POJ 1251 Jungle Roads
- POJ-1251-Jungle Roads
- POJ 1251 Jungle Roads
- POJ:1251 Jungle Roads
- POJ 1251 Jungle Roads
- POJ 1251 Jungle Roads
- POJ-1251 Jungle Roads
- AsyncTask原理分析
- Recommender System Fragment 2
- 通讯录动态开辟版本
- 忘记mysql root用户密码的解决办法(skip-grant-tables)
- hdoj 1575 Tr A 51nod oj 1113 矩阵快速幂【矩阵快速幂】
- 【POJ】Jungle Roads
- HDU1596 find the safest road(floyd)
- 想成为嵌入式程序员应知道的0x10个基本问题
- Java中可变长参数的使用及注意事项
- SQL内连接、左外链接、右外连接、交叉链接区别
- Advanced Fruits hd 1503
- "Mac OS X"想要进行更改。键入管理员的名称和密码以允许执行此操作("Mac OS X"想使用系统钥匙串)
- hdu 5823color II 状压(2016多校第八场1003)
- 【POJ】Stockbroker Grapevine