这道题没弄明白
初始模型很好想,是用到了最小生成树的性质
加入非树边后树上形成的环,非树边一定大于等于任意树边
然后考虑树边一定是缩小,非树边一定是增大
有di+wi>=dj-wj wi+wj>=dj-di(j是加入i形成的环上的边)
然后不知道为什么求∑wi最小就是跑最大费用可行流
求神犇指教
type node=record
po,next,cost,flow:longint;
end; var e:array[..] of node;
v,f:array[..] of boolean;
a:array[..,..] of longint;
d,pre,cur,w,p:array[..] of longint;
q:array[..] of longint;
x,y,len,i,j,n,m,t,k:longint; procedure add(x,y,f,c:longint);
begin
inc(len);
e[len].po:=y;
e[len].next:=p[x];
e[len].cost:=c;
e[len].flow:=f;
p[x]:=len;
end; procedure build(x,y,f,c:longint);
begin
add(x,y,f,c);
add(y,x,,-c);
end; function dfs(x:longint):boolean;
var i:longint;
begin
f[x]:=true;
if x=j then
begin
q[]:=-;
exit;
end;
for i:= to n do
if not f[i] and v[a[x,i]] then
begin
inc(t);
q[t]:=a[x,i];
dfs(i);
if q[]=- then exit;
q[t]:=;
dec(t);
end;
end; function spfa:boolean;
var f,r,y,i,j,x:longint;
begin
f:=;
r:=;
q[]:=;
d[]:=;
for i:= to t do
d[i]:=-;
fillchar(v,sizeof(v),false);
while f<=r do
begin
x:=q[f];
v[x]:=false;
i:=p[x];
while i<>- do
begin
y:=e[i].po;
if e[i].flow> then
if d[y]<d[x]+e[i].cost then
begin
d[y]:=d[x]+e[i].cost;
pre[y]:=x;
cur[y]:=i;
if not v[y] then
begin
v[y]:=true;
inc(r);
q[r]:=y;
end;
end;
i:=e[i].next;
end;
inc(f);
end;
if d[t]>= then exit(true) else exit(false);
end; function maxcost:longint;
var i,j:longint;
begin
maxcost:=;
while spfa do
begin
i:=t;
while i<> do
begin
j:=cur[i];
dec(e[j].flow);
inc(e[j xor ].flow);
i:=pre[i];
end;
maxcost:=maxcost+d[t];
end;
end; begin
len:=-;
fillchar(p,sizeof(p),);
readln(n,m);
for i:= to m do
begin
readln(x,y,w[i]);
a[x,y]:=i;
a[y,x]:=i;
end;
for i:= to n- do
begin
readln(x,y);
k:=a[x,y];
v[k]:=true;
build(,k,,);
end;
for i:= to n do
for j:=i+ to n do
if (a[i,j]>) and not v[a[i,j]] then
begin
build(a[i,j],m+,,);
fillchar(f,sizeof(f),false);
t:=; q[]:=;
dfs(i);
for k:= to t do
if w[q[k]]>w[a[i,j]] then build(q[k],a[i,j],,w[q[k]]-w[a[i,j]]);
end;
t:=m+;
writeln(maxcost);
end.