奶牛贝茜被雇去建设N(2≤N≤1000)个牛棚间的互联网.她已经勘探出M(1≤M≤
20000)条可建的线路,每条线路连接两个牛棚,而且会苞费C(1≤C≤100000).农夫约翰吝啬得很,他希望建设费用最少甚至他都不想给贝茜工钱. 贝茜得知工钱要告吹,决定报复.她打算选择建一些线路,把所有牛棚连接在一起,让约翰花费最大.但是她不能造出环来,这样约翰就会发现.
标签:
1 /************************************************************** 2 Problem: 3390 3 User: HansBug 4 Language: Pascal 5 Result: Accepted 6 Time:56 ms 7 Memory:1788 kb 8 ****************************************************************/ 9 10 var 11 i,j,k,l,m,n,x,ans:longint; 12 a:array[0..100005,1..3] of longint; 13 c:array[0..100005] of longint; 14 function getfat(x:longint):longint; 15 begin 16 if c[x]<>x then c[x]:=getfat(c[x]); 17 exit(c[x]); 18 end; 19 procedure swap(var x,y:longint); 20 var z:longint; 21 begin 22 z:=x;x:=y;y:=z; 23 end; 24 procedure sort(l,r:longint); 25 var i,j,x,y:longint; 26 begin 27 i:=l;j:=r;x:=a[(l+r) div 2,3]; 28 repeat 29 while a[i,3]>x do inc(i); 30 while a[j,3]<x do dec(j); 31 if i<=j then 32 begin 33 swap(a[i,1],a[j,1]); 34 swap(a[i,2],a[j,2]); 35 swap(a[i,3],a[j,3]); 36 inc(i);dec(j); 37 end; 38 until i>j; 39 if i<r then sort(i,r); 40 if l<j then sort(l,j); 41 end; 42 43 {$IFDEF WINDOWS}{$R bzoj3390.rc}{$ENDIF} 44 45 begin 46 readln(n,m); 47 for i:=1 to n do c[i]:=i; 48 for i:=1 to m do readln(a[i,1],a[i,2],a[i,3]); 49 sort(1,m); 50 j:=1;x:=0; 51 for i:=1 to n-1 do 52 begin 53 while j<=m do 54 begin 55 k:=getfat(a[j,1]); 56 l:=getfat(a[j,2]); 57 if k<>l then break; 58 inc(j); 59 end; 60 if k=l then break; 61 ans:=ans+a[j,3]; 62 c[k]:=l;inc(j); 63 inc(x); 64 end; 65 if x<(n-1) then writeln(-1) else writeln(ans); 66 readln; 67 end.
3390: [Usaco2004 Dec]Bad Cowtractors牛的报复
标签:
原文地址:http://www.cnblogs.com/HansBug/p/4415856.html