Connect the Cities

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

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
 
 
这道题我的思路是:先处理k个数,然后再用并查集求剩下没连通的。之前要排序,最后检查是否连通。。。。
 
╮(╯▽╰)╭  自己超时了好多次,还有就是,题意马虎了,又WA了几次,,,悲剧。
 
#include <iostream>
#include <cstdlib>
#include<algorithm>
using namespace std; int father[],Q; struct sum
{
int a;
int b;
int c;
}num[]; //路线数 bool cmp(const sum &x,const sum &y) //按长度从小到大快排,
{
return x.c<y.c;//原理有待研究
} int Find(int x) //找出祖先
{
while(x!=father[x])
x=father[x];
return x;
} void Union(int a,int b,int i)
{
if(a!=b)
{
father[a]=b;
Q+=num[i].c; //并入家族且把长度加上来
}
} int main()
{
int T,k,n,m,i,j,l,p,q,c,t;
int ss[];
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&k);
for(i=;i<=n;i++)
father[i]=i;
for(i=;i<m;i++)
scanf("%d%d%d",&num[i].a,&num[i].b,&num[i].c);
memset(ss,,sizeof(ss));
for(l=;l<k;l++)
{
scanf("%d",&t);
for(j=;j<t;j++)
scanf("%d",&ss[j]);
for(j=;j<t;j++)
{
if(Find(ss[])!=Find(ss[j]))
father[Find(ss[j])]=Find(ss[]);
}
memset(ss,,sizeof(ss));
}
sort(num,num+m,cmp);//排序
for(i=,Q=;i<m;i++)
{
Union(Find(num[i].a),Find(num[i].b),i);
}
for(i=,t=;t<&&i<=n;i++)
if(father[i]==i)
t++;
if(t==)
printf("-1\n");
else
printf("%d\n",Q);
}
return ;
}
/*
5
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
6 4 3
1 4 3
2 6 2
6 2 1
3 4 33
2 1 2
2 1 3
3 4 5 6
6 4 3
1 4 2
2 6 1
2 3 5
3 4 33
2 1 2
2 2 3
2 4 5
6 4 3
1 4 3
2 6 2
6 2 1
3 4 33
2 1 2
2 1 3
2 4 6*/

在网上找了Kruskal函数,   有待学习!

#include<stdio.h>           //Kruskal函数
#include<algorithm>
using namespace std; typedef struct{
int u;
int v;
int w;
}Edge; const int EdgeNum=;
const int PointNum=; Edge E[EdgeNum];
int P[PointNum]; int union_find(int x) // 并查集
{
return P[x]==x? x : P[x]=union_find(P[x]);
} bool cmp(Edge a,Edge b){ return a.w<b.w; } int MST_Kruskal(int n,int m) // 传入点数和边数
{
int i,j,x,y,k=,sum=;
for(i=;i<n;++i)
{
for(j=;j<n;++j)
{
if(i==j) continue;
if(P[j]==i)
{
k++;
// printf("P[%d]=%d\n",j,i);
}
}
}
// printf("k=%d\n",k);
sort(E,E+m,cmp);
for(i=;k<n&&i<m;++i)
{
x=union_find(E[i].u);
y=union_find(E[i].v);
if(x!=y)
{
sum+=E[i].w;
P[x]=y;
k++;
}
}
if(k<n) return -;
return sum;
} int f[];
int solve(int n,int m,int k)
{
int i,j,t;
for(i=;i<n;++i)
{ // 初始化并查集
P[i]=i;
}
for(i=;i<k;++i){
scanf("%d",&t);
for(j=;j<t;++j)
{
scanf("%d",&f[j]);
f[j]--;
}
for(j=;j<t;++j)
P[union_find(f[j])]=union_find(f[j-]);
}
return MST_Kruskal(n,m);
} int main()
{
int t,n,m,k,i,p,q,c,ans;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&k);
for(i=;i<m;++i)
{
scanf("%d%d%d",&p,&q,&c);
p--;q--;
E[i].u=p;E[i].v=q;E[i].w=c;
}
ans=solve(n,m,k);
printf("%d\n",ans);
}
return ;
}

