Poj(2421),Prim最小生成树】的更多相关文章

http://poj.org/problem?id=2421 这个题和poj1258是一样的,只要在1258的基础上那么几行代码,就可以A,水. 题意:还是n连通问题,和1258不同的就是这个还有几条路在之前就已经连通了的,所以不需要再去连. #include <stdio.h> #include <string.h> #define inf 100009 ]; ][],dis[],ans,n; int prim() { ;i<=n;i++) dis[i]=inf;dis[]…
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…
Constructing Roads Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2421 Appoint description:  System Crawler  (2015-05-27) Description There are N villages, which are numbered from 1 to N, and y…
Prim 算法是一种解决最小生成树问题(Minimum Spanning Tree)的算法.和 Kruskal 算法类似,Prim 算法的设计也是基于贪心算法(Greedy algorithm). Prim 算法的思想很简单,一棵生成树必须连接所有的顶点,而要保持最小权重则每次选择邻接的边时要选择较小权重的边.Prim 算法看起来非常类似于单源最短路径 Dijkstra 算法,从源点出发,寻找当前的最短路径,每次比较当前可达邻接顶点中最小的一个边加入到生成树中. 例如,下面这张连通的无向图 G,…
dijkstra(最短路)和Prim(最小生成树)下的堆优化 最小堆: down(i)[向下调整]:从第k层的点i开始向下操作,第k层的点与第k+1层的点(如果有)进行值大小的判断,如果父节点的值大于子节点的值,则修改,并继续对第k+1层与第k+2层的点进行判断和修改,否则不修改,且退出.当点向下移动到树的最后一层,没有子节点供判断与修改,停止操作. 树最多有log(n) 层[log(n)=log2n,一般省略数字2],时间复杂度log(n)次. up(i)[向上调整]:同理,时间复杂度log(…
题目链接:http://poj.org/problem?id=2421 最小生成树的变形,有的村庄已经连接了,就直接把他们的权值赋为0,一样的做最小生成树,Prim算法. #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…
题意:有几个村庄,要修最短的路,使得这几个村庄连通.但是现在已经有了几条路,求在已有路径上还要修至少多长的路. 分析:用Prim求最小生成树,将已有路径的长度置为0,由于0是最小的长度,所以一定会被Prim选中加入最小生成树. package Map; import java.util.Scanner; /** * Prime */ public class Poj_2421_Prim { static int MAXVEX = 200; static int n, m; static int[…
链接: http://poj.org/problem?id=2421 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21130   Accepted: 8924 Description There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can…
http://poj.org/problem?id=2031 Description You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You are expected to write a computer program to complete the task.  The space stati…
题目链接: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<…