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 ...
随机推荐
- 【LGP5437】【XR-2】约定
题目 显然每一条边出现在生成树中的方案数是相等的 根据矩阵树定理,\(n\)个节点的完全图生成树个数是\(n^{n-2}\)种,完全图共有\(\frac{n(n-1)}{2}\)条边,一棵生成树共\( ...
- 滑雪 矩阵中的最长上升路径 /// 记忆化DFS || DP oj22919
大致题意: Description 难怪Michael喜欢滑雪,因为滑雪确实很刺激.为了获得加速度,滑雪道必须向下倾斜,而且当滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道在一 ...
- SpringCloud学习笔记《---04 Hystrix---》基础篇
- 2018-2-13-安装visualStudio-出现-cant-install-Microsoft.TeamFoundation.OfficeIntegration.Resources...
title author date CreateTime categories 安装visualStudio 出现 cant install Microsoft.TeamFoundation.Offi ...
- SPOJ10707 COT2-Count on a tree II
COT2 - Count on a tree II 中文题意 离线询问一颗树上路径(u,v)中经过所有点的权值的种类数. 题解 树上莫队.即在树的欧拉序列上进行莫队.同一个点加第一次时增加,第二次时减 ...
- openSUSE 安装compass,mkmf.rb can't find,checking for ffi.h...extconf.rb failed
安装compass时,提示 Fetching: sass-.gem (%) Successfully installed sass- Fetching: ffi-.gem (%) Building n ...
- poj3294Life Forms
传送门 我真是一个垃圾 模板题都不会做 模板题还要别人教 细节写法还要别人教 别人一分钟AC,教我算法还教我写法,最后写出来的别人算法还比我优秀一百倍 数据结构把脑子学傻了,看到题就想怎么用数据结构, ...
- Netty设置高低水位
Configure high and low write watermarks Server ServerBootstrap bootstrap = new ServerBootstrap(); ...
- 《DSP using MATLAB》Problem 8.14
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- <每日一题>题目7:简单的学生管理系统V1.0
''' # 学生管理系统v1.0 # 添加学生的信息 # 删除学生的信息 # 修改学生的信息 # 查看学生的信息 #遍历学生的信息 #退出系统 ''' import json #1 显示操作功能 de ...