HDU1863 畅通工程 2017-04-12 19:25 59人阅读 评论(0) 收藏
畅通工程
Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 65 Accepted Submission(s) : 40
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Input
行对应村庄间道路的成本,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间道路的成本(也是正整数)。为简单起见,村庄从1到M编号。当N为0时,全部输入结束,相应的结果不要输出。
Output
Sample Input
3 3
1 2 1
1 3 2
2 3 4
1 3
2 3 2
0 100
Sample Output
3
?
Source
浙大计算机研究生复试上机考试-2007年
————————————————————————————————————
给出n个城市间的信息,求最小生成树
#include<iostream>
#include<cstdio>
#include<queue>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std; int m,n,pa[105];
struct node
{
int u,v,c;
} s[100005]; bool cmp(node a,node b)
{
return a.c<b.c;
} void init()
{
for(int i=0; i<105; i++)
pa[i]=i;
} int fin(int x)
{
if(pa[x]!=x)
{
pa[x]=fin(pa[x]);
}
return pa[x];
} void un(int a,int b)
{
int r1=fin(a);
int r2=fin(b);
if(r1!=r2)
{
pa[r1]=pa[r2];
}
} void kruskal()
{
init();
int sum=0;
int num=0;
int flag=0;
for(int i=0; i<m; i++)
{
if(fin(s[i].u)!=fin(s[i].v))
{
un(s[i].u,s[i].v);
num++;
sum+=s[i].c;
if(num>=n-1)
{
flag=1;
break;
}
}
}
if(flag)
printf("%d\n",sum);
else
printf("?\n");
} int main()
{
int u,v,c;
while(scanf("%d%d",&m,&n)&&m)
{
for(int i=0; i<m; i++)
{
scanf("%d%d%d",&u,&v,&c);
s[i].u=u;
s[i].v=v;
s[i].c=c;
}
sort(s,s+m,cmp);
kruskal();
}
return 0;
}
HDU1863 畅通工程 2017-04-12 19:25 59人阅读 评论(0) 收藏的更多相关文章
- 公钥,私钥和数字签名这样最好理解 分类: B3_LINUX 2015-05-06 16:25 59人阅读 评论(0) 收藏
一.公钥加密 假设一下,我找了两个数字,一个是1,一个是2.我喜欢2这个数字,就保留起来,不告诉你们(私钥),然后我告诉大家,1是我的公钥. 我有一个文件,不能让别人看,我就用1加密了.别人找到了这个 ...
- HDU1875 畅通工程再续 2017-04-12 19:52 48人阅读 评论(0) 收藏
畅通工程再续 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submis ...
- HDU1233 还是畅通工程 2017-04-12 19:49 64人阅读 评论(0) 收藏
还是畅通工程 Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- HDU1232 畅通工程 2017-04-12 19:20 53人阅读 评论(0) 收藏
畅通工程 Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submissi ...
- 滑雪 分类: POJ 2015-07-23 19:48 9人阅读 评论(0) 收藏
滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 83276 Accepted: 31159 Description Mich ...
- HDU6029 Happy Necklace 2017-05-07 19:11 45人阅读 评论(0) 收藏
Happy Necklace Time Limit: ...
- hdu 1053 (huffman coding, greedy algorithm, std::partition, std::priority_queue ) 分类: hdoj 2015-06-18 19:11 22人阅读 评论(0) 收藏
huffman coding, greedy algorithm. std::priority_queue, std::partition, when i use the three commente ...
- Network Saboteur 分类: 搜索 POJ 2015-08-09 19:48 7人阅读 评论(0) 收藏
Network Saboteur Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10147 Accepted: 4849 Des ...
- Python获取当前时间 分类: python 2014-11-08 19:02 132人阅读 评论(0) 收藏
Python有专门的time模块可以供调用. <span style="font-size:14px;">import time print time.time()&l ...
随机推荐
- An Introduction to Greta
I was surprised by greta. I had assumed that the tensorflow and reticulate packages would eventually ...
- C# 如何捕获一个USB设备发送到PC的数据
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- 转转转![Spring MVC] - 500/404错误处理-SimpleMappingExceptionResolver
参考博客: http://www.cnblogs.com/dongying/p/6129937.html http://www.cnblogs.com/rollenholt/archive/2012/ ...
- 《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #9 RT Group Scheduling 与RT Throttling
HACK #9 RT Group Scheduling 与RT Throttling 本节介绍对实时进程所使用的CPU时间进行限制的功能RT Group Scheduling和RT Throttlin ...
- 浅析JavaScript中的typeof运算符
对JavaScript中的typeof运算符进行了详细的分析介绍,需要的朋友可以过来参考下,希望对大家有所帮助. 如果typeof的运算符是数字.字符串或者布尔值,它返回的结果就是"numb ...
- JDK常用命令
转自:https://www.cnblogs.com/saiQsai/p/10353044.html 1.jps 查看java进程,得到进程ID:7854 作用等同于:ps -ef | grep ja ...
- 从LinqPad入门Linq
为什么要学习Linq? 在我们日常开发中,经常需要从数据库中执行各式各样的查询来获取需要的数据.但是如果需要对查询获得的数据进行二次筛选呢?linq就提供了对于可枚举类型(实现IEnumerable接 ...
- Maven(九)”编码 gbk 的不可映射字符“ 问题解决方案
解决这个问题的思路: 在maven的编译插件中声明正确的字符集编码编码——编译使用的字符集编码与代码文件使用的字符集编码一致!! 安装系统之后,一般中文系统默认字符集是GBK.我们安装的软件一般都继承 ...
- XP系统下 VS2010 选中行崩溃
- SVN代理设置
[SVN代理设置] ~/.subversion/servers文件下可以配置svn的代理. 打开上面那3行,替换为自定义的域名即可.