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 ...
随机推荐
- springmvc最简单的搭建,初学者必看
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...
- Android 面试题集 包含答案
作者:guoxiaoxing 链接: https://github.com/guoxiaoxing/android-interview 本文基于作者采用的MIT协议分发. 手画一下Android系统架 ...
- 【BZOJ】【3170】【TJOI2103】松鼠聚会
切比雪夫距离+曼哈顿距离 题解:http://www.cnblogs.com/zyfzyf/p/4105456.html 其实应该先做这题再做[BZOJ][3210]花神的浇花集会的吧…… 我们发现d ...
- Cocos2d-x3.3RC0通过JNI调用Android的Java层URI代码发送短信
1.Jni不在赘述.翻看前面博客 2.直接上代码 1)Java层,直接加在AppActivity.java中 public class AppActivity extends Cocos2dxActi ...
- javascript简单性能问题及学习笔记
最近在看一本书:<高性能javaScript>,发现自己平时写js存在很多小细节上的问题,虽然这些问题不会导致程序运行出错,但是会导致界面加载变慢,用户体验变差,那么我们就来细细数一下应该 ...
- smtp发送带附件的邮件(直接将string类型结果保存为附件)
该方式直接保存为HTML文件,也可以是文本文件,其它格式效果不是很好 MailMessage mmsg = new MailMessage(); mmsg.Subject = " ...
- css样式记录
样式一 #sideBar,#blog_post_info_block { display: none; } #under_post_news { display: none; } /*评论框大小*/ ...
- hdu1874 畅通project续(求最短路径)
畅通project续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- PCL系列——怎样逐渐地配准一对点云
欢迎訪问 博客新址 PCL系列 PCL系列--读入PCD格式文件操作 PCL系列--将点云数据写入PCD格式文件 PCL系列--拼接两个点云 PCL系列--从深度图像(RangeImage)中提取NA ...
- GLSL语言内置的变量详解
GLSL语言内置的变量,包括内置的顶点属性(attribute).一致变量(uniform).易变变量(varying)以及常量(const),一方面加深印象,另一方面今天的文章可以为以后的编程做查询 ...