zoukankan      html  css  js  c++  java
  • 最大的路径

    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. 

    想想和最大费用最大流和最小费用最大流关系一样,我们在求最大生成树的时候只需要把题目中给出的所有权边去相反数,然后利用最小生成树的kruskal算法,将结果取相反数就是答案

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    int n, m,sum;
    struct node
    {
        int start,end,power;//start为起始点,end为终止点,power为权值
    } edge[20001];
    int pre[20001];
    
    
    int cmp(node a, node b)
    {
        return a.power<b.power;//按照权值排序
    }
    
    
    int find(int x)//并查集找祖先
    {
        if(x!=pre[x])
        {
            pre[x]=find(pre[x]);
        }
        return pre[x];
    }
    
    
    void merge(int x,int y,int n)//并查集合并函数,n是用来记录最短路中应该加入哪个点
    {
        int fx=find(x);
        int fy=find(y);
        if(fx!=fy)
        {
            pre[fx]=fy;
            sum+=edge[n].power;
        }
    }
    int main()
    {
       while(~scanf("%d%d",&n,&m))
            {sum=0;
            int i;
            int start,end,power;
            for(i=1; i<=m; i++)
            {
                scanf("%d %d %d", &start, &end, &power);
                edge[i].start=start,edge[i].end=end,edge[i].power=-power;
            }
            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);
            }
            int ans=0;
            for(i=1;i<=n;i++)
                if(pre[i]==i)
                   ans++;
            if(sum<0)
                sum=-sum;
            if(ans==1)//一个祖先
              printf("%d
    ",sum);
            else
                printf("-1
    ");
    
    
        }
        return 0;
    }

     

  • 相关阅读:
    jquery正则表达式验证:正整数(限制长度)
    H5页面快速搭建之高级字体应用实践
    如何用Python写一个贪吃蛇AI
    HashMap多线程并发问题分析
    为RecyclerView打造通用Adapter 让RecyclerView更加好用
    学好Mac常用命令,助力iOS开发
    使用 Realm 和 Swift 创建 ToDo 应用
    看Facebook是如何优化React Native性能
    利用github搭建个人maven仓库
    Objective-C Runtime之着魔的UIAlertView
  • 原文地址:https://www.cnblogs.com/zcy19990813/p/9702787.html
Copyright © 2011-2022 走看看