POJ 1459(预留推进)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nike0good/article/details/7866210

hllp写到最后写成预留推进了……


Program P1459;
var
   n,np,nc,m,i,j,src,t,level:longint;
   ch:char;
   s:string;
   d,e,pre:array[-1..100] of longint;
   map,f:array[-1..100,-1..100] of longint;
   queue:array[1..102] of longint;
   list:array[0..103,0..102] of longint;
   b:array[-1..100] of boolean;
function min(a,b:longint):longint;
begin
   if a<b then exit(a) else exit(b);
end;
function max(a,b:longint):longint;
begin
   if a>b then exit(a) else exit(b);
end;

procedure hllp;
var
   i,j,maxflow,flow,minj:longint;
   h,t:longint;
   tag:boolean;
begin
   level:=0;
   maxflow:=0;
   flow:=0;
   h:=1;
   t:=1;
   queue[h]:=n;
   while h<=t do
   begin
      for i:=0 to n-1 do
         if (map[i,queue[h]]>0) and (not(b[i])) then
         begin
            inc(t);
            queue[t]:=i;
            d[i]:=d[queue[h]]+1;
            b[i]:=true;
         end;
      inc(h);
   end;
   d[-1]:=n+2;
   {
   for i:=0 to n-1 do
      if e[i]>0 then
      begin
         inc(list[d[i],0]);
         list[d[i],list[d[i],0]]:=i;
         level:=max(level,d[i]);
      end;
   }
   while (true) do
   begin
      i:=n;
      for j:=0 to n-1 do
      begin
         if (e[j]>0) and (d[j]>d[i]) then i:=j;
      end;
      if i=n then break;
 {     i:=list[level,list[level,0]];
      dec(list[level,0]);
      while (level>0) and (list[level,0]=0) do dec(level);
      }

      tag:=false;
      for j:=-1 to n do
      begin
         if e[i]=0 then break;
            if (d[i]=d[j]+1) and (map[i,j]-f[i,j]>0) then
            begin
               tag:=true;
               flow:=min(map[i,j]-f[i,j],e[i]);
               dec(e[i],flow);
               inc(e[j],flow);
               inc(f[i,j],flow);
               f[j,i]:=-f[i,j];
            end;
      end;
      if (e[i]>0) then
      begin
         minj:=maxlongint;
         for j:=-1 to n do
            if (map[i,j]-f[i,j]>0) {and (d[i]>=d[j])} then minj:=min(minj,d[j]);
        { if minj=maxlongint then
         begin
            e[i]:=0;
            continue;
         end;    }
         d[i]:=minj+1;
      end;

   end;

end;
function isdight(a:char):boolean;
begin
   if (48<=ord(a)) and (ord(a)<=57) then exit(true) else exit(false);
end;
procedure rea(var a:longint);
var
   ch:char;
   s:string;
begin
   ch:=' ';
   while not(isdight(ch)) do read(ch);
   s:='';
   repeat
      s:=s+ch;
      read(ch);
   until not(isdight(ch));
   val(s,a);
end;
begin
{   assign(input,'p1459.in');
   assign(output,'p1459.out');
   reset(input);
   rewrite(output);                    }
   while not seekeof do
   begin
      fillchar(map,sizeof(map),0);
      fillchar(e,sizeof(e),0);
      fillchar(f,sizeof(f),0);
      fillchar(list,sizeof(list),0);
      fillchar(d,sizeof(d),0);
      fillchar(b,sizeof(b),false);
      rea(n);
      for i:=-1 to n do pre[i]:=i;
      rea(np);
      rea(nc);
      rea(m);
      for i:=1 to m do
      begin
        rea(src);
        rea(t);
        rea(map[src,t]);
      end;
      for i:=1 to np do
      begin
        rea(t);
        rea(map[-1,t]);
        e[t]:=map[-1,t];
        f[-1,t]:=map[-1,t];
        f[t,-1]:=-map[-1,t];
      end;
      for i:=1 to nc do
      begin
         rea(src);
         rea(map[src,n]);
      end;

      hllp;
      writeln(e[n]);
   end;
{   close(input);
   close(output);     }
end.


展开阅读全文

POJ 1459 , MLE, 求解答,郁闷

06-02

