Connect the Cities&&http://acm.hdu.edu.cn/showproblem.php?pid=3371
来源:互联网 发布:淘宝香港it代购真假 编辑:程序博客网 时间:2024/06/06 08:33
最小生成树变形题,不解释,这里输入量比较多,最好用一下输入外挂。。。
AC代码:
#include<iostream>#include<string.h>#include<cstdio>#include<string>#include<limits.h>#include<algorithm>#include<queue>#define N 80000#define M 505using namespace std;typedef struct{ int x; int y; int len;}Node;Node s[N];int res,Father[M];int n,m,k;bool cmp(Node a,Node b){return a.len<b.len;}void in(int &a){ char ch; while((ch=getchar())<'0'||ch>'9'); for(a=0;ch>='0'&&ch<='9';ch=getchar()) a=a*10+ch-'0';}void init(){ res=0; for(int i=1;i<=n;++i) Father[i]=i;}int Find(int x){ if(x==Father[x]) return x; return Father[x]=Find(Father[x]);}int main(){ int T; in(T); while(T--) { //scanf("%d%d%d",&n,&m,&k); in(n),in(m),in(k); init(); for(int i=0;i!=m;++i) { int a,b,c; //scanf("%d%d%d",&a,&b,&c); in(a),in(b),in(c); s[res].x=a,s[res].y=b,s[res++].len=c; } int sum=0,ans=0; bool flag=false; for(int i=0;i!=k;++i) { int t,a; //scanf("%d%d",&t,&a); in(t),in(a); for(int i=0;i!=t-1;++i) { int b; //scanf("%d",&b); in(b); if(!flag) { int x=Find(a); int y=Find(b); if(x!=y) {Father[y]=x;sum++;} } if(sum==n-1) flag=true; } } if(flag) printf("0\n"); else { sort(s,s+res,cmp); for(int i=0;i!=res;++i) { if(sum==n-1) break; int x=Find(s[i].x); int y=Find(s[i].y); if(x!=y) {Father[x]=y;sum++;ans+=s[i].len;} } if(sum==n-1) printf("%d\n",ans); else printf("-1\n"); } }return 0;}
- Connect the Cities&&http://acm.hdu.edu.cn/showproblem.php?pid=3371
- http://acm.hdu.edu.cn/showproblem.php?pid=3371
- http://acm.hdu.edu.cn/showproblem.php?pid=1005
- http://acm.hdu.edu.cn/showproblem.php?pid=1333
- http://acm.hdu.edu.cn/showproblem.php?pid=2710
- http://acm.hdu.edu.cn/showproblem.php?pid=2866
- http://acm.hdu.edu.cn/showproblem.php?pid=1041
- http://acm.hdu.edu.cn/showproblem.php?pid=1719
- http://acm.hdu.edu.cn/showproblem.php?pid=2136
- http://acm.hdu.edu.cn/showproblem.php?pid=1166
- http://acm.hdu.edu.cn/showproblem.php?pid=3519
- http://acm.hdu.edu.cn/showproblem.php?pid=2446
- http://acm.hdu.edu.cn/showproblem.php?pid=1709
- http://acm.hdu.edu.cn/showproblem.php?pid=1874
- http://acm.hdu.edu.cn/showproblem.php?pid=1863
- http://acm.hdu.edu.cn/showproblem.php?pid=1879
- http://acm.hdu.edu.cn/showproblem.php?pid=1875
- http://acm.hdu.edu.cn/showproblem.php?pid=1102
- android自动化测试辅助工具Weinre
- 一个轻量级的jQuery拖拽排序插件 - HTML5 Sortable
- Boost的转换函数polymorphic_cast和polymorphic_downcast
- Silverlight WCF RIA服务(十七)数据 7
- 关于JVM INSTR swap赋值
- Connect the Cities&&http://acm.hdu.edu.cn/showproblem.php?pid=3371
- Android Launcher抽屉类SlidingDrawer的使用
- poj1161 The Suspects
- spring入门十大问题解答
- ASP.NET和ASP相比的优缺点
- Silverlight WCF RIA服务(十八)共享代码 1
- Windows 7 64位搭建 PHP+apache+mysql环境
- Silverlight WCF RIA服务(十九)共享代码 2
- spring异常解决方法Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-