POJ2075 & TOJ2119 Tangled in Cables

来源:互联网 发布:aes算法密钥最小长度 编辑:程序博客网 时间:2024/06/02 18:51
Tangled in Cables
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 5410 Accepted: 2175

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. 
  • 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).

Sample Input

100.04JonesSmithsHowardsWangs5Jones Smiths 2.0Jones Howards 4.2Jones Wangs 6.7Howards Wangs 4.0Smiths Wangs 10.0

Sample Output

Need 10.2 miles of cable

Source

Mid-Atlantic 2004
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <cmath>#include <map>using namespace std;double kruscal(int x);const int maxn = 5000;const int maxm = 110000;double tot;int gn, gm;//POJ CE map<string, int> gmap;struct edge  {    int x;    int y;    double w;}e[maxm];int f[maxn];bool flag;bool cmp(const edge& x, const edge& y) {    return x.w < y.w;}void init() {    flag = false;    string str;    int i;    gmap.clear();    scanf("%lf%d", &tot, &gn);    int counter = 0;    map<string, int>::iterator it;    for(i = 1; i <= gn; i++) {        cin >> str;        gmap.insert(pair<string, int>(str, ++counter));    }    scanf("%d", &gm);    string x, y;    double w;    int index = 0, from, to;    for(i = 1; i <= gm; i++) {        cin >> x >> y >> w;        it = gmap.find(x);        from = it->second;        it = gmap.find(y);        to = it->second;        e[index].x = from;        e[index].y = to;        e[index].w = w;        index++;    }    double res = kruscal(index);     if(flag) {        printf("Not enough cable\n");    }    else {        printf("Need %.1lf miles of cable\n", res);    }}int getfather(int x) {    if(x==f[x]) return x;    else return f[x] = getfather(f[x]);}/*void print(int Size) {    for(int i = 0; i < Size; i++) {        cout << e[i].x << "--" << e[i].y << "=" << e[i].w << endl;    }}*/double kruscal(int edgeSize) {    int i;    sort(e, e+edgeSize, cmp);   // print(edgeSize);    int cnt = gn;    for(i = 1; i <= gn; i++) {        f[i] = i;    }    double ans = 0.0;    for(i = 0; i < edgeSize; i++) {        int t1 = getfather(e[i].x);        int t2 = getfather(e[i].y);        if(t1 != t2) {            cnt--;            f[t1] = t2;            ans += e[i].w;            if(ans > tot) {                flag = true;                break;            }        }        if(cnt == 1) break;    }    return ans;}int main(){    init();    return 0;}
原创粉丝点击