HDUOJ---3371Connect the Cities
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
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.
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
代码:
#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的更多相关文章
- Connect the Cities[HDU3371]
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- hduoj 1455 && uva 243 E - Sticks
http://acm.hdu.edu.cn/showproblem.php?pid=1455 http://uva.onlinejudge.org/index.php?option=com_onlin ...
- codeforces 613D:Kingdom and its Cities
Description Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. Ho ...
- CF449B Jzzhu and Cities (最短路)
CF449B CF450D http://codeforces.com/contest/450/problem/D http://codeforces.com/contest/449/problem/ ...
- hdu 2874 Connections between cities [LCA] (lca->rmq)
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- Connect the Cities(MST prim)
Connect the Cities Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑
这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...
- PAT 解题报告 1013. Battle Over Cities (25)
1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...
- 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 ...
- 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 ...
随机推荐
- restful处理
重写/覆盖 HTTP 方法 一些HTTP客户端仅能处理简单的的GET和POST请求,为照顾这些功能有限的客户端,API需要一种方式来重写HTTP方法. 尽管没有一些硬性标准来做这事,但流行的惯例是 ...
- [leetcode]Convert Sorted Array to Binary Search Tree @ Python
原题地址:http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题意:将一个排序好的数组转换为一颗二叉 ...
- Android数据适配-ExpandableListView
Android中ListView的用法基本上学的时候都会使用,其中可以使用ArrayAdapter,SimpleAdapter,BaseAdapter去实现,这次主要使用的ExpandableList ...
- Unable to find manifest signing certificate in the certificate store
方法一:把DEF项目的属性->Signing选项->Sign the ClickOnce manifests 勾去掉,这样就可以编绎通过了: 方法二:用记事本打开 *.csproj文件 , ...
- [asp.net入门]利用ADO.NET处理数据的简单之处
由于项目需要,要往数据库中导入一些历史数据,而这些历史数据都是线下人工记录的,所以有很多不规范的地方,比如:同一个公司的名称在不同的记录中可能相差那么几个字,而且每条数据不是每个字段都是完整的,等等诸 ...
- Java基础(二):基本数据类型和变量类型
一.java基本数据类型: 变量就是申请内存来存储值.也就是说,当创建变量的时候,需要在内存中申请空间.内存管理系统根据变量的类型为变量分配存储空间,分配的空间只能用来储存该类型数据. Java 的两 ...
- Android中操作SQLite数据库
我又回到了安卓的学习当中,忙来忙去终于忙的差不多有时间做自己的事情了,这感觉实在是太棒了!!本来想写android的控件以及他们的监视器的,但是我查了查android的手册,基本上都能查到,但是查有些 ...
- POJ 1836-Alignment(DP/LIS变形)
Alignment Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 13465 Accepted: 4336 Descri ...
- 在陌生Linux环境查看Tomcat服务的方法
1.查看Tomcat进程 执行命令$ps -ef|grep tomcat 你就能找出tomcat占据的进程号,当然这要求tomcat启动了. # ps -ef | grep tomcatroot ...
- docker入门——构建镜像
前面我们已经介绍了如何拉取已经构建好的带有定制内容的Docker镜像,那么如何构建自己的镜像呢? 构建Docker镜像有以下两种方法: 使用docker commit命令. 使用docker buil ...