BZOJ3390: [Usaco2004 Dec]Bad Cowtractors牛的報複

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   ar   for   

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     最大的花費.如果不能建成合理的線路,就輸出-1Sample Input5 8
1 2 3
1 3 7
2 3 10
2 4 4
2 5 8
3 4 6
3 5 2
4 5 17Sample Output42

串連4和5,2和5,2和3,1和3,花費17+8+10+7=42HINT 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 100000000013 #define maxn 2000514 #define maxm 500+10015 #define eps 1e-1016 #define ll long long17 #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 100000000723 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\n",i<n?-1:ans);56     return 0;57 }
View Code

 

BZOJ3390: [Usaco2004 Dec]Bad Cowtractors牛的報複

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.