ACM

# POJ 2377 最大生成树 prim实现

 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

#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;
}



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

2016-01-14 16:58:55

#### 最小生成树Prim算法理解

2014-08-16 18:49:34

#### （poj 2377）Kruskal算法 最大生成树

2017-07-22 16:07:31

#### POJ 1797 Heavy Transportation（最大生成树）

2017-01-05 15:11:44

#### hdu4786 Fibonacci Tree（最小生成树+最大生成树+01树+理解）

2016-11-13 20:24:01

#### [NOIP2013]货车运输，最大生成树+LCA

2016-08-20 02:44:32

#### POJ 3723 Conscription（kruskal求最大生成树）

2016-04-11 22:13:29

#### POJ 2377 （最大生成树 Kruskal）

2016-07-30 21:02:27

#### codeforces 100959B Airports 曼哈顿距离最大生成树

2017-10-02 18:56:58

#### HDU6187 最大生成树

2018-03-07 23:52:59