標籤:des style blog http color os io 資料
1614: [Usaco2007 Jan]Telephone Lines架設電話線Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 892 Solved: 399
[Submit][Status]Description
Farmer John打算將電話線引到自己的農場,但電信公司並不打算為他提供免費服務。於是,FJ必須為此向電信公司支付一定的費用。 FJ的農場周圍分布著N(1 <= N <= 1,000)根按1..N順次編號的廢棄的電話線杆,任意兩根電話線杆間都沒有電話線相連。一共P(1 <= P <= 10,000)對電話線杆間可以拉電話線,其餘的那些由於隔得太遠而無法被串連。 第i對電話線杆的兩個端點分別為A_i、B_i,它們間的距離為 L_i (1 <= L_i <= 1,000,000)。資料中保證每對{A_i,B_i}最多隻出現1次。編號為1的電話線杆已經接入了全國的電話網路,整個農場的電話線全都連到了編號為N的電話線杆上。也就是說,FJ的任務僅僅是找一條將1號和N號電話線杆連起來的路徑,其餘的電話線杆並不一定要連入電話網路。 經過談判,電信公司最終同意免費為FJ連結K(0 <= K < N)對由FJ指定的電話線杆。對於此外的那些電話線,FJ需要為它們付的費用,等於其中最長的電話線的長度(每根電話線僅連結一對電話線杆)。如果需要連結的電話線杆不超過 K對,那麼FJ的總支出為0。 請你計算一下,FJ最少需要在電話線上花多少錢。
Input
* 第1行: 3個用空格隔開的整數:N,P,以及K
* 第2..P+1行: 第i+1行為3個用空格隔開的整數:A_i,B_i,L_i
Output
* 第1行: 輸出1個整數,為FJ在這項工程上的最小支出。如果任務不可能完成, 輸出-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
輸入說明:
一共有5根廢棄的電話線杆。電話線杆1不能直接與電話線杆4、5相連。電話
線杆5不能直接與電話線杆1、3相連。其餘所有電話線杆間均可拉電話線。電信
公司可以免費為FJ連結一對電話線杆。
Sample Output4
輸出說明:
FJ選擇如下的連結方案:1->3;3->2;2->5,這3對電話線杆間需要的
電話線的長度分別為4、3、9。FJ讓電信公司提供那條長度為9的電話線,於是,
他所需要購買的電話線的最大長度為4。
HINT Source
Silver
題解:好吧是二分。這種題目和二分好像都差不多有點兒關係?代碼:
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