P2939 [USACO09FEB]改造路Revamping Trails(迪杰斯特拉+堆优化)

约翰一共有N)个牧场.由M条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场1出发到牧场N去给奶牛检查身体.

通过每条小径都需要消耗一定的时间.约翰打算升级其中K条小径,使之成为高 速公路.在高速公路上的通行几乎是瞬间完成的,所以高速公路的通行时间为0.

请帮助约翰决定对哪些小径进行升级,使他每天早上到牧场W花的时间最少.输出这个最少 的时间.

输入输出格式

输入格式:
Line 1: Three space-separated integers: N, M, and K

Lines 2..M+1: Line i+1 describes trail i with three space-separated integers: P1_i, P2_i, and T_i
输出格式:
Line 1: The length of the shortest path after revamping no more than K edges
输入输出样例

输入样例#1: 复制
4 4 1
1 2 10
2 4 10
1 3 1
3 4 100
输出样例#1: 复制
1
说明

K is 1; revamp trail 3->4 to take time 0 instead of 100. The new shortest path is 1->3->4, total traversal time now 1.

又是一道卡spfa的裸题program df;
type point=^node;
node=record
date:int64;
ends:longint;
next:point;
end;
type ccdd=record
x,y:longint;
end;
var i,j,n,m,x,y,z,k,t,len:longint;
ans:int64;
path:array[0..100000] of point;
a:array[0..2000000] of int64;
dis:array[0..100000,0..20] of int64;
c:array[0..100000,0..20] of boolean;
b:array[0..2000000] of ccdd;
procedure add(xx:int64;y,z:longint);
var i,dd:int64;
d2:ccdd;
begin
inc(len);
a[len]:=xx;
b[len].x:=y;
b[len].y:=z;
//writeln('min ',len,' ',a[len]);
i:=len;
while (i>1) and (a[i div 2]>a[i]) do
begin
dd:=a[i div 2]; a[i div 2]:=a[i]; a[i]:=dd;
d2:=b[i div 2]; b[i div 2]:=b[i]; b[i]:=d2;
i:=i div 2;
end;
end;function get:ccdd;
var i,j,dd:int64;
d2:ccdd;
begin
get:=b[1];
a[1]:=a[len];
b[1]:=b[len];
dec(len);
i:=1;
while i*2<=len do
begin
if (i*2+1>len) or (a[i*2]2+1]) then j:=i*2
else j:=i*2+1;
if a[i]>a[j] then
begin
dd:=a[i]; a[i]:=a[j]; a[j]:=dd;
d2:=b[i]; b[i]:=b[j]; b[j]:=d2;
i:=j;
end else break;
end;
end;procedure com(x,y,z:longint);
var i:point;
begin
i:=path[x];
new(path[x]);
path[x]^.ends:=y;
path[x]^.date:=z;
path[x]^.next:=i;
end;procedure djs(x:longint);
var i:point;
y,u,ans:longint;
a,b:ccdd;
begin
i:=path[x];
len:=0;
dis[x,0]:=0;
add(0,x,0);while len>0 do
begina:=get;
while (c[a.x,a.y]=true) and (len>0) do a:=get;
if (c[a.x,a.y]=true) and (len=0) then exit;i:=path[a.x];
c[a.x,a.y]:=true;
while i<>nil do
begin
y:=i^.ends;if a.y+1<=k then 
if dis[y,a.y+1]>dis[a.x,a.y] then
begin
dis[y,a.y+1]:=dis[a.x,a.y];
if not c[y,a.y+1] then add(dis[y,a.y+1],y,a.y+1);
end;if dis[y,a.y]>dis[a.x,a.y]+i^.date then
begin
dis[y,a.y]:=dis[a.x,a.y]+i^.date;
if (not c[y,a.y]) then add(dis[y,a.y],y,a.y);
end;i:=i^.next;
end;end;end;begin
readln(n,m,k);
for i:=0 to n do
for j:=0 to k do
dis[i,j]:=9999999999999;
for i:=1 to m do
begin
readln(x,y,z);
com(x,y,z);
com(y,x,z);
end;
djs(1);ans:=dis[0,0];
for j:=0 to k do
if dis[n,j]then ans:=dis[n,j];
writeln(ans);
end.


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部