POJ 2377 最大生成树 prim实现
来源:互联网 发布:windows server 版本 编辑:程序博客网 时间:2024/05/02 06:45
Bad Cowtractors
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6904 Accepted: 3008
Description
Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N <= 1,000) barns that are conveniently numbered 1..N. FJ has already done some surveying, and found M (1 <= M <= 20,000) possible connection routes between pairs of barns. Each possible connection route has an associated cost C (1 <= C <= 100,000). Farmer John wants to spend the least amount on connecting the network; he doesn't even want to pay Bessie.
Realizing Farmer John will not pay her, Bessie decides to do the worst job possible. She must decide on a set of connections to install so that (i) the total cost of these connections is as large as possible, (ii) all the barns are connected together (so that it is possible to reach any barn from any other barn via a path of installed connections), and (iii) so that there are no cycles among the connections (which Farmer John would easily be able to detect). Conditions (ii) and (iii) ensure that the final set of connections will look like a "tree".
Realizing Farmer John will not pay her, Bessie decides to do the worst job possible. She must decide on a set of connections to install so that (i) the total cost of these connections is as large as possible, (ii) all the barns are connected together (so that it is possible to reach any barn from any other barn via a path of installed connections), and (iii) so that there are no cycles among the connections (which Farmer John would easily be able to detect). Conditions (ii) and (iii) ensure that the final set of connections will look like a "tree".
Input
* Line 1: Two space-separated integers: N and M
* Lines 2..M+1: Each line contains three space-separated integers A, B, and C that describe a connection route between barns A and B of cost C.
* Lines 2..M+1: Each line contains three space-separated integers A, B, and C that describe a connection route between barns A and B of cost C.
Output
* Line 1: A single integer, containing the price of the most expensive tree connecting all the barns. If it is not possible to connect all the barns, output -1.
Sample Input
5 81 2 31 3 72 3 102 4 42 5 83 4 63 5 24 5 17
Sample Output
42
Hint
OUTPUT DETAILS:
The most expensive tree has cost 17 + 8 + 10 + 7 = 42. It uses the following connections: 4 to 5, 2 to 5, 2 to 3, and 1 to 3.
The most expensive tree has cost 17 + 8 + 10 + 7 = 42. It uses the following connections: 4 to 5, 2 to 5, 2 to 3, and 1 to 3.
Source
USACO 2004 December Silver
题目意思很明了,就是想叫你求一个最大生成树
我的想法和网上流行的算法有点不一样,正所谓非主流就是这个意思。。。
主流的算法是利用克鲁斯卡尔算法,并且在排序的时候按降序排列
而我是把所有的边都存为负权,然后再利用prim求最小生成树、最后答案取负就可以了
注意:数据中有重边,必须要去掉。另外不连通的情况要慎重考虑
我的代码:
#include<stdio.h>#include<algorithm>#define inf 99999999using namespace std;struct node{int v1;int v2;int len;};node e[1005];int dis[1005][1005];void init(int n){int i,j;for(i=0;i<=n;i++)for(j=0;j<=n;j++)dis[i][j]=inf;}int prim(int n){int i,j,vx,vy,leng,min,minl;int res=0;for(i=1;i<=n-1;i++){e[i].v1=1;e[i].v2=i+1;e[i].len=dis[1][i+1];}for(i=1;i<=n-1;i++){min=-1;minl=2*inf;for(j=i;j<=n-1;j++){if(e[j].len<minl){minl=e[j].len;min=j;}}if(min==-1)break;res=res+minl;swap(e[i],e[min]);vx=e[i].v2;for(j=i+1;j<=n-1;j++){vy=e[j].v2;leng=dis[vx][vy];if(leng<e[j].len){e[j].len=leng;e[j].v1=vx;}}}return res;}int main(){int i,a,b,n,m,c;scanf("%d%d",&n,&m);init(n);for(i=0;i<m;i++){scanf("%d%d%d",&a,&b,&c);if(dis[a][b]==inf||-c<dis[a][b]){dis[a][b]=-c;dis[b][a]=-c;}}int ans=prim(n);if(ans>0)printf("-1/n");elseprintf("%d/n",-ans);return 0;}
----------------------------------------------------------------------------------------------------------传说中的分割线。。。
- POJ 2377 最大生成树 prim实现
- POJ 2377 Bad Cowtractors(最大生成树—prim算法)
- POJ 2377 最大生成树(prim,kruskal)
- poj 2485 最小生成树中的最大边 prim
- POJ 1797 Heavy Transportation(最大生成树-Prim)
- (POJ 1258)Prim算法 最大生成树
- poj 2377-prim(最小生成树的最大边权的和)
- poj 2485 Highways prim最小生成树 基础 球最小生成树中得最大边
- 【最大生成树】POJ 2377
- POJ 2377 最大生成树
- poj 2377 最大生成树
- POJ 2377 最大生成树
- POJ 2377(最大生成树)
- POJ 2395 Out of Hay(最小生成树—prim算法记录最大边)
- POJ Problem 2377 Cowtractors 【最小生成树Prim】
- Prim实现最小生成树
- POJ 1258 : 最小生成树(Prim)
- POJ 1251 最小生成树prim算法
- CANopen协议学习笔记一
- JAVA的初始化顺序
- Linux 下的串口编程(一)
- Java Regular Expressions Syntax
- 情人节寂寞了谁
- POJ 2377 最大生成树 prim实现
- c/c++中static详解
- test
- 已知某二叉树的某两种遍历序列,求另一种遍历序列面试题解法总结(转)
- (转)manifest的作用
- 内存对齐用法小结
- XtraReports 打印控件的简单使用
- 写给充满浮躁与抱怨的程序员
- 在datagrid里面生成指定的DataView ,进行过滤.把符合条件的数据显示出来