1614: [usaco Jan] telephone lines telephone line time limit: 5 sec memory limit: 64 MB
Submit: 892 solved: 399
[Submit] [Status] Description
Farmer John planned to direct the telephone line to his farm, but the telecommunications company did not plan to provide him with free services. Therefore, FJ must pay a certain fee to the telecommunications company. There are N (1 <= n <= 1,000) roots distributed around the farm of FJ .. N sequential number of discarded telephone lines, any two telephone lines are not connected to the telephone line. A total of P (1 <= P <= 10,000) can be used to pull telephone lines between telephone lines, and others cannot be connected due to the distance. The two ends of the I-th telephone pole are A_ I and B _ I, respectively, and the distance between them is L_ I (1 <= L_ I <= 1,000,000 ). Ensure that each pair of {a_ I, B _ I} appears only once at most. The telephone pole numbered 1 has been connected to the national telephone network, and all the telephone lines on the farm are connected to the telephone pole numbered n. That is to say, the task of Fj is only to find a path to connect telephone lines 1 and N. Other telephone lines do not have to be connected to the telephone network. After negotiation, the telecommunications company eventually agreed to connect K (0 <= k <n) to the telephone pole specified by FJ for free. For other telephone lines, FJ will pay for them, equal to the length of the longest telephone line (each telephone line is associated with only one pair of telephone lines ). If the number of telephone lines to be connected does not exceed K pairs, the total cost of Fj is 0. Calculate the minimum cost of Fj over the telephone line.
Input
* Row 1st: Three integers separated by spaces: N, P, and K
* Row 2nd. p + 1: I + 1 Act 3 integers separated by spaces: a_ I, B _ I, L_ I
Output
* Row 1st: output an integer, which is the minimum expenditure of Fj on this project. If the task cannot be completed, output-1
Sample input5 7 1
1 2 5
3 1 4
2 4 8
3 2 3
5 2 9
3 4 7
4 5 6
Input description:
A total of five discarded telephone lines. Telephone Line 1 cannot be directly connected to telephone lines 4 or 5. Phone number
Pole 5 cannot be directly connected to telephone lines 1 or 3. Telephone lines can be used between all other telephone lines. China Telecom
The company can connect one pair of telephone lines to FJ for free.
Sample output4
Output description:
FJ selects the following link scheme: 1-> 3; 3-> 2; 2-> 5, which is required between the three telephone lines
The telephone lines are 4, 3, and 9 respectively. FJ asked the telecommunications company to provide the telephone line with a length of 9, so,
The maximum length of the telephone line he needs to purchase is 4.
Hint Source
Silver
Question: Okay, it's binary. Does this question seem to have something to do with binary? Code:
1 uses math; 2 const maxn=1000+200; 3 type node=record 4 go,next,w:longint; 5 end; 6 var d,head:array[0..maxn] of longint; 7 v:array[0..maxn] of boolean; 8 q:array[0..10*maxn] of longint; 9 e:array[0..20000] of node;10 i,n,m,k,l,r,x,y,z,mid,tot,ll,rr:longint;11 procedure insert(x,y,z:longint);12 begin13 inc(tot);14 e[tot].go:=y;e[tot].w:=z;e[tot].next:=head[x];head[x]:=tot;15 end;16 17 function spfa(val:longint):boolean;18 var i,x,y,tmp:longint;19 begin20 fillchar(d,sizeof(d),60);21 fillchar(v,sizeof(v),false);22 l:=0;r:=1;q[1]:=1;d[1]:=0;v[1]:=true;23 while l<r do24 begin25 l:=l+1;26 x:=q[l];v[x]:=false;27 i:=head[x];28 while i<>0 do29 begin30 y:=e[i].go;31 if e[i].w>val then tmp:=d[x]+1 else tmp:=d[x];32 if tmp<d[y] then33 begin34 d[y]:=tmp;35 if not(v[y]) then36 begin37 v[y]:=true;38 r:=r+1;39 q[r]:=y;40 end;41 end;42 i:=e[i].next;43 end;44 end;45 exit(d[n]<=k);46 end;47 procedure init;48 begin49 readln(n,m,k);50 for i:=1 to m do51 begin52 readln(x,y,z);53 insert(x,y,z);insert(y,x,z);54 end;55 end;56 procedure main;57 begin58 ll:=0;rr:=1000000+1;59 while ll<rr do60 begin61 mid:=(ll+rr)>>1;62 if spfa(mid) then rr:=mid else ll:=mid+1;63 end;64 if ll>1000000 then writeln(‘-1‘) else writeln(ll);65 end;66 67 begin68 assign(input,‘input.txt‘);assign(output,‘output.txt‘);69 reset(input);rewrite(output);70 init;71 main;72 close(input);close(output);73 end.
View code