POJ 2395 Out of Hay( 最小生成树 )】的更多相关文章

POJ 2395 Out of Hay 本题是要求最小生成树中的最大长度, 无向边,初始化es结构体时要加倍,别忘了init(n)并查集的初始化,同时要单独标记使用过的边数, 判断ans==n-1时,即找到了最大边. #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <queue> #include <vector&g…
Description The cows have run <= N <= ,) farms (numbered ..N); Bessie starts at Farm . She'll traverse some or all of the M (1 <= M <= 10,000) two-way roads whose length does not exceed 1,000,000,000 that connect the farms. Some farms may be m…
题意:求最小生成树中最大的一条边. 分析:求最小生成树,可用Prim和Kruskal算法.一般稀疏图用Kruskal比较适合,稠密图用Prim.由于Kruskal的思想是把非连通的N个顶点用最小的代价构成一个连通分量,这与并查集的思想类似,所以可以用并查集来实现Kruskal. import java.util.Scanner; /** * 稀疏图用Prim,21316K,3047MS 不划算. */ public class Poj_2395_Prim { static int n,m; st…
百度百科:瓶颈生成树 瓶颈生成树 :无向图G的一颗瓶颈生成树是这样的一颗生成树,它最大的边权值在G的所有生成树中是最小的.瓶颈生成树的值为T中最大权值边的权. 无向图的最小生成树一定是瓶颈生成树,但瓶颈生成树不一定是最小生成树.(最小瓶颈生成树==最小生成树) 命题:无向图的最小生成树一定是瓶颈生成树. 证明:可以采用反证法予以证明. 假设最小生成树不是瓶颈树,设最小生成树T的最大权边为e,则存在一棵瓶颈树Tb,其所有的边的权值小于w(e).删除T中的e,形成两棵数T', T'',用Tb中连接T…
Out of Hay Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18472   Accepted: 7318 Description The cows have run out of hay, a horrible event that must be remedied immediately. Bessie intends to visit the other farms to survey their hay s…
链接:传送门 题意:求最小生成树中的权值最大边 /************************************************************************* > File Name: poj2395.cpp > Author: WArobot > Blog: http://www.cnblogs.com/WArobot/ > Created Time: 2017年06月19日 星期一 19时00分25秒 *******************…
http://poj.org/problem?id=2377 bessie要为FJ的N个农场联网,给出M条联通的线路,每条线路需要花费C,因为意识到FJ不想付钱,所以bsssie想把工作做的很糟糕,她想要花费越多越好,并且任意两个农场都需要连通,并且不能存在环.后面两个条件保证最后的连通图是一棵树. 输出最小花费,如果没办法连通所有节点输出-1. 最大生成树问题,按边的权值从大道小排序即可,kruskal算法可以处理重边的情况,但是在处理的时候,不能仅仅因为两个节点在同一个连通子图就判断图不合法…
[题目链接]http://poj.org/problem?id=2395 [解题思路]找最小生成树中权值最大的那条边输出,模板过的,出现了几个问题,开的数据不够大导致运行错误,第一次用模板,理解得不够深厚且模板有错误导致提交错误   #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<algorithm&…
题目链接 Description The cows have run out of hay, a horrible event that must be remedied immediately. Bessie intends to visit the other farms to survey their hay situation. There are N (2 <= N <= 2,000) farms (numbered 1..N); Bessie starts at Farm 1. S…
题意:Bessie要从牧场1到达各大牧场去,他从不关心他要走多远,他只关心他的水袋够不够水,他可以在任意牧场补给水,问他走完各大牧场,最多的一次需要多少带多少单位的水? 思路:其实就是要让所带的水尽量少,即所选的每条路都要尽量短,即最小瓶颈生成树.其实也就是最小生成树.再求生成树上权值最大的边即可. //#include <bits/stdc++.h> #include <cstdio> #include <iostream> #include <algorith…
这个问题等价于求最小生成树中权值最大的边. #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<vector> #include<algorithm> using namespace std; ; int n,m; struct X { int u,v; long long val; } e[maxn*maxn]; int f[maxn…
题意:从待选的路里面选出若干将所有点连通,求选出的边里最长边的最小值. 算法:要使得树的最长边最小,那么每次确定的边都应是待选边里最小的,即最小生成树.对应Kruskal算法. #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <vector> using namespace std; int N, M; // 农场数,道路…
缺乏粮草 题目大意:一群牛要修建一些通道,到各个农场距离总和要最小,求这些通道的最大值 水题了,一个Kruskal搞定 #include <iostream> #include <functional> #include <algorithm> using namespace std; typedef int Postion; typedef struct _edge { Postion from; Postion to; int cost; }Edge_Set; in…
题目链接:http://poj.org/problem?id=2784 Buy or Build Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1528   Accepted: 592 Description World Wide Networks (WWN) is a leading company that operates large telecommunication networks. WWN would li…
题目链接:http://poj.org/problem?id=3026. Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to describe the group consciousness of the Borg civilization. Ea…
链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走).空格代表空位(可走).S代表搜索起点(可走),A代表目的地(可走),如今要从S出发,每次可上下左右移动一格到可走的地方.求到达全部的A的路线总距离最小值 分析:能够先用bfs从上下左右四个方向将全部的A,S两两之间的最短距离,题目的目的是将S与全部的A连通,使得总距离最小,所以任选一点開始按最小生成树的算法做即可,并不是非要从S点開始 注:题目输入x,y后可能有非常多空格,能够用gets将多余的空格取走,开数组是尽量开大点.…
链接: http://poj.org/problem?id=1789 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#problem/G Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14950   Accepted: 5714 Description Advanced Cargo Movement, Ltd. uses…
Out of Hay Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18780 Accepted: 7414 Description The cows have run out of hay, a horrible event that must be remedied immediately. Bessie intends to visit the other farms to survey their hay situa…
http://poj.org/problem?id=1679 题目大意: 给你一些点,判断MST(最小生成树)是否唯一. 思路: 以前做过这题,不过写的是O(n^3)的,今天学了一招O(n^2)的,哈哈~ 方法一: 首先先建立MST,然后把这个MST的边一个个尝试不使用,构建另外一颗MST,然后判断权值是否相等. 这样复杂度需要O(n^3).. 方法二: 还可以用次最小生成树的方法解决:如果最小生成树不唯一,那么次小生成树的权值和最小生成树相同. 我们可以枚举要加入哪一条新边.在最小生成树上加一…
题目链接:http://poj.org/problem?id=3026 题意:题意就是从起点开始可以分成多组总权值就是各组经过的路程,每次到达一个‘A'点可以继续分组,但是在路上不能分组 于是就是明显的最小生成树,点与点的距离要用bfs或者dfs求一下.这题bfs要稍微优化一下.不能下暴力的bfs就是两点两点之间 bfs这样会有很多重复的查询会超时,所以要一次性的bfs找到一个点就bfs到底. #include <iostream> #include <algorithm> #in…
题意:给定完全无向图,求其中m个子节点,要求Sum(edge)/Sum(node)最小. 思路:由于N很小,枚举所有可能的子节点可能情况,然后求MST,memset()在POJ G++里面需要cstring头文件. #include <iostream> #include <vector> #include <map> #include <cmath> #include <memory> #include <algorithm> #i…
Object Clustering   Description We have N (N ≤ 10000) objects, and wish to classify them into several groups by judgement of their resemblance. To simply the model, each object has 2 indexes a and b (a, b ≤ 500). The resemblance of object i and objec…
题目链接:http://poj.org/problem?id=1251 字符用%s好了,方便一点. #include <stdio.h> #include <string.h> #define INF 0x3f3f3f3f ][]; ]; ]; int n; int Prim() { memset(vis,false,sizeof(vis)); ; i<=n; i++) dis[i] = INF; ; dis[] = ; ; i<=n; i++) { ; ; j<…
The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22668   Accepted: 8038 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire…
题目 //最小生成树,只是变成二维的了 #define _CRT_SECURE_NO_WARNINGS #include<stdlib.h> #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> #include<math.h> using namespace std; #define inf 999999999 #define M 10…
题目 /*********题意解说——来自discuss——by sixshine**************/ 有卫星电台的城市之间可以任意联络.没有卫星电台的城市只能和距离小于等于D的城市联络.题目告诉你卫星电台的个数S,让你求最小的D. 做最小生成树,去掉最长的S条边后,剩下最长的边就是D. 也就是求最小生成树中第S+1长的边. 完毕. /********************************************************/ //听说听木看懂之后,数据很水,果然…
Borg Maze 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/I Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to describe the group consciou…
Arctic Network 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/F Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are to be use…
Truck History 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/E Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for bricks. The company has its…
Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two villa…