codevs 蚯蚓的遊戲問題

題目描述 Description

在一塊梯形田地上,一羣蚯蚓在做收集食物遊戲。蚯蚓們把梯形田地上的食物堆積整理如下:

                                                 a(1,1)  a(1,2)…a(1,m)

                                          a(2,1)  a(2,2)  a(2,3)…a(2,m)  a(2,m+1)     

                                     a(3,1)  a (3,2)  a(3,3)…a(3,m+1)  a(3,m+2)

                             ……  

                                   a(n,1)   a(n,2)   a(n,3)…           a(n,m+n-1)     

       它們把食物分成n行,第1行有m堆的食物,每堆的食物量分別是a(1,1),a(1,2),…,a(1,m);

第2行有m+1堆食物,每堆的食物量分別是a(2,1),a(2,2),…,  a(2,m+1);以下依次有m+2堆、m+3堆、…m+n-1堆食物。

現在蚯蚓們選擇了k條蚯蚓來測試它們的合作能力(1≤ k ≤m)。測試法如下:第1條蚯蚓從第1行選擇一堆食物,然後往左下或右下爬,並收集1堆食物,例如從a(1,2)只能爬向a(2,2) 或a(2,3),而不能爬向其它地方。接下來再爬向下一行收集一堆食物,直到第n行收集一堆食物。第1條蚯蚓所收集到的食物量是它在每一行所收集的食物量之和;第2條蚯蚓也從第1行爬到第n行,每行收集一堆食物,爬的方法與第1條蚯蚓相類似,但不能碰到第1條蚯蚓所爬的軌跡;一般地,第i 條蚯蚓從第1行爬到第 n行,每行收集一堆食物,爬的方法與第1條蚯蚓類似,但不能碰到前 I-1 條蚯蚓所爬的軌跡。這k條蚯蚓應該如何合作,才能使它們所收集到的食物總量最多?收集到的食物總量可代表這k條蚯蚓的合作水平。

  • Ø編程任務:

       給定上述梯形m、n和k的值(1≤k≤m≤30;1≤n≤30)以及梯形中每堆食物的量(小於10的非整數),編程計算這k條蚯蚓所能收集到的食物的最多總量。

輸入描述 Input Description

       輸入數據由文件名爲INPUT1.*的文本文件提供,共有n+1行。每行的兩個數據之間用一個空格隔開。

        ●第1行是n、m和k的值。

  • 接下來的n行依次是梯形的每一行的食物量a(i,1),a(i,2),…,a(i,m+i-1),i=1,2,…,n。
分析:

將第一排與源點連接 最後一排與匯點連接

對於每個點拆點 中間連一條流量 1 費用 方格里的數 的邊

注意連別的邊的時候流量要置爲 1 因爲每個格子只能經過一次而不是k次


代碼:

type

 bi=record

  next,cost,flow,go:longint;

end;

var

 n,k,i,j,x,a,xb,t,w,ans,p,m,po:longint;

 e:array[0..200000] of bi;

 h,ll:array[0..60000] of longint;

 b:array[0..60000] of boolean;

 f,l,mi,r:array[0..60000] of longint;

 q:array[0..10000000] of longint;

procedure adde(a,b,c,d:longint);

begin

 inc(xb);

 e[xb].cost:=d;

 e[xb].flow:=c;

 e[xb].go:=b;

 if h[a]=0 then

 begin

  h[a]:=xb;

  ll[a]:=xb

 end else

 begin

  e[ll[a]].next:=xb;

  ll[a]:=xb

 end

end;

procedure cl(x:longint);

begin

 if (x<=m)or(x=xb) then exit;

 if odd(x-m) then inc(e[x+1].flow,mi[po+2])

  else inc(e[x-1].flow,mi[po+2])

end;

