TyvjP1413 費用流模板裸題

2022-04-10 00:47:36 字數 2503 閱讀 1578

拆點即可。

在乙個寬m,長n的矩陣中,請你編乙個程式,n次從矩陣的左上角走到矩陣的右下角,每到一處,就取走該處的數字,請你選擇一

種走法使取得的數字的和最大,並輸出其最大值。其中:3<=m<=20   m<=n<=100   1<=n<=10 

測試資料 #1: accepted, time=15ms, mem=1154kb, score=10

測試資料 #2: accepted, time=0ms, mem=1154kb, score=10

測試資料 #3: accepted, time=15ms, mem=1154kb, score=10

測試資料 #4: accepted, time=0ms, mem=1154kb, score=10

測試資料 #5: accepted, time=15ms, mem=1154kb, score=10

測試資料 #6: accepted, time=0ms, mem=1154kb, score=10

測試資料 #7: accepted, time=0ms, mem=1154kb, score=10

測試資料 #8: accepted, time=0ms, mem=1158kb, score=10

測試資料 #9: accepted, time=0ms, mem=1150kb, score=10

測試資料 #10: accepted, time=0ms, mem=1150kb, score=10

time = 45ms mem = 1158kb score= 100

program p1413;

const

inf=10000000;

type

rec=record

s,e,c,w,flow,next:longint;

end;

var v:array[0..4002] of boolean;

a:array[0..8002] of rec;

b,q,d,cur:array[0..4002] of longint;

n,m,k,i,j,st,tar,va,top:longint;

function poi2num(x,y:longint;isin:boolean):longint;inline;//num 1~2mn

begin

exit((x-1)*m*2+(y-1)*2+2-ord(isin));

end;

function min(a,b:longint):longint;inline;begin if aopen do

begin

close:=close mod n+1;

x:=q[close];

u:=b[x];

while u<>b[0] do

begin

if (d[a[u].e]st do

begin

delta:=min(delta,a[cur[u]].w-a[cur[u]].flow);

u:=y;

y:=a[cur[y]].s;

end;

u:=tar;

y:=a[cur[u]].s;

while u<>st do

begin

inc(a[cur[u]].flow,delta);

inc(addflow,a[cur[u]].c*delta);

dec(a[cur[u] xor 1].flow,delta);

u:=y;

y:=a[cur[y]].s;

end;

end;

function costflow:longint;

begin

costflow:=0;

while spfa do

inc(costflow,addflow);

end;

begin

fillchar(b,sizeof(b),$ff);

readln(k,m,n); top:=-1;

for i:=1 to n do

begin

for j:=1 to m do

begin

read(va);

add(poi2num(i,j,true),poi2num(i,j,false),1,va);

if (i=n) and (j=m) then add(poi2num(n,m,true),poi2num(n,m,false),k-1,0) else

if (i=1) and (j=1) then add(poi2num(1,1,true),poi2num(1,1,false),k-1,0) else

add(poi2num(i,j,true),poi2num(i,j,false),inf,0);

if iif jend;

readln;

end;

st:=1;tar:=2*m*n;

n:=2*m*n;

writeln(costflow);

end.

費用流模板

const int oo 1e9 無窮 const int mm 11111111 邊 const int mn 888888 點 int node,src,dest,edge int ver mm flow mm cost mm nex mm int head mn dis mn p mn q m...

費用流(模板)

費用流模板 spfa不停找當前從s到t的最小費用路,然後重新整理最大流 這裡把每條邊的cost為0,同樣就可以求最大流,不過有點慢 include include include include include include include include include include incl...

費用流模板

我借鑑了一些大神的 也就是他們經常用的模板。費用流模板 include includeusing namespace std const int oo 1e9 const int mm 11111 const int mn 888 int node,src,dest,edge int ver mm ...