Problem D hdu 3371 Connect the Cities

Connect the Cities

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5956    Accepted Submission(s): 1733


Problem 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
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3371
题意:有n座城市,有m条可修的路,已有k组城市相互可达,求通过修路能否使图全部连通,若能则求总费用,不能输出-1。
感想:这题也是蛮裸的最小生成树 不过有一个位置比较坑 就是用G++ 或者GUN C++ 提交都需要优化输入才能过 不然会TML(因为输入量很大)
所以我写了一个getint()函数(其实这是我们老大 博博学长 教了我们的) 用字符来读取整数 优化输入 可以直接由TML到314ms
因为题目比较裸 所以就不用贴思路了 就是kruskul算法 若不懂的话就去看一下以前的ppt
下面贴我的代码:
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

int set[510],sum,num,rank[510];   // 用并查集的知识  
// set[]-存集合  num-存set[]当前有几个不相交的集合 
// sum-存修路的花费  rank[]-存集合的深度(用于优化路径)
struct Tnode
{
    int v1,v2,cost;
} node[25005];   // node[]结构用来存图 以及权值(两城市之间的花费)

void init(int n)  // 并查集中初始化函数
{
    int i;
    for(i=1; i<=n; i++)
    {
        set[i]=i;
        rank[i]=1;
    }
    sum=0;
    num=n;
}
int find(int x)   // 并查集中带优化路径的寻找父节点的函数
{
    int r=x,n;
    while(set[r]!=r) r=set[r];
    while(x!=r)
    {
        n=set[x];
        set[x]=r;
        x=n;
    }
    return r;
}
void merge(int a,int b)   // 并查集中将两集合合并的函数
{
    int x,y;
    x=find(a);
    y=find(b);
    if(x!=y)
    {
        if(rank[x]<rank[y])  set[x]=y;
        else
        {
            set[y]=x;
            if(rank[x]==rank[y])  rank[x]++;
        }
        num--;
    }
}
bool cmp(const Tnode &a, const Tnode &b)
{
    return  a.cost<b.cost;
}
int  getint()       //读取整数函数  可以优化输入  节约时间
{
    char c;
    int t=0;
    c=getchar();
    while(c<'0'||c>'9')  c=getchar();
    while(c>='0'&&c<='9')
    {
        t=10*t+c-'0';
        c=getchar();
    }
    return t;
}
int main()
{
    int n,i,x1,x2,m,k,a,b,cnt,t,j;
    t=getint();
    while(t--)
    {
        n=getint();
        m=getint();
        k=getint();
        init(n);
        for(i=1; i<=m; i++)     // 用node[]来存图
        {
            node[i].v1=getint();
            node[i].v2=getint();
            node[i].cost=getint();
        }
        for(i=1; i<=k; i++)     // 已经连接起来的城市将其合并
        {
            cnt=getint();
            a=getint();
            for(j=1; j<cnt; j++)
            {
                b=getint();
                merge(a,b);
            }
        }
        sort(node+1,node+m+1,cmp);   // 将边按权值从小到大排序
        for(i=1; num>1&&i<=m; i++)   // 每次取最小的边 如果能加入到最小生成树中就加入 不能则进行下一条边
        {         // 判断循环退出条件可以加一个num>1 因为num=1 说明只剩下一个集合了 即已经满足条件了
            x1=find(node[i].v1);
            x2=find(node[i].v2);
            if(x1!=x2)         // 如果两个点属于不同集合 才将其合并
            {
                merge(node[i].v1,node[i].v2);
                sum+=node[i].cost;    // 合并时更新花费
            }
        }
        if(num==1)  printf("%d\n",sum);    // num=1 才满足条件
        else printf("-1\n");
    }
    return 0;
}


你可能感兴趣的:(Problem D hdu 3371 Connect the Cities)