hdoj-3371-Connect the Cities【最小生成树】
Connect the Cities
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13299 Accepted Submission(s): 3618
again, but they don’t want to take too much money.
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.
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
1
pid=1301" target="_blank">
1301
pid=1874" target="_blank">
1874
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct graph{
int a;
int b;
int cost;
}G[ 25005];
int root[505];
int n,m;
int find(int i){
if(root[i]==i) return i;
return root[i]=find(root[i]);
}
void unio(int i,int j){
int t=find(i);
int k=find(j);
if(t<=k) root[k]=t;
else root[t]=k;
return ;
}
int cmp(graph u,graph v){
return u.cost<v.cost;
}
int shortest;
void kruskal(){
sort(G,G+m,cmp);
int i,t,k;
shortest=0;
for(i=0;i<m;++i){
t=find(G[i].a); k=find(G[i].b);
if(t!=k){
unio(t,k);
shortest+=G[i].cost;
}
}
return ;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
int k,i,j,p,q,c,t,x,y;
scanf("%d%d%d",&n,&m,&k);
for(i=1;i<=n;++i) root[i] = i;
for(i=0;i<m;++i){
scanf("%d%d%d",&p,&q,&c);
G[i].a=p,G[i].b=q,G[i].cost=c;
}
for(i=1;i<=k;++i){
scanf("%d",&t);
scanf("%d%d",&x,&y);
unio(x,y);
for(j=3;j<=t;++j){
scanf("%d",&y);
unio(x,y);
} }
kruskal();
int nu=0;
for(i=1;i<=n;++i){
if(find(i)==i) nu++;
}
if(nu>1) printf("-1\n");
else printf("%d\n",shortest); }
return 0;
}
hdoj-3371-Connect the Cities【最小生成树】的更多相关文章
- hdu 3371 Connect the Cities(最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...
- hdoj 3371 Connect the Cities
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- POJ:3371 Connect the Cities(最小生成树)
http://acm.hdu.edu.cn/showproblem.php?pid=3371 AC代码: /** /*@author Victor /* C++ */ #include <bit ...
- hdu 3371 Connect the Cities (最小生成树Prim)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...
- HDU 3371 Connect the Cities 最小生成树(和关于sort和qsort的一些小发现)
解题报告:有n个点,然后有m条可以添加的边,然后有一个k输入,表示一开始已经有k个集合的点,每个集合的点表示现在已经是连通的了. 还是用并查集加克鲁斯卡尔.只是在输入已经连通的集合的时候,通过并查集将 ...
- hdu 3371 Connect the Cities
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Description In 2100, since th ...
- hdu oj 3371 Connect the Cities (最小生成树)
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 3371 Connect the Cities(prim算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3371 Problem Description In 2100, since the sea leve ...
- Hdu 3371 Connect the Cities(最小生成树)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 其实就是最小生成树,但是这其中有值得注意的地方:就是重边.题目没有告诉你两个城市之间只有一条路可走, ...
- HDU 3371 Connect the Cities(并查集+Kruskal)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先 ...
随机推荐
- shell基础学习-难点重点学习
来自shell13问 -e : 啟用反斜線控制字符的轉換(參考下表) -E:關閉反斜線控制字符的轉換(預設如此) -n : 取消行末之換行符號(與 -e 選項下的 \c 字符同意) 要取消一個变量,在 ...
- 关于get_magic_quotes_gpc()函数
function sqlReplace($str) { $strResult = $str; if(!get_magic_quotes_gpc()) //如果 gpc 没有开的话 { $strResu ...
- LeetCode(9)Palindrome Number
题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...
- python基础学习笔记——列表技巧
列表: 循环删除列表中的每⼀个元素 li = [, , , ] for e in li: li.remove(e) print(li) 结果: [, ] 分析原因: for的运⾏过程. 会有⼀个指针来 ...
- 深入浅出Oracle:DBA入门、进阶与诊断案例(读书笔记1)
一.数据库的启动和关闭 Oracle Server共有2部分组成:Instance和Database. Instance是指一组后台进程/线程和一块共享内存区域,而Database是指存储在磁盘上的一 ...
- Leetcode 363.矩形区域不超过k的最大数值和
矩形区域不超过k的最大数值和 给定一个非空二维矩阵 matrix 和一个整数 k,找到这个矩阵内部不大于 k 的最大矩形和. 示例: 输入: matrix = [[1,0,1],[0,-2,3]], ...
- Non-maximum suppression(非极大值抑制算法)
在RCNN系列目标检测中,有一个重要的算法,用于消除一些冗余的bounding box,这就是non-maximum suppression算法. 这里有一篇博客写的挺好的: http://www.c ...
- [BZOJ1574] [Usaco2009 Jan]地震损坏Damage(贪心 + dfs)
传送门 告诉你一些点不能到达1,由于是双向边,也就是1不能到达那些点 那么从1开始dfs,如果当前点能到达不能到达的点,那么当前点就是损坏的. #include <cstdio> #inc ...
- POJ 1006 生理周期【数论】
这题是有中文版的(右上角选项卡里把default改成简体中文)然后看到他把biorhythms翻成生理周期我可耻的笑了......23333 如果没有限定从日期d开始,完全可以从第一天起开始计时,因此 ...
- Ionic1与Ionic2
1.Ionic2新特性 ①组织结构与框架: 在Ionic2中,每个组件.页面都只专注于做一件事,它单独有自己的一个目录,有自己的类(Class).模板文件(Template)和自己的样式文件(在这里我 ...