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

来源:互联网 发布:天下3鬼墨女捏脸数据 编辑:程序博客网 时间:2024/06/04 20:00

约翰一共有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=recorddate:int64;ends:longint;next:point;end;type ccdd=recordx,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;begininc(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]) dobegindd:=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;beginget:=b[1];a[1]:=a[len];b[1]:=b[len];dec(len);i:=1;while i*2<=len dobeginif (i*2+1>len) or (a[i*2]<a[i*2+1]) then j:=i*2else j:=i*2+1;if a[i]>a[j] thenbegindd:=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;begini:=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;begini:=path[x];len:=0;dis[x,0]:=0;add(0,x,0);while len>0 dobegina:=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 dobeginy:=i^.ends;if a.y+1<=k then if dis[y,a.y+1]>dis[a.x,a.y] thenbegindis[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 thenbegindis[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;beginreadln(n,m,k);for i:=0 to n dofor j:=0 to k dodis[i,j]:=9999999999999;for i:=1 to m dobeginreadln(x,y,z);com(x,y,z);com(y,x,z);end;djs(1);ans:=dis[0,0];for j:=0 to k doif dis[n,j]<ans then ans:=dis[n,j];writeln(ans);end.
阅读全文
0 0
原创粉丝点击