就是一个最大流的基本问题,怎么会是 Memory Limit Exceed,怎么调都找不出来,谢谢了,测试样例通过。rn[code=c]#include rn#include rn#include rn#include rnusing namespace std;rnrnconst int maxNum = 100;rnint poitMatrix[maxNum + 2][maxNum + 2];rnint flowMatrix[maxNum + 2][maxNum + 2];rnint h[maxNum + 2];rnint e[maxNum + 2];rnint mySum;rnint n, np, nc, m;rnqueue nodeQ;rnrnstruct nodeLinkrnrn int num;rn nodeLink* next;rn;rnrnnodeLink* nodes[maxNum + 2];rnrnvoid initPreflow(int n)rnrn h[0] = n + 2;rn for(int i = 0; i != n + 2; ++i)rn if(poitMatrix[0][i] > 0)rn flowMatrix[0][i] = poitMatrix[0][i];rn flowMatrix[i][0] = - poitMatrix[0][i];rn e[i] = poitMatrix[0][i];rn nodeQ.push(i);rn e[0] -= poitMatrix[0][i];rn rn rnrnrnvoid push(int u, int v)rnrn int df = min(e[u], poitMatrix[u][v] - flowMatrix[u][v]);rn flowMatrix[u][v] += df;rn flowMatrix[v][u] = -flowMatrix[u][v];rn e[u] -= df;rn e[v] += df;rn if(v == n + 1)rn mySum += df;rn rnrnrnvoid relabel(int u)rnrn ++h[u];rnrnrnvoid push_relabel()rnrnrn int tmpn;rn while(!nodeQ.empty())rn tmpn = nodeQ.front();rn nodeQ.pop();rnrn int flag = 0;rnrn for(int i = 0; i != n + 2; ++i)rn if(h[tmpn] == h[i] + 1 && rn poitMatrix[tmpn][i] > flowMatrix[tmpn][i])rn push(tmpn, i);rn if(e[i] > 0 && i != n + 1)rn nodeQ.push(i);rn rn flag = 1;rn rn //end forrnrn if(e[tmpn] > 0)rn if(flag == 0)rn relabel(tmpn);rn rn nodeQ.push(tmpn);rn rn //cout << tmpn << ' ' << mySum << endl;rn //for(int i = 0; i != n + 2; ++i)rn // cout << e[i] << ' ';rn //rn //cout << endl << ' ';rn //for(int i = 0; i != n + 2; ++i)rn // cout << h[i] << ' ';rn //rn //cout << endl << endl;rn //cout << flowMatrix[4][8] << endl;rn //system("pause");rn //end whilernrnrnint main()rnrn //memset(nodes, 0, sizeof(nodes));rnrn while(cin >> n >> np >> nc >> m)rn mySum = 0;rn memset(poitMatrix, 0, sizeof(poitMatrix));rn memset(flowMatrix, 0, sizeof(flowMatrix));rn memset(e, 0, sizeof(e));rn memset(h, 0, sizeof(h));rnrn string tmpstr;rn int tmpn = 0;rn int prePoint = 0;rn int nxtPoint = 0;rn int flow = 0;rnrn while(m--)rn cin >> tmpstr;rn for(int i = 1; i != tmpstr.size(); ++i)rn if(tmpstr[i] == ',')rn prePoint = tmpn;rn tmpn = 0;rn else if(tmpstr[i] == ')')rn nxtPoint = tmpn;rn tmpn = 0;rn elsern tmpn = (tmpn * 10 + (tmpstr[i] - '0'));rn rnrn //end forrn flow = tmpn;rn tmpn = 0;rn poitMatrix[prePoint + 1][nxtPoint + 1] = flow;rnrn //end whilernrn while(np--)rn cin >> tmpstr;rn for(int i = 1; i != tmpstr.size(); ++i)rn if(tmpstr[i] == ')')rn nxtPoint = tmpn;rn tmpn = 0;rn elsern tmpn = tmpn * 10 + (tmpstr[i] - '0');rn rnrn rn flow = tmpn;rn tmpn = 0;rn poitMatrix[0][nxtPoint + 1] = flow;rnrn //end whilern while(nc--)rn cin >> tmpstr;rn for(int i = 1; i != tmpstr.size(); ++i)rn if(tmpstr[i] == ')')rn prePoint = tmpn;rn tmpn = 0;rn elsern tmpn = tmpn * 10 + (tmpstr[i] - '0');rn rnrn rn flow = tmpn;rn tmpn = 0;rn poitMatrix[prePoint + 1][n + 1] = flow;rnrn //end whilern initPreflow(n);rn push_relabel();rn cout << mySum << endl;rn rn //system("pause");rn return 0;rn[/code] 论坛

没有更多推荐了,返回首页