POJ:1751-Highways(Kruskal和Prim)】的更多相关文章

题目链接:http://poj.org/problem?id=1751 题意是给你n个点的坐标,然后给你m对点是已经相连的,问你还需要连接哪几对点,使这个图为最小生成树. 这里用kruskal不会超时,用prim应该会超时,特别注意在输入的时候不要多组输入,否则会TLE. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vec…
思路: 一开始用Kruskal超时了,因为这是一个稠密图,边的数量最惨可能N^2,改用Prim. Prim是这样的,先选一个点(这里选1)作为集合A的起始元素,然后其他点为集合B的元素,我们要做的就是每次找到B中的一个点,满足这个点到A的权值是B到A的权值中最小的,然后我们把这个点加入到A,再更新B中的点到A的最小距离. 代码: #include<cstdio> #include<cmath> #include<cstring> #include<iostream…
Highways Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1751 Appoint description:  System Crawler  (2015-06-02) Description The island nation of Flatopia is perfectly flat. Unfortunately, Flato…
Highways Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23070   Accepted: 6760   Special Judge Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian…
题目链接:点击打开链接 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has already constructed a number of highways connecting…
Highways 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/G Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem an…
http://poj.org/problem?id=1751 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2048 题目大意: 给你n个点的坐标,并且m个点之间已经建立起连接,让你输出剩下的MST的连接点. 两题其实一样.就输入一点点不同. 因为题目采用Special Judge故水题一题. POJ: #include<cstdio> #include<cmath> #include<algorit…
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has already constructed a number of highways connecting some of the…
Highways POJ-1751 最小生成树 Prim算法 题意 有一个N个城市M条路的无向图,给你N个城市的坐标,然后现在该无向图已经有M条边了,问你还需要添加总长为多少的边能使得该无向图连通.输出需要添加边的两端点编号即可. 解题思路 这个可以使用最短路里面的Prim算法来实现,对于已经连接的城市,处理方式是令这两个城市之间的距离等于0即可. prim算法可以实现我们具体的路径输出,Kruskal算法暂时还不大会. 代码实现 #include<cstdio> #include<cs…
模版题为[poj 1287]Networking. 题意我就不说了,我就想简单讲一下Kruskal和Prim算法.卡Kruskal的题似乎几乎为0.(●-`o´-)ノ 假设有一个N个点的连通图,有M条边(不定向),求MST(Minimal Spanning Tree)最小生成树的值. 1.Kruskal 克鲁斯卡算法 概述:将边从小到大排序,依次将边两端的不在同一个联通分量/联盟的点分别加入一个个联盟内,将边也纳入,计入答案.最终N个点合并为一个联盟,也就是纳入联盟内的边达到N-1条就结束算法.…
最小生成树——Kruskal与Prim算法 序: 首先: 啥是最小生成树??? 咳咳... 如图: 在一个有n个点的无向连通图中,选取n-1条边使得这个图变成一棵树.这就叫“生成树”.(如下图) 每个无向连通图都会拥有至少一个生成树. 而在无向连通图中,我们让每一个边都拥有一个边权(就是每个边代表一个值). 而我们在有边权的无向连通图中构造一个生成树,使得这个生成树所用的边的边权之和最小.这个生成树就叫这个无向连通图的最小生成树! 上图这个最小生成树的边权之和为9,是所有生成树中边权之和最小的.…
链接: http://poj.org/problem?id=1751 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11507   Accepted: 3279   Special Judge Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of publ…
某个地方政府想修建一些高速公路使他们每个乡镇都可以相同通达,不过以前已经修建过一些公路,现在要实现所有的联通,所花费的最小代价是多少?(也就是最小的修建长度),输出的是需要修的路,不过如果不需要修建就什么都不输出. 分析:构建一个完全图,使用krusal进行一些简单优化不知道可以不,试一下吧 已经T成狗了 下面是TLE的krusal代码 ****************************************************************************** #…
Highways Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6078 Accepted: 1650 Special Judge Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian govern…
题目链接: 传送门 Agri-Net Time Limit: 1000MS     Memory Limit: 10000K Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course. Farmer…
Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23033   Accepted: 10612 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Fl…
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways so that i…
对于终于生成的最小生成树中最长边所连接的两点来说 不存在更短的边使得该两点以不论什么方式联通 对于本题来说 最小生成树中的最长边的边长就是使整个图联通的最长边的边长 由此可知仅仅要对给出城市所抽象出的图做一次最小生成树 去树上的最长边就可以 #include<bits/stdc++.h> using namespace std; int T,n,a,dist[1020],m[1020][1020]; void prim() { bool p[1020]; for(int i=2;i<=n…
题目链接:https://vjudge.net/problem/POJ-1751 思路: 最小生成树板子,只需要多记录每个dis[x]的权值是从哪个点到x这个点的. #include <stdio.h> #include <iostream> #include <queue> #include <math.h> #include <algorithm> using namespace std; ; const int inf = (int)1e9…
POJ 1258 Agri-Net http://poj.org/problem?id=1258 水题. 题目就是让你求MST,连矩阵都给你了. prim版 #include<cstdio> const int MAXN=101; const int INF=100000+10; int map[MAXN][MAXN]; int dis[MAXN]; int n; void prim() { bool vis[MAXN]={0}; for(int i=0;i<=n;i++) dis[i]…
题目连接 http://poj.org/problem?id=2485 Highways Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They…
链接:poj 2485 题意:输入n个城镇相互之间的距离,输出将n个城镇连通费用最小的方案中修的最长的路的长度 这个也是最小生成树的题,仅仅只是要求的不是最小价值,而是最小生成树中的最大权值.仅仅须要加个推断 比較最小生成树每条边的大小即可 kruskal算法 #include<cstdio> #include<algorithm> using namespace std; int f[510],n,m; struct stu { int a,b,c; }t[20100]; int…
题目链接 Description The islandnation of Flatopia is perfectly flat. Unfortunately, Flatopia has no publichighways. So the traffic is difficult in Flatopia. The Flatopian government isaware of this problem. They're planning to build some highways so that…
题目链接:http://poj.org/problem?id=3026 题目大意:在一个y行 x列的迷宫中,有可行走的通路空格’  ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的路径L连接所有字母,输出这条路径L的总长度. 解题思路:相当于所有的字母A和S都是结点,求连接这些结点的最小距离和,是最小生成树的题目.先用BFS求各点间的距离,然后再用Prim(Kruskal也可以)求出最小距离就可以了. 注意:输完行列m和n之后,后面有一堆空格,要用gets()去掉,题目…
链接: http://poj.org/problem?id=2485 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#problem/H Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18668   Accepted: 8648 Description The island nation of Flatopia is perfect…
部分内容摘自 勿在浮沙筑高台 http://blog.csdn.net/luoshixian099/article/details/51908175 关于图的几个概念定义: 连通图:在无向图中,若任意两个顶点vi与vj都有路径相通,则称该无向图为连通图. 强连通图:在有向图中,若任意两个顶点vi与vj都有路径相通,则称该有向图为强连通图. 连通网:在连通图中,若图的边具有一定的意义,每一条边都对应着一个数,称为权:权代表着连接连个顶点的代价,称这种连通图叫做连通网. 生成树:一个连通图的生成树是…
题目链接:Highways 没看题,看了输入输出.就有种似曾相识的感觉,果然和HDU1102 题相似度99%,可是也遇到一坑 cin输入居然TLE,cin的缓存不至于这么狠吧,题目非常水.矩阵已经告诉你了.就敲个模板就是了,5分钟.1A 题意就是打印,最小生成树的最大边权.改了改输入,水过 这个题完了.我的个人POJ计划进度以完毕 20/200,这当中主要是图论的题.等下周把POJ计划图论的题目打完,就回头打模拟!我就不信还能比图论难 #include <iostream> #include…
Kruskal: #include<iostream> #include<cstdio> #include<algorithm> using namespace std; //#define debug #if defined debug #define CDBG(format,...) printf("File: "__FILE__", Line: %05d: "format"\n", __LINE__, #…
1.HDU  1102  Constructing Roads    最小生成树 2.总结: 题意:修路,裸题 (1)kruskal //kruskal #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio> #define max(a,b) a>b?a:b using na…
D - Jungle Roads Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1251 Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extr…