Connect the Cities

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

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

代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxn 25005
typedef struct Side
{
int vs;
int ve;
int cost;
}side;
side sta[maxn];
int father[],rank[];
void init(int n)
{
for(int i= ;i<n ; i++)
{
father[i]=i;
rank[i]=;
}
} int setfind(int x)
{ if(x!=father[x])
father[x]=setfind(father[x]);
return father[x]; //这里不能用x
}
void krusal(int a,int b)
{
int x=setfind(a);
int y=setfind(b);
if(x==y) return ;
if(x<y)
{
father[y]=x;
rank[x]+=rank[y];
}
else
{
father[x]=y;
rank[y]+=rank[x];
}
}
int cmp(const void* a , const void* b)
{
return (*(side*)a).cost-(*(side*)b).cost;
}
int main()
{
int test,n,m,k,i,ans;
scanf("%d",&test);
while(test--)
{
ans=;
scanf("%d%d%d",&n,&m,&k);
init(n);
for(i=; i<m ;i++)
{
scanf("%d%d%d",&sta[i].vs,&sta[i].ve,&sta[i].cost);
}
int a,b,c;
for(i=;i<k ;i++)
{
scanf("%d",&c);
scanf("%d",&a);
while(c-- >)
{
scanf("%d",&b);
krusal(a,b);
}
}
qsort(sta,m,sizeof(sta[]),cmp);
for(i=; i<m ;i++)
{
int tem1=setfind(sta[i].vs);
int tem2=setfind(sta[i].ve);
if(tem1 != tem2)
{
krusal(tem1,tem2);
ans+=sta[i].cost;
}
} if(rank[]==n)
printf("%d\n",ans);
else
printf("-1\n");
}
return ;
}

HDUOJ---3371Connect the Cities的更多相关文章

  1. Connect the Cities[HDU3371]

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...

  2. hduoj 1455 && uva 243 E - Sticks

    http://acm.hdu.edu.cn/showproblem.php?pid=1455 http://uva.onlinejudge.org/index.php?option=com_onlin ...

  3. codeforces 613D:Kingdom and its Cities

    Description Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. Ho ...

  4. CF449B Jzzhu and Cities (最短路)

    CF449B CF450D http://codeforces.com/contest/450/problem/D http://codeforces.com/contest/449/problem/ ...

  5. hdu 2874 Connections between cities [LCA] (lca->rmq)

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  6. Connect the Cities(MST prim)

    Connect the Cities Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  7. HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑

    这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...

  8. PAT 解题报告 1013. Battle Over Cities (25)

    1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...

  9. hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...

  10. hduoj 4706 Herding 2013 ACM/ICPC Asia Regional Online —— Warmup

    hduoj 4706 Children's Day 2013 ACM/ICPC Asia Regional Online —— Warmup Herding Time Limit: 2000/1000 ...

随机推荐

  1. restful处理

    重写/覆盖   HTTP 方法 一些HTTP客户端仅能处理简单的的GET和POST请求,为照顾这些功能有限的客户端,API需要一种方式来重写HTTP方法. 尽管没有一些硬性标准来做这事,但流行的惯例是 ...

  2. [leetcode]Convert Sorted Array to Binary Search Tree @ Python

    原题地址:http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题意:将一个排序好的数组转换为一颗二叉 ...

  3. Android数据适配-ExpandableListView

    Android中ListView的用法基本上学的时候都会使用,其中可以使用ArrayAdapter,SimpleAdapter,BaseAdapter去实现,这次主要使用的ExpandableList ...

  4. Unable to find manifest signing certificate in the certificate store

    方法一:把DEF项目的属性->Signing选项->Sign the ClickOnce manifests 勾去掉,这样就可以编绎通过了: 方法二:用记事本打开 *.csproj文件 , ...

  5. [asp.net入门]利用ADO.NET处理数据的简单之处

    由于项目需要,要往数据库中导入一些历史数据,而这些历史数据都是线下人工记录的,所以有很多不规范的地方,比如:同一个公司的名称在不同的记录中可能相差那么几个字,而且每条数据不是每个字段都是完整的,等等诸 ...

  6. Java基础(二):基本数据类型和变量类型

    一.java基本数据类型: 变量就是申请内存来存储值.也就是说,当创建变量的时候,需要在内存中申请空间.内存管理系统根据变量的类型为变量分配存储空间,分配的空间只能用来储存该类型数据. Java 的两 ...

  7. Android中操作SQLite数据库

    我又回到了安卓的学习当中,忙来忙去终于忙的差不多有时间做自己的事情了,这感觉实在是太棒了!!本来想写android的控件以及他们的监视器的,但是我查了查android的手册,基本上都能查到,但是查有些 ...

  8. POJ 1836-Alignment(DP/LIS变形)

    Alignment Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13465   Accepted: 4336 Descri ...

  9. 在陌生Linux环境查看Tomcat服务的方法

    1.查看Tomcat进程 执行命令$ps -ef|grep tomcat 你就能找出tomcat占据的进程号,当然这要求tomcat启动了. # ps -ef | grep tomcatroot    ...

  10. docker入门——构建镜像

    前面我们已经介绍了如何拉取已经构建好的带有定制内容的Docker镜像,那么如何构建自己的镜像呢? 构建Docker镜像有以下两种方法: 使用docker commit命令. 使用docker buil ...