POJ3469 代码

Source Code

Problem: 3469 User: forsona
Memory: 14596K Time: 3329MS
Language: C++ Result: Accepted

  • Source Code
    #include"iostream"
    #include"cstdio"
    #include"algorithm"
    #define INF 999999999
    using namespace std;
    struct edge
    {
      int p,q,maxf,f;       
    }e[1000010];
    int ep,map[1000020][2],mp,n,m;
    int h[20010],current[20010],num[40050],so,to,pre[20010],from[20010];
    int que[20010],more[20010];
    void Add(int p,int q,int maxf)
    {
      e[ep].p=p;
      e[ep].q=q;
      e[ep].maxf=maxf;
      e[ep].f=0;
      map[mp][0]=ep;
      map[mp][1]=map[p][1];
      map[p][1]=mp++;
      map[mp][0]=ep;
      map[mp][1]=map[q][1];
      map[q][1]=mp++;
      ep++;
    }
    void Inp(void)
    {
      int i,j,k,t;
      scanf("%d%d",&n,&m);
      so=0;
      to=n+1;
      mp=to+1;
      for(i=1;i<=n;i++)
        {
        scanf("%d%d",&j,&k);
        Add(so,i,j);
        Add(i,to,k);               
        }   
      for(i=0;i<m;i++)
        {
        scanf("%d%d%d",&j,&k,&t);              
        Add(j,k,t);
        Add(k,j,t);
        }  
    }
    void Greedy(void)
    {
      int t,ep,delta,p,q,qbeg,qend;
      for(t=map[so][1];t;t=map[t][1])
        {
        ep=map[t][0];
        more[so]=e[ep].maxf;
        }
      qbeg=0;
      qend=1;
      que[0]=so;  
      while(qbeg<qend)
        {
        p=que[qbeg++];
        for(t=map[p][1];t;t=map[t][1])
          {
          ep=map[t][0];
          if (e[ep].p==p&&h[e[ep].q]==h[p]-1)
            {
            delta=min(more[p],e[ep].maxf-e[ep].f);
            e[ep].f+=delta;
            more[e[ep].q]+=delta;
            more[p]-=delta;
            que[qend++]=e[ep].q;
            if (!more[p])                                
              break;
            }                             
          }              
        }   
      for(qbeg=qend-1;qbeg>0;qbeg--)                 
        {
        p=que[qbeg];
        if (p==to)
          continue;                                             
        for(t=map[p][1];t;t=map[t][1])
          {
          ep=map[t][0];
          if (e[ep].q==p&&h[e[ep].p]==h[p]+1)
            {
            delta=min(more[p],e[ep].f);
            e[ep].f-=delta;
            more[e[ep].p]+=delta;
            more[p]-=delta;
            if (!more[p])
              break;                                 
            }                            
          }  
        }
    }
    void BFS(void)
    {
      int qbeg,qend,i,t,p,ep;  
      for(i=so;i<to;i++)
        h[i]=to+1;
      h[to]=0;  
      qbeg=0;
      qend=1;
      que[qbeg]=to;
      num[0]++;
      while(qbeg<qend)
        {
        p=que[qbeg++];
        for(t=map[p][1];t;t=map[t][1])
          {
          ep=map[t][0];   
          if (e[ep].q==p&&e[ep].maxf)
            if (h[e[ep].p]>h[p]+1)
              {
              h[e[ep].p]=h[p]+1;    
              que[qend++]=e[ep].p;
              }                         
          }              
        }   
      for(i=so;i<=to;i++)
        num[h[i]]++;  
    }
    void Argument(void)
    {
      int p,ep,mt=INF;
      for(p=to;p!=so;)
        {
        ep=from[p];
        mt=min(mt,e[ep].maxf-e[ep].f);
        p=e[ep].p;             
        }     
      for(p=to;p!=so;)
        {
        ep=from[p];
        e[ep].f+=mt;
        p=e[ep].p;       
        }      
    }
    bool ReLabel(int p)
    {
      int oh=h[p],t,ep;
      h[p]=to+1;
      for(t=map[p][1];t;t=map[t][1])
        {
        ep=map[t][0];
        if (e[ep].p==p&&e[ep].maxf>e[ep].f)
          h[p]=min(h[p],h[e[ep].q]+1);                            
        }   
      num[oh]--;
      num[h[p]]++;
      current[p]=map[p][1];
      if (num[oh])
        return true;
      else
        return false;    
    }
    void SAP(void)
    {
      int t,ep,i,p,r=0;
      for(i=so;i<=to;i++)
        current[i]=map[i][1];
      BFS();
      Greedy();
      num[0]=to+1;
      p=so;
      while(h[so]!=to+1) 
        {
        start:
        if (p==to)
          {
          Argument();
          p=so;
          }
        while(current[p])                 
          {
          ep=map[current[p]][0];
          if (e[ep].p==p&&e[ep].maxf>e[ep].f&&h[p]==h[e[ep].q]+1)
            {
            pre[e[ep].q]=p;
            from[e[ep].q]=ep;
            p=e[ep].q;
            goto start;                                                     
            }
          current[p]=map[current[p]][1];  
          }
        if (!ReLabel(p))
          break;
        if (p!=to)
          p=pre[p];  
        }     
      for(t=map[so][1];t;t=map[t][1])
        {
        ep=map[t][0];
        r+=e[ep].f;                             
        }  
      printf("%d/n",r);  
    }
    int main()
    {
      Inp();
      SAP();
      system("pause");
      return 0;    
    }
    
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值