POJ 2075 Tangled in cables (Kruskal algorithm MST + map)

Source: Internet
Author: User


Tangled in Cables
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 6039 Accepted: 2386

Description

The owner of Smallcableco and has purchased the franchise rights for a small town. Unfortunately, lack enough funds to start your business properly and is relying on parts we have found in a old war Ehouse you bought. Among your finds is a single spool of cable and a lot of connectors. You want-to-figure out whether the enough cable to connect every house in town. You had a map of town with the distances for all the paths and you could use the run your cable between the houses. You want to calculate the shortest length of cable you must has to connect all of the houses together.

Input

Only one town would be given on 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 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 house names match, different names in the list above and the distance are a positive real number. There'll is not paths between the same pair of houses.

Output

The output would 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 and 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


Topic Link: poj.org/problem?id=2075

Topic: Give a bus a long, and n person, M group relationship, represents the distance between two people, now requires that all people are connected, ask the minimum length of the line, if more than the total length, then output not enough cable

Topic Analysis: Bare minimum spanning tree problem, directly map the string, Run a little bit more Kruskal

#include <cstdio> #include <cstring> #include <map> #include <algorithm> #include <iostream  >using namespace Std;int Const MAX = 1e4;int fa[max];int re[max];int N, m;map<string, int> mp;struct Edge{int    U, v; Double W;} E[max];bool CMP (Edge A, Edge b) {return A.W < B.W;} void Uf_set () {for (int i = 0; i < MAX; i++) fa[i] = i;} int Find (int x) {return x = = Fa[x]? x:fa[x] = Find (Fa[x]);}    void Union (int a, int b) {int r1 = Find (a);    int r2 = Find (b); if (r1! = r2) fa[r2] =r1;}    Double Kruskal () {uf_set ();    int num = 0;    Double res = 0;        for (int i = 0; i < m; i++) {int u = e[i].u;        int v = E[I].V;            if (Find (u)! = Find (v)) {Union (U, v);            Res + = E[I].W;        num + +;    } if (num >= n-1) break; } return res;    int main () {string S, S1, S2;    Double Val, sum, ans = 0;    scanf ("%lf", &sum);    int cnt = 1; Mp.clear ();    scanf ("%d", &n);        for (int i = 0; i < n; i++) {cin >> S;    if (!mp[s]) mp[s] = cnt + +;    } scanf ("%d", &m);        for (int i = 0; i < m; i++) {cin >> s1 >> S2 >> val;        E[I].U = Mp[s1];                E[I].V = Mp[s2];    E[I].W = val;    } sort (E, E + M, CMP);    ans = Kruskal ();    if (ans < sum) printf ("Need%.1f miles of cable\n", ans); else printf ("Not Enough cable\n");}



POJ 2075 Tangled in cables (Kruskal algorithm MST + map)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.