HDU 1301 Jungle Roads Kruskal Solution

Source: Internet
Author: User

Title Link: Http://acm.hust.edu.cn/vjudge/contest/view.action?cid=66965#problem/M

Test instructions: A country in the tropics or something, need to maintain the network, to ask for minimal maintenance costs, is almost the minimum spanning tree meaning.

Ideas: Kruskal solution, there is no bright spot, you can use the map to facilitate the character of the whole type. You can also subtract ' A ' from the character.


Code:

#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <map
> Using namespace std;
    struct edge{int u,v,w;
    BOOL operator< (const edge &a) const{return w<a.w;

}}e[900];
Map<char,int> mp;//the character integer int n,tot;

int pre[30];
    int find (int x) {int t=x;
    while (T!=pre[t]) t=pre[t];
    while (x!=t) pre[x]=t,x=pre[x];
return t;
    } void Kruskal () {for (int i=0;i<=26;i++) pre[i]=i;
    int sum=0,cnt=1;
        for (int i=0;i<tot;i++) {int u=e[i].u,v=e[i].v,w=e[i].w;
        int Fu=find (U), Fv=find (v);
        if (FU==FV) continue;
        pre[fu]=fv;sum+=w;cnt++;
    if (cnt==n) break;
} cout<<sum<<endl;
    } int main () {//freopen ("D:\\in.txt", "R", stdin);
    for (int i=0;i<26;i++) mp[' A ' +i]=i;
        while (Cin>>n && N) {char ch;
        int k,t;
        tot=0;
           for (int i=0;i<n-1;i++) {cin>>ch>>k; for (int j=0;j<k;j++) {cin>>ch>>t;
            E[tot++]= (Edge) {i,mp[ch],t};
        }} sort (E,e+tot);

    Kruskal ();
} return 0;
 }


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.