Kruskal和map映射 解決POJ 2075
Description
You are the owner of SmallCableCo and have purchased the franchise rights for a small town. Unfortunately, you lack enough funds to start your business properly and are relying on parts you have found in an old warehouse you bought. Among your finds is a single spool of cable and a lot of connectors. You want to figure out whether you have enough cable to connect every house in town. You have a map of town with the distances for all the paths you may use to run your cable between the houses. You want to calculate the shortest length of cable you must have to connect all of the houses together.
Input
Only one town will be given in an input.
< house name A > < house name B > < distance >
Where the two house names match two different names in the list above and the distance is a positive real number. There will not be two paths between the same pair of houses.
- The first line gives the length of cable on the spool as a real number.
- The second line contains the number of houses, N
- The next N lines give the name of each house's owner. Each name consists of up to 20 characters {a–z,A–Z,0–9} and contains no whitespace or punctuation.
- Next line: M, number of paths between houses
- next M lines in the form
< house name A > < house name B > < distance >
Where the two house names match two different names in the list above and the distance is a positive real number. There will not be two paths between the same pair of houses.
Output
The output will consist of a single line. If there is not enough cable to connect all of the houses in the town, output
Not enough cable
If there is enough cable, then output
Need < X > miles of cable
Print X to the nearest tenth of a mile (0.1).
Not enough cable
If there is enough cable, then output
Need < X > miles of cable
Print X to the nearest tenth of a mile (0.1).
Sample Input
100.0
4
Jones
Smiths
Howards
Wangs
5
Jones Smiths 2.0
Jones Howards 4.2
Jones Wangs 6.7
Howards Wangs 4.0
Smiths Wangs 10.0
Sample Output
Need 10.2 miles of cable
其實只要你會了Kruskal算法,那么這道上題目的關鍵就在
于映射了。為什么呢?因為要用Kruskal就必然要用并查
集,而并查集處理的是數字間的關系,不可以處理字符串的。
所以,在這道題目中要將不同的名稱映射成不同的數字,以
此來用并查集判斷是否存在回路。可以說,做好了映射,這
道題目相當的簡單。那么怎樣來映射呢?這里有一個十分有
效的工具,那就是map,可以說這個是對我在編程技巧欄目中
發表《映射在編程中的應用》的補充和完善。
View Code
#include "iostream" #include "algorithm" #include "cstring" #include "string" #include "map" using namespace std; struct line //凡是用kruskal的結構體,其中的begin和end必是整形的,不然,無法使用并查集 { int begin; int end; double length; }; map<string, int> s_to_int; line num[1000]; int sum_line, father[1000], i, j; int a, b; double toatl, minlen; int peo, group; string name; int find(int k) { return father[k]==k?k:father[k]=find(father[k]); } int cmp(line first, line second) { return first.length<second.length; } double kruskal() { minlen = 0.0; for(i=0; i<peo; i++) father[i] = i; for(i=0; i<group; i++) { a = find(num[i].begin); b = find(num[i].end); if(a!=b) { father[a] = b; minlen += num[i].length; } } return minlen; } void init() { cin>>toatl>>peo; for(i=0; i<peo; i++) { cin>>name; s_to_int[name] = i; } cin>>group; for(i=0; i<group; i++) { cin>>name; num[i].begin = s_to_int[name]; cin>>name; num[i].end = s_to_int[name]; cin>>num[i].length; } sort(num, num+group, cmp); } int main() { init(); if(kruskal()<=toatl) cout<<"Need "<<kruskal()<<" miles of cable"<<endl; else cout<<"Not enough cable"<<endl; return 0; }
posted on 2011-09-21 11:10 More study needed. 閱讀(245) 評論(0) 收藏 舉報

浙公網安備 33010602011771號