还是蛮简单的一道题,首先dfs一遍,在所有能到达放有干草的洞穴的所有路径中,找出路径上最小伐值的最大值,按这个值由小到大,再来一遍贪心就行了,能放就放,不能放拉倒(也可以理解为,不能放把最前一个删了)。
但是如果题目改为每个洞穴不止一堆干草的话,也是没有问题的,只要在队列上在维护一个小根堆,判断队列中最小的干草堆是否比当前小,小则替换,否则不变。
1 const maxn=200; 2 type 3 node=record 4 f,t,w:longint; 5 end; 6 var n,i,j,m,k,u,v,x,now,ans:longint; 7 head:array[0..101] of longint; 8 b:array[0..2001] of node; 9 f:array[0..100000] of longint; 10 p:array[0..101] of boolean; 11 a:array[0..15] of longint; 12 max:array[0..101] of longint; 13 function min(a,b:longint):longint; 14 begin 15 if a>b then exit(b) 16 else exit(a); 17 end; 18 procedure insert(num,u,v,x:longint); 19 begin 20 b[num].f:=head[u]; 21 b[num].t:=v; 22 b[num].w:=x; 23 head[u]:=num; 24 end; 25 procedure qs(t,w:longint); 26 var mid,l,r,tem:longint; 27 begin 28 l:=t; r:=w; mid:=max[a[(l+r) shr 1]]; 29 repeat 30 begin 31 while max[a[l]]<mid do inc(l); 32 while max[a[r]]>mid do dec(r); 33 if l<=r then 34 begin 35 tem:=a[l]; 36 a[l]:=a[r]; 37 a[r]:=tem; 38 inc(l); 39 dec(r); 40 end; 41 end; 42 until l>r; 43 if t<r then qs(t,r); 44 if l<w then qs(l,w); 45 end; 46 procedure bfs; 47 var now,nowe,l,r:longint; 48 begin 49 fillchar(max,sizeof(max),0); 50 fillchar(p,sizeof(p),true); 51 l:=1; r:=1; f[1]:=1; max[1]:=maxn; 52 while l<=r do 53 begin 54 now:=f[l]; 55 nowe:=head[now]; 56 while nowe<>0 do 57 begin 58 if min(max[now],b[nowe].w)>max[b[nowe].t] then 59 begin 60 max[b[nowe].t]:=min(max[now],b[nowe].w); 61 if p[b[nowe].t] then 62 begin 63 p[b[nowe].t]:=false; 64 inc(r); 65 f[r]:=b[nowe].t; 66 end; 67 end; 68 nowe:=b[nowe].f; 69 end; 70 inc(l); 71 p[now]:=true; 72 end; 73 end; 74 begin 75 readln(n,m,k); 76 for i:=1 to k do 77 readln(a[i]); 78 for i:=1 to m do 79 begin 80 readln(u,v,x); 81 insert(2*i-1,u,v,x); 82 insert(2*i,v,u,x); 83 end; 84 bfs; 85 qs(1,k); 86 now:=1; 87 while (max[a[now]]=0) and (now<=k) do inc(now); //q[1]:=now; 88 if now<>k+1 then ans:=1 89 else begin 90 writeln(0); 91 halt; 92 end; 93 for i:=now+1 to k do 94 if max[a[i]]>ans then inc(ans); 95 writeln(ans); 96 end.
(转载请注明出处:http://www.cnblogs.com/Kalenda/)
时间: 2024-10-14 18:15:56