题目
路由是指通过计算机网络把信息从源地址传输到目的地址的活动,也是计算机网络设计中的重点和难点。网络中实现
路由转发的硬件设备称为路由器。为了使数据包最快的到达目的地,路由器需要选择最优的路径转发数据包。例如,
在常用的路由算法OSPF(开放式最短路径优先)中,路由器会使用经典的Dijkstra算法计算最短路径,然后尽量沿最短
路径转发数据包。
现在,若已知一个计算机网络中各路由器间的连接情况,以及各个路由器的最大吞吐量(即每秒能转发的数据包数
量),假设所有数据包一定沿最短路径转发,试计算从路由器1到路由器n的网络的最大吞吐量。计算中忽略转发及传
输的时间开销,不考虑链路的带宽限制,即认为数据包可以瞬间通过网络。路由器1到路由器n作为起点和终点,自身
的吞吐量不用考虑,网络上也不存在将1和n直接相连的链路。
输入格式
输入文件第一行包含两个空格分开的正整数n和m,分别表示路由器数量和链路的数量。网络中的路由器使用1到n编
号。
接下来m行,每行包含三个空格分开的正整数a、b和d,表示从路由器a到路由器b存在一条距离为d的双向链路。
接下来n行,每行包含一个正整数c,分别给出每一个路由器的吞吐量。
输出格式
输出一个整数,为题目所求吞吐量。
输入样例
7 10
1 2 2
1 5 2
2 4 1
2 3 3
3 7 1
4 5 4
4 3 1
4 6 1
5 6 2
6 7 1
1
100
20
50
20
60
1
输出样例
70
n<=500,m<=100000,c,d<=
109
分析
这题的题意其实很简单,就是给你一个图,求它的最短路图,然后求起点到终点的最大流。
所以,我们可以用spfa建一个图,每条边都是在最短路图中的。但,有一点比较麻烦的是,这里限制的是点的流量,所以我们可以拆点,分成两个点,x->x’的容量为这个点的限制,然后再做最大流,即可。
代码
const maxl=10000000000000;
var
nu,n,m,i,x,y,z,num,ni:longint;ans:int64;
b,last,next,v,las,nex,b1,re:array[1..200000] of longint;
f:array[1..200000] of int64;
bz:array[1..505] of boolean;
dis:array[1..505] of int64;
vh,h:array[0..1005] of longint;
a:array[1..1000000] of longint;
procedure insert(x,y,z:longint);
begin
inc(nu);b[nu]:=y;next[nu]:=last[x];last[x]:=nu;v[nu]:=z;
end;
procedure add(x,y:longint;z:int64);
begin
inc(num);b1[num]:=y;nex[num]:=las[x];las[x]:=num;f[num]:=z;
inc(num);b1[num]:=x;nex[num]:=las[y];las[y]:=num;f[num]:=0;re[num-1]:=num;re[num]:=num-1;
end;
procedure spfa;
var p,l,r,now,i:longint;
begin
fillchar(dis,sizeof(dis),127);
a[1]:=1;bz[a[1]]:=true;l:=0;r:=1;dis[a[1]]:=0;
while l<>r do begin
inc(l);now:=a[l];
p:=last[now];
while p<>0 do begin
if dis[b[p]]>dis[now]+v[p] then begin
dis[b[p]]:=dis[now]+v[p];
if not bz[b[p]] then begin
bz[b[p]]:=true;
inc(r);a[r]:=b[p];
end;
end;
p:=next[p];
end;
bz[now]:=false;
end;
for i:=1 to n do begin
p:=last[i];
while p<>0 do begin
if dis[b[p]]=dis[i]+v[p] then begin
if i=1 then add(i,b[p],maxl)
else
add(i+n,b[p],maxl);
end;
p:=next[p];
end;
end;
for i:=1 to n do begin
readln(now);
if (i=1)or(n=1) then continue;
add(i,i+n,now);
end;
end;
function min(l,r:int64):int64;
begin
if l<r then exit(l);exit(r);
end;
function sap(x:longint;y:int64):int64;
var minh,p,t:longint;
begin
if x=n then exit(y);
minh:=ni+1;p:=las[x];
while p<>0 do begin
if f[p]>0 then begin
if h[x]=h[b1[p]]+1 then begin
t:=sap(b1[p],min(y,f[p]));
if t>0 then begin
f[p]:=f[p]-t;
f[re[p]]:=f[re[p]]+t;
exit(t);
end;
if h[1]>ni then exit(0);
end;
minh:=min(minh,h[b1[p]]+1);
end;
p:=nex[p];
end;
dec(vh[h[x]]);
if vh[h[x]]=0 then h[1]:=ni+1;
h[x]:=minh;
inc(vh[h[x]]);
exit(0);
end;
begin
assign(input,'network.in');reset(input);
assign(output,'network.out');rewrite(output);
readln(n,m);
for i:=1 to m do begin
readln(x,y,z);
insert(x,y,z);
insert(y,x,z);
end;
spfa;
ni:=n*2;vh[0]:=ni;
while h[1]<=ni do
ans:=ans+sap(1,maxl);
writeln(ans);
end.