Graph theory --- Minimum Spanning Tree + pruning + path merge

Source: Internet
Author: User
Connect the cities

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 9941 accepted submission (s): 2827


Problem descriptionin 2100, since the sea level rise, most of the cities disappear. though some have ved cities are still connected with others, but most of them become disconnected. the government wants to build some roads to connect all of these cities again, but they don't want to take too much money.

 

Inputthe first line contains the number of test cases.
Each test case starts with three integers: n, m and K. N (3 <= n <= 500) stands for the number of attached ved cities, M (0 <= m <= 25000) stands for the number of roads you can choose to connect the cities and K (0 <= k <= 100) stands for the number of still connected cities.
To make it easy, the cities are signed from 1 to n.
Then follow M lines, each contains three integers p, q and C (0 <= C <= 1000), means it takes C to connect p and q.
Then follow K lines, each line starts with an integer T (2 <= T <= N) stands for the number of this connected cities. then T integers follow stands for the ID of these cities.

 

Outputfor each case, output the least money you need to take, if it's impossible, just output-1.

 

Sample input16 4 31 4 22 6 12 3 53 4 332 1 22 1 33 4 5 6

 

 

Mean:

I haven't asked questions for a long time...

Here is a directed graph. Some of the edges are connected. Now you need to find a minimal spanning tree.

 

Analyze:

First, merge connected edges with the query set, and then use Kruskal to find the Minimum Spanning Tree. Note that there is a pruning here. Because there are many edges in this question, we do not need to judge all of them, according to the nature of the Minimum Spanning Tree, we only need to find n-1 edges.

 

Time Complexity:O (N)

 

Source code:

 

//Memory   Time// 5644K   241MS//by : Snarl_jsb#include<algorithm>#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<vector>#include<queue>#include<stack>#include<iomanip>#include<string>#include<climits>#include<cmath>#define MAXV 520#define MAXE 25010#define LL long longusing namespace std;int T,n,m,k;struct Node{    int s,e,val;} graph[MAXE];int father[MAXV];void scan(int& x){        x = 0;        char c = getchar ();        while (!(c>=‘0‘ && c<=‘9‘ || c==‘-‘))     c = getchar ();        while (c >= ‘0‘ && c <= ‘9‘){                x = x * 10 + c - ‘0‘;                c = getchar ();        }}int Find(int x){    return x==father[x]?x:father[x]=Find(father[x]);}bool cmp(Node a,Node b){    return a.val<b.val;}int kruskal(){    int ans=0;    int cnt=0;    sort(graph+1,graph+1+m,cmp);    for(int i=1;i<=n;i++)        if(father[i]==i)            cnt++;    for(int i=1;i<=m&&cnt>1;i++)    {        int x=Find(graph[i].s);        int y=Find(graph[i].e);        if(x!=y)        {            father[x]=y;            ans+=graph[i].val;            cnt--;        }    }    if(cnt==1)        return ans;    else return -1;}int main(){//    freopen("cin.txt","r",stdin);//    freopen("cout.txt","w",stdout);    scan(T);    while(T--)    {        scan(n);        scan(m);        scan(k);        for(int i=1;i<=m;i++)        {            scan(graph[i].s);            scan(graph[i].e);            scan(graph[i].val);        }        for(int i=0;i<=n;i++)            father[i]=i;        int cnt,fa,son;        while(k--)        {            scan(cnt);            scan(fa);            fa=Find(fa);            while(--cnt)            {                scan(son);                father[Find(son)]=fa;            }        }        int mincost=kruskal();        printf("%d\n",mincost);    }    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.