1232: [usaco 162 Nov] cheertime limit: 10 sec memory limit: MB
Submit: 578 solved: 403
[Submit] [Status] Description
Farmer John became very lazy and he didn't want to continue to maintain the road for the cows to pass. the road is used to connect n (5 <= n <= 10,000) pastures. The pastures are consecutively numbered as 1 .. n. every farm is a home of cows. FJ planned to remove as many roads as possible in the P (N-1 <= P <= 100,000) Road, but also to maintain connectivity between pastures. first you have to decide which road is the N-1 road that needs to be retained. article J bidirectional roads connect farm s_j and E_j (1 <= s_j <= N; 1 <= E_j <= N; s_j! = E_j), and it takes l_j (0 <= l_j <= 1,000. no two pastures are connected by more than one road. the cows were very sad because their transportation system was cut down. you need to comfort every cow. every time you arrive at farm I (even if you have already been there), you have to spend C_ I (1 <= C_ I <= 1,000) time talking to the cows. every night you spend the night in the same farm (this is for your choice) until the cows are slowing down from sorrow. when you get up in the morning and go back to bed at night, you need to talk to the cows in your sleeping farm once. in this way, you can complete your conversation tasks. assuming Farmer John has adopted your suggestion, calculate the minimum time to comfort all cows. for the first 10 submissions, your program will run on some official test data and return the running results.
Input
* Row 1st: two integers N and P * 2nd separated by spaces .. n + 1 row: line I + 1 contains an integer: C_ I * n + 2 .. row n + p + 1: Row N + J + 1 contains three integers separated by spaces: s_j, E_j, and l_j.
Output
Row 1st: an integer of the total time required (including the two conversations with the cows in your farm ).
Sample input5 7
10
10
20
6
30
1 2 5
2 3 5
2 4 12
3 4 17
2 5 15
3 5 6
4 5 12
Sample output176
Hint
Source question:
Some usaco questions do not use much knowledge, but they have clever ideas.
Manually simulate this question and you will find that each question will be taken twice, therefore, we can replace the weight of this edge with W [A [I] + W [B [I] + 2 * C [I, because this is the real cost of taking this edge
Do kruskall and add min {W [I]}.
Code:
1 uses math; 2 var fa,a,b,c,w:array[0..120000] of longint; 3 i,j,n,m,ans:longint; 4 function find(x:longint):longint; 5 begin 6 if fa[x]<>x then fa[x]:=find(fa[x]); 7 exit(fa[x]); 8 end; 9 10 procedure sort(l,r:longint);11 var i,j,x,y:longint;12 begin13 i:=l;j:=r;x:=c[(i+j)>>1];14 repeat15 while c[i]<x do inc(i);16 while c[j]>x do dec(j);17 if i<=j then18 begin19 y:=c[i];c[i]:=c[j];c[j]:=y;20 y:=a[i];a[i]:=a[j];a[j]:=y;21 y:=b[i];b[i]:=b[j];b[j]:=y;22 inc(i);dec(j);23 end;24 until i>j;25 if i<r then sort(i,r);26 if j>l then sort(l,j);27 end;28 procedure init;29 begin30 readln(n,m);31 ans:=maxlongint;32 for i:=1 to n do begin readln(w[i]);ans:=min(ans,w[i]);end;33 for i:=1 to m do begin readln(a[i],b[i],c[i]);inc(c[i],c[i]+w[a[i]]+w[b[i]]);end;34 sort(1,m);35 end;36 procedure main;37 begin38 for i:=1 to n do fa[i]:=i;j:=1;39 for i:=1 to n-1 do40 begin41 while find(a[j])=find(b[j]) do inc(j);42 fa[find(a[j])]:=find(b[j]);43 inc(ans,c[j]);44 end;45 writeln(ans);46 end;47 begin48 assign(input,‘input.txt‘);assign(output,‘output.txt‘);49 reset(input);rewrite(output);50 init;51 main;52 close(input);close(output);53 end.
View code