POJ 2377 最大生成树 prim实现

原创 2011年02月14日 14:46:00
Bad Cowtractors
Time Limit: 1000MS   Memory Limit: 65536K
Total 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".

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.

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 8
1 2 3
1 3 7
2 3 10
2 4 4
2 5 8
3 4 6
3 5 2
4 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.

Source

 

题目意思很明了,就是想叫你求一个最大生成树

我的想法和网上流行的算法有点不一样,正所谓非主流就是这个意思。。。

主流的算法是利用克鲁斯卡尔算法,并且在排序的时候按降序排列

而我是把所有的边都存为负权,然后再利用prim求最小生成树、最后答案取负就可以了

注意:数据中有重边,必须要去掉。另外不连通的情况要慎重考虑

我的代码:

#include<stdio.h>
#include<algorithm>
#define inf 99999999

using 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");
	else
		printf("%d/n",-ans);
	return 0;
}


 

----------------------------------------------------------------------------------------------------------传说中的分割线。。。

 

 

相关文章推荐

POJ 1797 Heavy Transportation(最大生成树)

题目链接:http://poj.org/problem?id=1797 题目大意:给定n个顶点,以及m条边的描述,每条边的描述包括:起点、终点、权重。现在要从顶点1出发到达顶点n,求路径中能够承受的最...

POJ 1797 Heavy Transportation(最大生成树)

POJ 1797 Heavy Transportation(最大生成树) http://poj.org/problem?id=1797 题意:        求1号点s到n号点t的可行路径上最小值的最...

(poj 2377)Kruskal算法 最大生成树

这道题只是一道模板题,感到唯一的坑点就是n,m容易打错,一定要注意结构体要开到Max(M)+n; 之前便是因为这个地方Runtime Error了两次;顺便注意最后输出的答案 为long long型题...

POJ 3723 Conscription(kruskal求最大生成树)

http://poj.org/problem?id=3723 题目的意思相当于是给出一个非连通图,非连通图可以看成是几个连通图的组合。每一次有人入伍的时候,都会和他/她所在的连通图中,和已经...
  • secfly
  • secfly
  • 2016年04月11日 22:13
  • 273

hdu4786 Fibonacci Tree(最小生成树+最大生成树+01树+理解)

http://acm.hdu.edu.cn/showproblem.php?pid=4786 题意:有n个节点,给你一些白边和黑边,问能否用两色边构造出一棵生成树,且树中白色边的数量为一个Fib...

POJ 2377 最大生成树

#include #include #include using namespace std; //#define DEBUG struct edge { int u; int v; in...

POJ 2377 (最大生成树 Kruskal)

Bad Cowtractors Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13469   Accepted:...

POJ 1797 Heavy Transportation(最大生成树-Prim)

Description 从城市1到城市n运送货物,有m条道路,每条道路都有它的最大载重量,问从城市1到城市n运送最多的重量是多少 Input 第一行一个整数T表示用例组数,每组用例第一行两个整数...
  • V5ZSQ
  • V5ZSQ
  • 2015年12月28日 14:34
  • 326

POJ--3723---Conscription---最大生成树

本题大体题意是:一个国王要招募士兵,现有男兵和女兵,招募一个需要10000元,但是如果一个男兵和一个女兵有关系d的话,在已经招募一个的情况下招募另一个只需花费10000-d即可。现在给出以下关系,问国...

最大生成树与最小生成树

Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ 2377 最大生成树 prim实现
举报原因:
原因补充:

(最多只允许输入30个字)