HDU 3371 Connect the Cities

Description

In 2100, since the sea level rise, most of the cities disappear. Though some survived 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.  
 

Input

The 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 survived 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.
 

Output

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

Sample Input

     
     
     
     
1 6 4 3 1 4 2 2 6 1 2 3 5 3 4 33 2 1 2 2 1 3 3 4 5 6
 

Sample Output

     
     
     
     
1
这个题的意思是给你N个城,其中有些是连通的,有些则不然。问你要使所有路都能连通,最小要花多少钱,如果不能连通输出-1;
思路:先把题目中给定连通的城市用并查连起来,然后贪心,从花费最小的开始连接,用sum记录一共连接了多少个城市;
language:c++
code:
#include<iostream>
#include<cstdio>
#include<queue>

using namespace std;

int father[505],rank[505];

struct node
{
    int u,v,w;
    node(int x,int y,int z):u(x),v(y),w(z) {};
    bool operator <(const node a)const
    {
        return w>a.w;
    }
};

void set(int x)
{
    father[x]=x;
    rank[x]=0;
}

int find(int x)
{
    if(x!=father[x])
        father[x]=find(father[x]);
    return father[x];
}

bool merge(int x,int y)
{
    int u=find(x);
    int v=find(y);
    if(u!=v)
    {
        if(rank[u]>rank[v])
        {
            father[v]=u;
        }
        else
        {
            if(rank[u]==rank[v])
            {
                rank[v]++;
            }
            father[u]=v;
        }
        return true;
    }
    return false;
}

void init()
{
    for(int i=0; i<501; i++)
        set(i);
}

void connect(bool a[],int num,int&sum)
{
    if(!a[num])
    {
        a[num]=true;
        sum++;
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
    int cas;
    scanf("%d",&cas);
    int n,m,k;
    int u,v,w;
    int ans,sum;
    int arr[504];
    while(cas--)
    {
        ans=0,init(),sum=0;
        bool isConnected[504]= {false};
        priority_queue<node>prique;
        scanf("%d%d%d",&n,&m,&k);
        for(int i=0; i<m; i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            prique.push(node(u,v,w));
        }

        for(int i=0; i<k; i++)
        {
            int t;
            scanf("%d",&t);
            for(int j=0; j<t; j++)
            {
                scanf("%d",arr+j);
                if(j>0)
                {
                    merge(arr[j],arr[j-1]);
                    connect(isConnected,arr[j],sum);
                    connect(isConnected,arr[j-1],sum);
                }
            }
        }
        while(!prique.empty())
        {
            if(merge(prique.top().u,prique.top().v))
                ans+=prique.top().w;
            connect(isConnected,prique.top().u,sum);
            connect(isConnected,prique.top().v,sum);
            prique.pop();
        }
        if(sum==n)
            printf("%d\n",ans);
        else
            puts("-1");
    }
    return 0;
}


你可能感兴趣的:(HDU 3371 Connect the Cities)