begin


 readln(n,m,k);

 for i:=1 to m do adde(0,2*i-1,1,0);

 po:=(m shl 1+n-1)*n;

 for i:=1 to n do


  for j:=1 to m+i-1 do

  begin

   read(x);

   a:=(m+m+i-2)*(i-1) shr 1+j;

   adde(2*a-1,2*a,1,x);

   adde(2*a,2*a-1,0,-x);

   if i<n then

   begin

    addE(2*a,2*a+(m+i-1)*2-1,1,0);

    addE(2*a+(m+i-1)*2-1,2*a,0,0);

    addE(2*a,2*a+(m+i-1)*2+1,1,0);

    addE(2*a+(m+i-1)*2+1,2*a,0,0);

   end else

   begin

    addE(2*a,po+1,1,0);

    addE(po+1,2*a,0,0)

   end

  end;

 adde(po+1,po+2,k,0);


 repeat

  fillchar(b,sizeof(b),false);

  b[0]:=true;

  t:=0;

  w:=1;

  for i:=0 to po+2 do

  begin

   f[i]:=-100000000;

   mi[i]:=-f[i]

  end;


  for i:=0 to po+2 do l[i]:=-1;

  fillchar(r,sizeof(r),0);

  q[1]:=0;

  f[0]:=0;

  while t<w do

  begin

   inc(t);

   b[q[t]]:=false;

   p:=h[q[t]];

   while p<>0 do

   begin

    if (f[e[p].go]<f[q[t]]+e[p].cost)and(e[p].flow>0) then

    begin

     f[e[p].go]:=f[q[t]]+e[p].cost;

     l[e[p].go]:=q[t];

     r[e[p].go]:=p;

     if e[p].flow<mi[q[t]] then mi[e[p].go]:=e[p].flow

      else mi[e[p].go]:=mi[q[t]];

     if not b[e[p].go] then

     begin

      inc(w);

      b[e[p].go]:=true;

      q[w]:=e[p].go

     end;

    end;

    p:=e[p].next

   end

  end;

  j:=po+2;

  if f[j]=-100000000 then break;

  inc(ans,f[j]*mi[j]);

  while l[j]<>-1 do

  begin


   dec(e[r[j]].flow,mi[po+2]);

   cl(r[j]);

   j:=l[j]

  end


 until false;

 writeln(ans)

end.


type

 bi=record

  next,cost,flow,go:longint;

end;

var

 n,k,i,j,x,a,xb,t,w,ans,p,m,po:longint;

 e:array[0..200000] of bi;

 h,ll:array[0..60000] of longint;

 b:array[0..60000] of boolean;

 f,l,mi,r:array[0..60000] of longint;

 q:array[0..10000000] of longint;

procedure adde(a,b,c,d:longint);

begin

 inc(xb);

 e[xb].cost:=d;

 e[xb].flow:=c;

 e[xb].go:=b;

 if h[a]=0 then

 begin

  h[a]:=xb;

  ll[a]:=xb

 end else

 begin

  e[ll[a]].next:=xb;

  ll[a]:=xb

 end

end;

procedure cl(x:longint);

begin

 if (x<=m)or(x=xb) then exit;

 if odd(x-m) then inc(e[x+1].flow,mi[po+2])

  else inc(e[x-1].flow,mi[po+2])

end;

begin


 readln(n,m,k);

 for i:=1 to m do adde(0,2*i-1,1,0);

 po:=(m shl 1+n-1)*n;

 for i:=1 to n do


  for j:=1 to m+i-1 do

  begin

   read(x);

   a:=(m+m+i-2)*(i-1) shr 1+j;

   adde(2*a-1,2*a,1,x);

   adde(2*a,2*a-1,0,-x);

   if i<n then

   begin

    addE(2*a,2*a+(m+i-1)*2-1,1,0);

    addE(2*a+(m+i-1)*2-1,2*a,0,0);

    addE(2*a,2*a+(m+i-1)*2+1,1,0);

    addE(2*a+(m+i-1)*2+1,2*a,0,0);

   end else

   begin

    addE(2*a,po+1,1,0);

    addE(po+1,2*a,0,0)

   end

  end;

 adde(po+1,po+2,k,0);


 repeat

  fillchar(b,sizeof(b),false);

  b[0]:=true;

  t:=0;

  w:=1;

  for i:=0 to po+2 do

  begin

   f[i]:=-100000000;

   mi[i]:=-f[i]

  end;


  for i:=0 to po+2 do l[i]:=-1;

  fillchar(r,sizeof(r),0);

  q[1]:=0;

  f[0]:=0;

  while t<w do

  begin

   inc(t);

   b[q[t]]:=false;

   p:=h[q[t]];

   while p<>0 do

   begin

    if (f[e[p].go]<f[q[t]]+e[p].cost)and(e[p].flow>0) then

    begin

     f[e[p].go]:=f[q[t]]+e[p].cost;

     l[e[p].go]:=q[t];

     r[e[p].go]:=p;

     if e[p].flow<mi[q[t]] then mi[e[p].go]:=e[p].flow

      else mi[e[p].go]:=mi[q[t]];

     if not b[e[p].go] then

     begin

      inc(w);

      b[e[p].go]:=true;

      q[w]:=e[p].go

     end;

    end;

    p:=e[p].next

   end

  end;

  j:=po+2;

  if f[j]=-100000000 then break;

  inc(ans,f[j]*mi[j]);

  while l[j]<>-1 do

  begin


   dec(e[r[j]].flow,mi[po+2]);

   cl(r[j]);

   j:=l[j]

  end


 until false;

 writeln(ans)

end.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章