• BZOJ3390: [Usaco2004 Dec]Bad Cowtractors牛的报复


    3390: [Usaco2004 Dec]Bad Cowtractors牛的报复

    Time Limit: 1 Sec  Memory Limit: 128 MB
    Submit: 43  Solved: 29
    [Submit][Status]

    Description

        奶牛贝茜被雇去建设N(2≤N≤1000)个牛棚间的互联网.她已经勘探出M(1≤M≤
    20000)条可建的线路,每条线路连接两个牛棚,而且会苞费C(1≤C≤100000).农夫约翰吝啬得很,他希望建设费用最少甚至他都不想给贝茜工钱. 贝茜得知工钱要告吹,决定报复.她打算选择建一些线路,把所有牛棚连接在一起,让约翰花费最大.但是她不能造出环来,这样约翰就会发现.

    Input

      第1行:N,M.
      第2到M+1行:三个整数,表示一条可能线路的两个端点和费用.
     

    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

    连接4和5,2和5,2和3,1和3,花费17+8+10+7=42

    HINT

     

    Source

    Silver

     题解:

    裸MST,呵呵。

    代码:

     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<iostream>
     7 #include<vector>
     8 #include<map>
     9 #include<set>
    10 #include<queue>
    11 #include<string>
    12 #define inf 1000000000
    13 #define maxn 20005
    14 #define maxm 500+100
    15 #define eps 1e-10
    16 #define ll long long
    17 #define pa pair<int,int>
    18 #define for0(i,n) for(int i=0;i<=(n);i++)
    19 #define for1(i,n) for(int i=1;i<=(n);i++)
    20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
    21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
    22 #define mod 1000000007
    23 using namespace std;
    24 inline int read()
    25 {
    26     int x=0,f=1;char ch=getchar();
    27     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    28     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
    29     return x*f;
    30 }
    31 int n,m,fa[maxn];
    32 struct rec{int x,y,w;}a[maxn];
    33 inline bool cmp(rec a,rec b)
    34 {
    35     return a.w>b.w;
    36 }
    37 inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
    38 int main()
    39 {
    40     freopen("input.txt","r",stdin);
    41     freopen("output.txt","w",stdout);
    42     n=read();m=read();
    43     for1(i,m)a[i].x=read(),a[i].y=read(),a[i].w=read();
    44     sort(a+1,a+m+1,cmp);
    45     for1(i,n)fa[i]=i;
    46     int j=1,i,ans=0;
    47     for(i=1;i<n;i++)
    48     {
    49         while(j<=m&&find(a[j].x)==find(a[j].y))j++;
    50         if(j>m)break;
    51         fa[find(a[j].x)]=find(a[j].y);
    52         ans+=a[j].w;
    53         j++;
    54     }
    55     printf("%d
    ",i<n?-1:ans);
    56     return 0;
    57 }
    View Code
  • 相关阅读:
    [luoguP1098] 字符串的展开(模拟)
    [luoguP1033] 自由落体(模拟?)
    [luoguP1011] 车站(递推)
    [luoguP1097] 统计数字(水)
    [luoguP2672] 推销员(贪心 + 树状数组 + 优先队列)
    [luoguP1043] 数字游戏(DP)
    [luoguP1058] 立体图(超级大模拟(¬︿̫̿¬☆))
    [luoguP1021] 邮票面值设计(DFS + dp)
    POJ 2184 Cow Exhibition (带负值的01背包)
    POJ 2392 Space Elevator (DP)
  • 原文地址:https://www.cnblogs.com/zyfzyf/p/4005518.html
Copyright © 2020-2023  润新知