hdu1863畅通工程
畅通工程
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 39468 Accepted Submission(s): 17635
Problem Description
省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本。现请你编写程序,计算出全省畅通需要的最低成本。
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<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
const int maxN = 105;
//kruskal
struct node
{
int u;
int v;
int w;
}edges[maxN * maxN];
bool cmp2(const node &a, const node &b)
{
return a.w < b.w;
}
int father[maxN];
int Rank[maxN];
void Init(int n)//结点的数量
{
for(int i = 1; i <= n; i++)
{
father[i] = i;
Rank[i] = 1;
}
}
int Find(int x)
{
if(x != father[x])
father[x] = Find(father[x]);
return father[x];
}
void Union(int x, int y)
{
x = Find(x);
y = Find(y);
if(Rank[x] < Rank[y])
father[x] = y;
else
{
if(Rank[x] == Rank[y])
Rank[x]++;
father[y] = x;
}
}
void Kruskal(int n, int m)
{
Init(n);
int sum = 0;
int cnt = 0;
for(int i = 0; i < m; i++)
{
if(Find(edges[i].u) != Find(edges[i].v))
{
Union(edges[i].u, edges[i].v);
cnt++;
sum += edges[i].w;
}
}
if(cnt != n - 1)
cout << "?" << endl;
else
cout << sum << endl;
}
int main()
{
int m, n;//way, node
int u, v, w;
while(cin >> m >> n && m)
{
int cnt = 0;
for(int i = 0; i < m; i++)
{
cin >> u >> v >> w;
edges[cnt].u = u;
edges[cnt].v = v;
edges[cnt++].w = w;
}
sort(edges, edges + cnt, cmp2);
Kruskal(n, m);
}
return 0;
}
hdu1863畅通工程的更多相关文章
- 最小生成树算法 prim kruskal两种算法实现 HDU-1863 畅通工程
最小生成树 通俗解释:一个连通图,可将这个连通图删减任意条边,仍然保持连通图的状态并且所有边权值加起来的总和使其达到最小.这就是最小生成树 可以参考下图,便于理解 原来的图: 最小生成树(蓝色线): ...
- hdu1863 畅通工程(最小生成树之prim)
Problem Description 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出了有可 ...
- Kruskal算法-HDU1863畅通工程
链接 [http://acm.hdu.edu.cn/showproblem.php?pid=1863] 题意 Problem Description 省政府"畅通工程"的目标是使全 ...
- HDU1863 畅通工程 2017-04-12 19:25 59人阅读 评论(0) 收藏
畅通工程 Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submissi ...
- HDU1863 畅通工程---(最小生成树)
畅通工程 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU1863畅通工程---并查集+最小生成树
#include<cstdio> #include<algorithm> #define MAX 105 struct edge { int from,to; long lon ...
- hdu1863 畅通工程---MST&连通
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1863 题目大意: 中文题,求MST权值,如果不连通,输出? 解题思路: 这道题帮我找出了之前模板中的 ...
- hdu1863 畅通工程 基础最小生成树
#include <iostream> #include <cstdio> #include <algorithm> #define N 110 #define M ...
- 畅通工程[HDU1863]
畅通工程 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissio ...
随机推荐
- 6_5.springboot2.x数据整合springData JPA
1.配置文件 pom.xml <dependencies> <dependency> <groupId>org.springframework.boot</g ...
- 论文阅读-(ECCV 2018) Second-order Democratic Aggregation
本文是Tsung-Yu Lin大神所作(B-CNN一作),主要是探究了一种无序的池化方法\(\gamma\) -democratic aggregators,可以最小化干扰信息或者对二阶特征的内容均等 ...
- C++单纯的指针传参的问题
C++指针传参也是单纯的复制一份地址,如下代码: #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace st ...
- (转)linux centos 编译luabind-0.9.1 动态库 静态库
编译时:virtual memory exhausted: Cannot allocate memory 一.问题 当安装虚拟机时系统时没有设置swap大小或设置内存太小,编译程序会出现virtual ...
- mysql主从复制linux配置(二进制日志文件)
安装mysql,两台机器一主(192.168.131.153),一从(192.168.131.154) 主机配置 修改主/etc/my.cnf文件 添加 #server_id=153 ###服务器id ...
- loj6244 七选五
题意:从n个数中选k个数,问有多少种排列与标准k项串恰好有x个位置相同. 标程: #include<cstdio> using namespace std; typedef long lo ...
- Tensorflow入门篇
参考Tensorflow中文网(http://www.tensorfly.cn/tfdoc/get_started/introduction.html) ,写一个入门. 1.打开pyCharm,新建 ...
- csps模拟69chess,array,70木板,打扫卫生题解
题面:https://www.cnblogs.com/Juve/articles/11663898.html 69: 本以为T2傻逼题结果爆零了...T3原题虽然打的不是正解复杂度但是都不记得做过这道 ...
- ireport 无法打开问题
打开时闪退 ,是因为jdk版本过高的原因:https://blog.csdn.net/aust_glj/article/details/52291240 相关软件下载地址: JasperReports ...
- 05-1-操作css样式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...