POJ - 1287超级模板题 大概意思就是点的编号从1到N,会给你m条边,可能两个点之间有多条边这种情况,求最小生成树总长度? 这题就不解释了,总结就算,prim是类似dijkstra,从第一个点出发,每次走这个点没走过的最小边权值,这样不断找下去就可以找出,本质就是贪心算法 而kruskal是利用并查集,先按照边权值大小排序,然后从小的边开始往里面添加边,利用并查集判断是否在一个联通分量里面(就是是否相连)如果不相 连就建立边,从而建图,注意,节点编号如果是从1->n,那么相应初始化就应该从…
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…
Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 45050   Accepted: 18479 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…
最小生成树概念: 一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的最少的边. 最小生成树可以用kruskal(克鲁斯卡尔)算法或prim(普里姆)算法求出.最小生成树其实是最小权重生成树的简称. prim: 普里姆算法(Prim算法),图论中的一种算法,可在加权连通图里搜索最小生成树.意即由此算法搜索到的边子集所构成的树中,不但包括了连通图里的所有顶点(英语:Vertex (graph theory)),且其所有边的权值之和亦为最小. p…
Economic times these days are tough, even in Byteland. To reduce the operating costs, the government of Byteland has decided to optimize the road lighting. Till now every road was illuminated all night long, which costs 1 Bytelandian Dollar per meter…
题目:点这里 题意:给一个长度n的数列,然后又Q个询问,问L   到R   中最大值与最小值的差. 分析:RMQ 的模板题. 代码: #include<stdio.h> #include<iostream> #include<algorithm> #include<string.h> using namespace std; ; int minm,maxm; ],dp_min[max_][]; int a[max_]; void RMQ_init(int n…
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 own code describing each type of a truck. The code is simply a string of ex…
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 possible routes for the cables that may connect pairs of points. For each possible route between…
题目链接:http://poj.org/problem?id=1789 大意: 不同字符串相同位置上不同字符的数目和是它们之间的差距.求衍生出全部字符串的最小差距. #include<stdio.h> #include<math.h> #include<algorithm> using namespace std; ; int cnt; int pre[MAXN]; ]; struct Edge { int from, to; int val; }edge[MAXN *…
http://poj.org/problem?id=1258 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 John ordered a high speed conne…
链接: http://poj.org/problem?id=1287 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7494   Accepted: 4090 Description You are assigned to design network connections between certain points in a wide area. You are given a set of points in t…
题目描述 如题,给出一个无向图,求出最小生成树,如果该图不连通,则输出orz 输入格式 第一行包含两个整数N.M,表示该图共有N个结点和M条无向边.(N<=5000,M<=200000) 接下来M行每行包含三个整数Xi.Yi.Zi,表示有一条长度为Zi的无向边连接结点Xi.Yi 输出格式 输出包含一个数,即最小生成树的各边的长度之和:如果该图不连通则输出orz 输入输出样例 输入 #1复制 4 5 1 2 2 1 3 2 1 4 3 2 3 4 3 4 3 输出 #1复制 7 说明/提示 时空…
Relatives Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11372   Accepted: 5544 Description Given n, a positive integer, how many positive integers less than n are relatively prime to n? Two integers a and b are relatively prime if ther…
http://acm.hdu.edu.cn/showproblem.php?pid=1879 刚开始么看清题  以为就是n行  后来一看是n*(n-1)/2行   是输入错误  真是够够的 #include <iostream> #include <cstring> #include <algorithm> #include <queue> #include <cstdio> #include <cstdlib> #include &…
问题 H: Vegetable and Road again 时间限制: 1 Sec 内存限制: 128 MB 提交: 19 解决: 8 题目描述 修路的方案终于确定了.市政府要求任意两个公园之间都必须实现公路交通(并不一定有直接公路连接,间接公路相连也可以).但是考虑到经济成本,市政府希望钱花的越少越好. 你能帮助Vegetable找到给出的修路方案所需的最少花费吗? 输入 有T组测试数据. 每组包含一组N(0<n<=100)和M,N表示有N个公园,M表示这N个公园间的M条路. 接下来给出M…
#include <cstdio> #include <cstring> ],fr[]; int st; struct Tire{ ]; ]; }node[]; void insert(char *s,int cur) { if(*s){ if(!node[cur].next[*s-'a']) node[cur].next[*s-'a']=++st; insert(s+,node[cur].next[*s-'a']); } else strcpy(node[cur].eng,en)…
还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 71235    Accepted Submission(s): 32197 Problem Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路…
最小生成树模板题 跑一次kruskal就可以了 /* *********************************************** Author :Sun Yuefeng Created Time :2016/11/9 18:26:37 File Name :tree.cpp ************************************************ */ #include<cstdio> #include<iostream> #includ…
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 John ordered a high speed connection for his farm and is going to share his…
题目链接>>> 题目大意: 给出n个城市,接下来n行每一行对应该城市所能连接的城市的个数,城市的编号以及花费,现在求能连通整个城市所需要的最小花费. 解题分析: 最小生成树模板题,下面用的是kruscal算法. //Kruscal算法采用的是"加边"的想法 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using…
传送门:http://poj.org/problem?id=1287 题意:给出n个点 m条边 ,求最小生成树的权 思路:最小生树的模板题,直接跑一遍kruskal即可 代码: #include<iostream> #include<cstdio> #include<algorithm> #include<string.h> using namespace std; const int maxn = 5005; struct node { int u; in…
题目链接>>> 题目大意:     给你N*N矩阵,表示N个村庄之间的距离.FJ要把N个村庄全都连接起来,求连接的最短距离(即求最小生成树).解析如下: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define inf 0x3f3f3f3f int n; ][]; ], vis[]; /…
POJ 最小生成树模板 Kruskal算法 #include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> #include<ctype.h> #include<stdlib.h> #include<limits.h> #include<math.h> #include<queue> #include<st…
最近都是图,为了防止几次记不住,先把自己理解的写下来,有问题继续改.先把算法过程记下来: prime算法:                  原始的加权连通图——————D被选作起点,选与之相连的权值最小的边              选与D.A相连权值最小的边——————可选的有B(7).E(8).G(11)                   ————————————————————————重复上述步骤,最小生成树 代码: 用maze[M][M]存两点间的长度,vis[M]判断是否使用此边,…
题目链接:problemCode=1372">ZOJ1372 POJ 1287 Networking 网络设计 Networking Time Limit: 2 Seconds      Memory Limit: 65536 KB You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, a…
最小生成树概念: 一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的最少的边. 最小生成树可以用kruskal(克鲁斯卡尔)算法或prim(普里姆)算法求出.最小生成树其实是最小权重生成树的简称. prim: 概念:普里姆算法(Prim算法),图论中的一种算法,可在加权连通图里搜索最小生成树.意即由此算法搜索到的边子集所构成的树中,不但包括了连通图里的所有顶点,且其所有边的权值之和亦为最小. 实现过程: 图例 说明 不可选 可选 已选(Vn…
Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V', E'), with the following properties…
Networking Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1287 Appoint description:  System Crawler  (2015-06-02) Description You are assigned to design network connections between certain poin…
layout: post title: 最小生成树 Prim Kruskal date: 2017-04-29 tag: 数据结构和算法 --- 目录 TOC {:toc} 最小生成树Minimum Spanning Tree 一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的最少的边. 树: 无回路,|V|个顶点,一定有|V|-1条边 生成树: 包含全部顶点,|V|-1 条边都在图里:边权重和最小 最小生成树存在<--->图联通:向生成…
题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes…