Bad Cowtractors(最大生成树)

 

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.
 
 
题目意思:为了防止雇主不给钱或者少给钱,安装网络的人想要是每一个谷堆之间连通网络的成本最大,即生成一个最大生成树。
 
 
 
\\\克鲁斯卡尔算法
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,m,sum;
struct node
{
    int start;///起点
    int end;///终点
    int power;///权值
} edge[20050];
int pre[20050];
int cmp(node a,node b)
{
    return a.power<b.power;///按权值降序排列
    // return a.power<b.power
}
int find(int x)///并查集找祖先
{
    int a;///循环法
    a=x;
    while(pre[a]!=a)
    {
        a=pre[a];
    }
    return a;
}
void merge(int x,int y,int n)
{
    int fx =find(x);
    int fy =find(y);
    if(fx!=fy)
    {
        pre[fx]=fy;
        sum+=edge[n].power;
    }
}
int main()
{
    int i,x,count;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        sum=0;
        count=0;
        for(i=1; i<=m; i++)
        {
            scanf("%d%d%d",&edge[i].start,&edge[i].end,&x);
            edge[i].power=x;
            //edge[i].power=-x;
        }
        for(i=1; i<=m; i++) ///并查集的初始化
        {
            pre[i]=i;
        }
        sort(edge+1,edge+m+1,cmp);
        for(i=1; i<=m; i++)
        {
            merge(edge[i].start,edge[i].end,i);
        }
        for(i=1; i<=n; i++)
        {
            if(pre[i]==i)
            {
                count++;
            }
        }
        if(count==1)
        {
            printf("%d\n",sum);
            // printf("%d\n",-sum);
        }
        else
        {
            printf("-1\n");
        }
    }
    return 0;
}

  

///普里姆算法

#include<stdio.h>
#include<string.h>
#define MAX 0x3f3f3f3f
using namespace std;
int logo[1010];///用0和1来表示是否被选择过
int map1[1010][1010];
int dis[1010];///记录任意一点到这一点的最近的距离
int n,m;
int prim()
{
    int i,j,now;
    int sum=0;
    for(i=1; i<=n; i++) ///初始化
    {
        dis[i]=MAX;
        logo[i]=0;
    }
    for(i=1; i<=n; i++)
    {
        dis[i]=map1[1][i];
    }
    dis[1]=0;
    logo[1]=1;
    for(i=1; i<n; i++) ///循环查找
    {
        now=-MAX;
        int max1=-MAX;
        for(j=1; j<=n; j++)
        {
            if(logo[j]==0&&dis[j]>max1)
            {
                now=j;
                max1=dis[j];
            }
        }
        if(now==-MAX)///防止不成图
        {
            break;
        }
        logo[now]=1;
        sum=sum+max1;
        for(j=1; j<=n; j++) ///填入新点后更新最小距离,到顶点集的距离
        {
            if(logo[j]==0&&dis[j]<map1[now][j])
            {
                dis[j]=map1[now][j];
            }
        }
    }
    if(i<n)
    {
        printf("-1\n");
    }
    else
    {
        printf("%d\n",sum);
    }
}
int main()
{
    int i,j;
    int a,b,c;
    while(scanf("%d%d",&n,&m)!=EOF)///n是点数
    {
        for(i=1; i<=n; i++)
        {
            for(j=i; j<=n; j++)
            {
                if(i==j)
                {
                    map1[i][j]=map1[j][i]=0;
                }
                else
                {
                    map1[i][j]=map1[j][i]=-MAX;
                }
            }
        }
        for(i=0; i<m; i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            if(map1[a][b]<c)///防止出现重边
            {
                map1[a][b]=map1[b][a]=c;
            }
        }
        prim();
    }
    return 0;
}

 

 

 

 

 

 

 

 

 

  

posted @ 2018-06-30 08:52  王陸  阅读(347)  评论(0编辑  收藏  举报