Power Network(POJ1459)

原题链接:http://poj.org/problem?id=1459

代码:

#include<stdio.h>
#include<iostream>
#include<queue>
#include<memory.h>
using namespace std;
const int MAX=200+10;
int cap[MAX][MAX];//记录每条路的容量
/*EdmondsKarp(int cap[10001][10001],int s,int t)
 函数说明:cap是记录每条路经容量的二维数组
         s是源的下标
         t是汇的下标
         n是节点的数量
*/
void EdmondsKarp(int s,int t,int n)
{
  int pre[MAX],a[MAX];
  int flow[MAX][MAX];//记录每条路的流量
    int max=0,u,v;
    queue<int> q;
    memset(flow,0,sizeof(flow));
    memset(pre,0,sizeof(pre));
    while(true)
    {
      memset(a,0,sizeof(a));
      a[s]=1000000001;
      q.push(s);
      while(!q.empty())
      {
         u=q.front();
         q.pop();
         for(v=0;v<=n+1;v++)
         {
           if(a[v]==0&&cap[u][v]>flow[u][v])
            {
              pre[v]=u;
              q.push(v);
               if(a[u]>cap[u][v]-flow[u][v])
                a[v]=cap[u][v]-flow[u][v];
              else
                a[v]=a[u];
            }
         }

      }
      if(a[t]==0)
        break;
      for(u=t;u!=s;u=pre[u])
      {
        flow[pre[u]][u]+=a[t];
        flow[u][pre[u]]-=a[t];
      }
      max+=a[t];
    }
    printf("%d\n",max);

}
int main()
{

int n,np,nc,m;
 char ch;
 int a,b,c;
 while(~scanf("%d%d%d%d",&n,&np,&nc,&m))
 {
   memset(cap,0,sizeof(cap));
   for(int i=0;i<m;i++)
   {
     cin>>ch>>a>>ch>>b>>ch>>c;
     cap[a+1][b+1]=c;
   }
   for(int i=0;i<np;i++)
   {
     cin>>ch>>a>>ch>>b;
     cap[0][a+1]=b;
   }
   for(int i=0;i<nc;i++)
   {
     cin>>ch>>a>>ch>>b;
     cap[a+1][n+1]=b;
   }
  EdmondsKarp(0,n+1,n);
 }

}

 

posted @ 2013-05-28 20:00  supersnow0622  Views(145)  Comments(0)    收藏  举报