URAL 1160 Network(最小生成树)
Network
Memory limit: 64 MB
Input
Output
Sample
| input | output |
|---|---|
4 6 |
1 |
题意:
给定几个需要链接的点以及能够利用的边,要求用这些变将所有的点都链接起来,且所用边长的最大值尽量小。
思路:
起初,看题意是最小生成树,但题目所给的样例,并不是最小生成树的结果,纠结半天,然后又看了几遍题目,还是不知所云,最后试着把模版敲上去,submit~~
然后便ac了,
后来问了才知道,这道题确实最小生成树,不过只要所用边的最大值不变,那些权值小的边可以任意加上去,因为题目只是对最大边最小有要求,对边的数量没有要求。
#include <stdio.h>
#include <string.h>
#include <cmath>
#include <iostream>
#include <stack>
#include <queue>
#include <algorithm>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = ;
const int M = ;
int n,m,k,s,t,tot,sum=,maxn=;
int head[N],vis[N],dis[N],father[N];
int dfn[N],low[N],stack1[N],num[N],in[N],out[N];
struct man{
int u,v,val,used;
}edg[M];
bool cmp(man f,man g){
return f.val<g.val;
}
int find(int x){
if(father[x]!=x)father[x]=find(father[x]);
return father[x];
}
void Union(int x,int y){
x=find(x);y=find(y);
if(x!=y)father[y]=x;
return;
}
void kruskal(){
for(int i=;i<=m;i++){
int u=edg[i].u,v=edg[i].v;
if(find(u)==find(v))continue;
Union(u,v);
sum++;edg[i].used=;
maxn=edg[i].val;
if(sum==n-)return;
}
}
int main() {
int u,v,val;tot=;met(dfn,);met(vis,);met(head,-);
for(int i=;i<N;i++)father[i]=i;
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){
scanf("%d%d%d",&u,&v,&val);
edg[i].u=u;edg[i].v=v;edg[i].val=val;edg[i].used=;
}
sort(edg+,edg+m+,cmp);
kruskal();
printf("%d\n%d\n",maxn,sum);
for(int i=;i<=m;i++){
if(edg[i].used){
printf("%d %d\n",edg[i].u,edg[i].v);
}
}
return ;
}
URAL 1160 Network(最小生成树)的更多相关文章
- 1160. Network(最小生成树)
1160 算是模版了 没什么限制 结束输出就行了 #include <iostream> #include<cstdio> #include<cstring> #i ...
- [poj2349]Arctic Network(最小生成树+贪心)
Arctic Network Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17758 Accepted: 5646 D ...
- BZOJ 3732: Network 最小生成树 倍增
3732: Network 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3732 Description 给你N个点的无向图 (1 &l ...
- POJ 2349 Arctic Network (最小生成树)
Arctic Network Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Subm ...
- ZOJ1586——QS Network(最小生成树)
QS Network DescriptionIn the planet w-503 of galaxy cgb, there is a kind of intelligent creature nam ...
- TZOJ 2415 Arctic Network(最小生成树第k小边)
描述 The Department of National Defence (DND) wishes to connect several northern outposts by a wireles ...
- ZOJ1586:QS Network (最小生成树)
QS Network 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1586 Description: In th ...
- poj2349 Arctic Network - 最小生成树
2017-08-04 16:19:13 writer:pprp 题意如下: Description The Department of National Defence (DND) wishes to ...
- POJ-1861,Network,最小生成树水题,,注意题面输出有问题,不必理会~~
Network Time Limit: 1000MS Memory Limit: 30000K Special Judge http://poj.org/problem?id=1 ...
随机推荐
- JSON.parse()和eval()区别
JSON.parse()只会将标准的Json字符串(key和value都由双引号引起来,最外面用单引号括住)转为JSON对象. eval()在转换字符串的时候是比较松的,即使不是标准的Json字符串也 ...
- Python开发入门与实战1-开发环境
1.搭建Python Django开发环境 1.1.Python运行环境安装 Python官网:http://www.python.org/ Python最新源码,二进制文档,新闻资讯等可以在Pyth ...
- 《java作业》
/* 2.编写一个类,该类有一个方法public int f(int a,int b), 该方法返回a和b的最大公约数.然后再编写一个该类的子类, 要求子类重写方法f,而且重写的方法将返回a和b的最小 ...
- iOS对象序列化
系统对象的归档我就不介绍了,这个不复杂,自己看一下就会了. 我在这里主要介绍自定义对象的归档. Sample.h文件 // // Sample.h // Serialization // // ...
- xml文件有误
Unable to start activity ComponentInfo{com.anzi.jmsht.scripturelibrary/com.anzi.jmsht.scripturelibra ...
- Emacs+highlight-parentheses高亮括号
EmacsWiki上关于它的介绍HighlightParentheses,下载最新版请通过作者的GitHub:https://github.com/nschum/highlight-parenthes ...
- c++回调函数 callback
C++中实现回调机制的几种方式 (1)Callback方式Callback的本质是设置一个函数指针进去,然后在需要需要触发某个事件时调用该方法, 比如Windows的窗口消息处理函数就是这种类型.比如 ...
- SQL基础2
create database fuxi --创建一个名为“fuxi”的数据库go --连接语句use fuxi --使用名为“fuxi”的数据库gocreat ...
- 【转】Entity Systems
“Favour composition over inheritance” If you haven’t already read my previous post on the problems o ...
- eval函数的工作原理
如果您想详细了解eval和JSON请参考以下链接: eval :https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Glob ...