Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44827   Accepted: 18351 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 nee…
用的是prim算法. 我用vector数组,每次求最小的dis时,不需要遍历所有的点,只需要遍历之前加入到vector数组中的点(即dis[v]!=INF的点).但其实时间也差不多,和遍历所有的点的方法都是16ms... #include <iostream> #include <algorithm> #include <string.h> #include <stdio.h> #include <string> #include <que…
题目: Out of Hay Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: 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 wh…
题目链接:https://vjudge.net/contest/241341#problem/D 这里有多个发电站,需要求出所有点都和发电站直接或间接相连的最小代价,那么就是求出最小生成树的问题了,有点细微的不同是我们一开始要把所有的发电站看成起点(看成一个点),把其他点到发电站的最小代价求出,之后就是模板题了. 代码: #include<iostream> #include<cstring> using namespace std; const int inf=0x3f3f3f;…
#include<stdio.h> #include<string.h> using namespace std; int map[505][505]; int v, e; int prime() { bool vis[505]; int dist[505]; int i,j,sum=0; for(i=1;i<=v;i++) { vis[i]=0; //先假设编号为1的点加入MST dist[i]=map[1][i]; } vis[1]=1; for(i=1;i<v;i…
题目链接: 传送门 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…
Agri-Net 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/H 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.…
Networking Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7321   Accepted: 3977 Description You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of po…
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1233 可以用Kruskal来做,不过当图的边比较稠密的时候用prime会更快一些. AC代码:296MS #include <iostream> #include <cstring> #include <cstdio> using namespace std; const int inf=0xffffff; int dis[105],sum,n; int weight[105]…
题目: http://acm.hdu.edu.cn/showproblem.php?pid=1863 注意有可能出现无法生成树的情况. #include <iostream> #include <cstring> using namespace std; const int inf=0xffffff; int weight[105][105],n,m; int prime() { int sum=0; int pre[105]; int dis[105]; bool visit[1…
题目 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> using namespace std; #define M 110 #define inf 999999999 int mat[M][M]; int prim(int n,int sta) { int sum=0.0,dis[M]; int…
链接: 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…
poj 1251  && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E 44E 2 F 60 G 38F 0G 1 H 35H 1 I 353A 2 B 10 C 40B 1 C 200Sample Output 21630 prim算法 # include <iostream> # include <cstdio> # include <cstr…
1.Kruskal算法 图的存贮采用边集数组或邻接矩阵,权值相等的边在数组中排列次序可任意,边较多的不很实用,浪费时间,适合稀疏图.      方法:将图中边按其权值由小到大的次序顺序选取,若选边后不形成回路,则保留作为一条边,若形成回路则除去.依次选够(n-1)条边,即得最小生成树.(n为顶点数). Kruskal算法在图G=(V,E)上的运行时间取决于分离集合这一数据结构如何实现.采用在分离集合中描述的按行结合和通路压缩的启发式方法来实现分离集合森林的结构,这是从渐近意义上说,目前最快实现法…
首先说一下什么是树: 1.只含一个根节点 2.任意两个节点之间只能有一条或者没有线相连 3.任意两个节点之间都可以通过别的节点间接相连 4.除了根节点没一个节点都只有唯一的一个父节点 5.也有可能是空树(不含任何节点) 最小生成树就是: 在所有数据满足是一棵树的情况下一条将所有节点都连接起来且长度最短的一条路(因为任意两个节点之间有权值 (相连的两点之间权值为一个具体的数,不相连的两个点之间权值为无穷大)) 下面介绍通用的求最小生成树的两种算法: ps:这里用的两种算法都是用邻接矩阵实现适合点稠…
连着做了四道畅通工程的题,其实都是一个套路,转化为可以求最小生成树的形式求最小生成树即可 这道题需要注意: 1:因为满足路的长度在10到1000之间才能建路,所以不满足条件的路径长度可以初始化为无穷 2:在求最小生成树的算法中(我用的prime算法)做一次过滤,找距离某个点的最短路径的时候,如果这个路径长度大于1000,那么就没有答案,prime算法课直接返回即可 Description 相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来…
还是畅通project Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 26860    Accepted Submission(s): 11985 Problem Description 某省调查乡村交通状况,得到的统计表中列出了随意两村庄间的距离.省政府"畅通project"的目标是使全省不论什么两个村庄间都能够实现公路交…
一.主要内容: 介绍图论中两大经典问题:最小生成树问题以及最短路径问题,以及给出解决每个问题的两种不同算法. 其中最小生成树问题可参考以下题目: 题目1012:畅通工程 http://ac.jobdu.com/problem.php?pid=1012 题目1017:还是畅通工程 http://ac.jobdu.com/problem.php?pid=1017 题目1024:畅通工程 http://ac.jobdu.com/problem.php?pid=1024 题目1028:继续畅通工程 ht…
经典的最小生成树例子,Prime算法,具体的步骤及其注释本人均在代码中附加,请仔细阅读与品味,要求,可以熟练的打出. //Prime算法基础 #include<iostream> using namespace std; int main() { int n,m,i,j,k,min,t1,t2,t3; ][],dis[],book[] = {}; ; ,sum = ; cin >> n >> m; //初始化 用邻接矩阵存储 ;i <= n;i++) ;j <…
Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21518   Accepted: 8367 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for brick…
#include <iostream>// poj 1258 10.1.5.253 1505 using namespace std; #define N 105 // 顶点的最大个数 (多写 int a[N][N],low[N],n,ans; int min(int x,int y) { return x<y?x:y; } void prim(int u0) { int i,j,m,k; ans=0; // for (i=1;i<n;i++) low[i]=a[u0][i]; /…
poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23507   Accepted: 11012 Description The Head Elder of the tropical island of Lagrishan has a problem. A b…
前面在介绍并查集时顺便提了Kruskal算法,既然已经说到了最小生成树问题,就没有道理不把Prime算法说了. 这里面先补充下Kruskal算法的大概意思,Kruskal算法通过把所有的边从小到大排列后,不断取权值最小的边加入最小生成树(起初可能是离散的多个树,最终连成一个整体),并通过并查集来舍弃形成回路的边. Prime算法有所不同,Prime算法先将一个起点加入最小生成树,之后不断寻找与最小生成树相连的边权最小的边能通向的点,并将其加入最小生成树,是一种更符合人的主观直觉的最小生成树算法.…
思路:一开始想用贪心来着,发现贪心有缺陷,然后就用了最小生成树来写,这里用了prime算法,首先,先建个图,两点之间的边的权值就是两个点的距离,然后直接prime模板 代码 #include<iostream> #include<algorithm> #include<cstring> #include<math.h> #include<cstdio> const int inf=0x7fffffff; using namespace std;…
1.prime算法 prime算法类似于bfs,就是判断每次连接的点中距离最短的,加入到树中,具体如下: prime算法要求一开始随便选择一个点作为起点,因为最小生成树包括所有点,所以起点随机即可(一般选1),将该点加入一个集合,然后判断集合中所有点与之相连的点中最小的,将其加入集合中,加入集合的点都要用一个vis数组判断是否重复出现过,如果重复出现,就说明你要连接的这两个点已经是连通的了,不需要再直接连接. 比如:  图中三条边,分别为1,2,3,从1开始,1的连接的边两条<1,3>,<…
题意:给你一个矩阵M[i][j]表示i到j的距离 求最小生成树 思路:裸最小生成树 prime就可以了 最小生成树专题 AC代码: #include "iostream" #include "string.h" #include "stack" #include "queue" #include "string" #include "vector" #include "set&…
存图方式 最小生成树prime+队列优化 优化后时间复杂度是O(m*lgm) m为边数 优化后简直神速,应该说对于绝大多数的题目来说都够用了 具体有多快呢 请参照这篇博客:堆排序 Heapsort ///prime队列优化 #include "iostream" #include "string.h" #include "stack" #include "queue" #include "string" #…
//归并排序递归方法实现 #include <iostream> #include <cstdio> using namespace std; #define maxn 1000005 int a[maxn], temp[maxn]; long long ans; void MergeSort(int a[], int l, int mid, int r) { ; int i = l, n = mid, j = mid, m = r; while ( i<n &&am…
POJ 2739 Sum of Consecutive Prime Numbers Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu   Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representati…
最小生成树,普利姆算法. 简述算法: 先初始化一棵只有一个顶点的树,以这一顶点开始,找到它的最小权值,将这条边上的令一个顶点添加到树中 再从这棵树中的所有顶点中找到一个最小权值(而且权值的另一顶点不属于这棵树) 重复上一步.直到所有顶点并入树中. 图示: 注:以a点开始,最小权值为1,另一顶点是c,将c加入到最小生成树中.树中 a-c 在最小生成树中的顶点找到一个权值最小且另一顶点不在树中的,最小权值是4,另一个顶点是f,将f并入树中, a-c-f 重复上一步骤,a-c-f-d, a-c-f-d…