HDU1863(Kruskal+并查集水题)
https://cn.vjudge.net/problem/HDU-1863
Input测试输入包含若干测试用例。每个测试用例的第1行给出评估的道路条数 N、村庄数目M ( < 100 );随后的 N
行对应村庄间道路的成本,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间道路的成本(也是正整数)。为简单起见,村庄从1到M编号。当N为0时,全部输入结束,相应的结果不要输出。
Output对每个测试用例,在1行里输出全省畅通需要的最低成本。若统计数据不足以保证畅通,则输出“?”。
Sample Input
3 3
1 2 1
1 3 2
2 3 4
1 3
2 3 2
0 100
Sample Output
3
?
#include<bits/stdc++.h>
#define maxn 110
using namespace std;
int n,m,tot;
int parent[maxn];
int ans;
struct edge
{
int u,v,w;
}EV[];
bool cmp(edge a,edge b)
{
return a.w<b.w;
}
int Find(int x)
{
if(parent[x]==-)
return x;
else
return Find(parent[x]);
}
void kruskal()
{
memset(parent,-,sizeof parent);
sort(EV+,EV+m+,cmp);
for(int i=;i<=m;i++)
{
int t1=Find(EV[i].u);
int t2=Find(EV[i].v);
if(t1!=t2)
{
ans+=EV[i].w;
parent[t1]=t2;
tot++;
}
}
}
int main()
{
while(~scanf("%d%d",&m,&n)&&m)
{
for(int i=;i<=m;i++)
cin>>EV[i].u>>EV[i].v>>EV[i].w;
ans=;
tot=;
kruskal();
if(tot!=n-)
cout<<"?"<<endl;
else
cout<<ans<<endl;
}
return ;
}
HDU1863(Kruskal+并查集水题)的更多相关文章
- Brain Network (easy)(并查集水题)
G - Brain Network (easy) Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- 【PAT-并查集-水题】L2-007-家庭房产
L2-007. 家庭房产 给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(<=1000),随后N行,每行按下 ...
- poj2524(并查集水题)
题目链接:http://poj.org/problem?id=2524 题目大意:学校共有n个同学,告诉你m对同学信仰同一宗教,问这个学校学生信仰宗教的数目最多为多少. 例: Sample Input ...
- POJ2524并查集水题
Description There are so many different religions in the world today that it is difficult to keep tr ...
- PAT题解-1118. Birds in Forest (25)-(并查集模板题)
如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
- 【HDU1231】How Many Tables(并查集基础题)
什么也不用说,并查集裸题,直接盲敲即可. #include <iostream> #include <cstring> #include <cstdlib> #in ...
- poj1182 食物链(并查集 好题)
https://vjudge.net/problem/POJ-1182 并查集经典题 对于每只动物创建3个元素,x, x+N, x+2*N(分别表示x属于A类,B类和C类). 把两个元素放在一个组代表 ...
- 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 ...
随机推荐
- C#进行数据筛选(一)
这里介绍数据筛选的第一种方式,不用过滤器,给新手看得 public DataTable SourceList(string Wmain, string OrderNo, string Process) ...
- VMware打开虚拟机没反应的解决方案(全面汇总)
VMware打开虚拟机无反应的解决方案(全面汇总)虚拟机没反应的解决办法大概是如下几点:一.若是第一次安装后打不开虚拟机,大致是如下两种解决方案: 1.大多数时候,虚拟机打不开都是因为防火墙拦截所致 ...
- POJ1149(最大流)
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21678 Accepted: 9911 Description ...
- POJ1611(KB2-B)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 39211 Accepted: 18981 De ...
- java.lang.IllegalArgumentException: findUserById is ambiguous in Mapped Statements collection
这是由于mybatis的mapper xml文件中的id 名字和mybatis内置的方法可能有冲突导致的, 更改xml 的id 名字就可以了!
- js-ES6学习笔记-编程风格(2)
1.那些需要使用函数表达式的场合,尽量用箭头函数代替.因为这样更简洁,而且绑定了this. 2.所有配置项都应该集中在一个对象,放在最后一个参数,布尔值不可以直接作为参数. 3.不要在函数体内使用ar ...
- 上传文件Base64格式(React)
记录一下上传文件时将文件数据转为Base64的方法 通过 FileReader对象创建一个实例,然后使用 readAsDataURL方法将数据转为Base64格式 注意: 读取过程是异步的 绑定onl ...
- Python+Selenium笔记(五):生成测试报告
#HTMLTestRunner代码修改参考 微微微笑 的说明,下面是链接,这个已经说的很详细了 https://www.cnblogs.com/miniren/p/5301081.html (一) 前 ...
- 【three.js练习程序】创建简单物理场景
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- LeetCode题解之Largest Number
1.题目描述 2. 将整数值转换为string ,然后排序. 3.代码 string largestNumber(vector<int>& nums) { vector<s ...