Connect the Cities(hdu3371)并查集(附测试数据)的更多相关文章

  1. Connect the Cities[HDU3371]

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

  2. [hdu2874]Connections between cities(LCA+并查集)

    题意:n棵树,求任意两点的最短距离. 解题关键:并查集判断两点是否位于一棵树上,然后求最短距离即可.此题可以直接对全部区间直接进行st表,因为first数组会将连接的两点的区间表示出来. //#pra ...

  3. HDU 3371 Connect the Cities(并查集+Kruskal)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先 ...

  4. PAT 1013 Battle Over Cities(并查集)

    1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...

  5. TOJ 2815 Connect them (kruskal+并查集)

    描述 You have n computers numbered from 1 to n and you want to connect them to make a small local area ...

  6. hdu 2874 Connections between cities (并查集+LCA)

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

  7. 九度OJ 1325:Battle Over Cities(城市间的战争) (并查集)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:376 解决:132 题目描述: It is vitally important to have all the cities connect ...

  8. hdu-2874 Connections between cities(lca+tarjan+并查集)

    题目链接: Connections between cities Time Limit: 10000/5000 MS (Java/Others)     Memory Limit: 32768/327 ...

  9. 1013 Battle Over Cities (25分) DFS | 并查集

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

随机推荐

  1. 1.翻译:EF基础系列--什么是Entity Framework?

    大家好,好久不见,EF系列之前落下了,还是打算重新整理一下. 先说说目前的打算:先简单了解一下EF基础系列-->然后就是EF 6 Code-First系列-->接着就是EF 6 DB-Fi ...

  2. 关于WordCount的作业

    一.开发者:201631062418 二.代码地址:https://gitee.com/YsuLIyan/WordCount 三.作业地址:https://edu.cnblogs.com/campus ...

  3. [学习笔记]后缀自动机SAM

    好抽象啊,早上看了两个多小时才看懂,\(\%\%\%Fading\) 早就懂了 讲解就算了吧--可以去看看其他人的博客 1.[模板]后缀自动机 \(siz\) 为该串出现的次数,\(l\) 为子串长度 ...

  4. Spring Cloud断路器Hystrix

    在微服务架构中,存在着那么多的服务单元,若一个单元出现故障,就会因依赖关系形成故障蔓延,最终导致整个系统的瘫痪,这样的架构相较传统架构就更加的不稳定.为了解决这样的问题,因此产生了断路器模式. 什么是 ...

  5. 微信小程序实现给循环列表点击添加类(单项和多项)

    在微信小程序里面没有DOM对象, 不能操作DOM. 所有的操作通过数据来实现,下面主要实现了给循环列表点击添加类的操作 一.单项 目标需求:实现下图,给点击的view增加类,每次只能选择一个. 主要思 ...

  6. MVC3学习:将excel文件导入到sql server数据库

    思路: 1.将excel文件导入到服务器中. 2.读取excel文件,转换成dataset. 3.循环将dataset数据插入到数据库中. 本例子使用的表格为一个友情链接表F_Link(LinkId, ...

  7. Java代码操作HDFS测试类

    1.Java代码操作HDFS需要用到Jar包和Java类 Jar包: hadoop-common-2.6.0.jar和hadoop-hdfs-2.6.0.jar Java类: java.net.URL ...

  8. Zookeeper--0300--java操作Zookeeper,临时节点实现分布式锁原理

    删除Zookeeper的java客户端有  : 1,Zookeeper官方提供的原生API, 2,zkClient,在原生api上进行扩展的开源java客户端 3, 一.Zookeeper原生API ...

  9. mysql-定时对表分区

    1, 分区 具体可见: http://blog.csdn.net/open_data/article/details/46893331 1, 分区类型: RANGE分区:基于属于一个给定连续区间的列值 ...

  10. SpringMVC之类型转换

    在数据绑定上,SpringMVC提供了到各种基本类型的转换,由前端到后台时,SpringMVC将字符串参数自动转换为各种基本类型.而对于其他,则需要自己编写代码进行转换.本随笔以转换时间类型为例,使用 ...