Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

求纠错

Posted by CrazyMichael at 2015-04-12 16:15:00 on Problem 3469
#include "stdio.h"
#define maxn 20012
#define maxm 200012
#define inf 200000000
struct hp
{
  int c,f,next,to;
}edge[5*maxm];
int head[2*maxn+2],flag[2*maxn+2],level[2+2*maxn],n,m,vs,vt,m1;
int bfs()
{
  int u,v,front,rear,queue[maxn],i;
  memset(flag,0,sizeof(flag));
  flag[vs]=1;
  level[vs]=0;
  front=-1;
  rear=0;
  queue[rear]=vs;
  while(front<rear)
  {
    front++;
    u=queue[front];
    if(u==vt) return 1;/*找到vt*/
    for(i=head[u];i!=-1;i=edge[i].next)
    {
      v=edge[i].to;
      if(!flag[v] && edge[i].c>edge[i].f)
      {
        flag[v]=1;
        level[v]=level[u]+1;
        rear++;
        queue[rear]=v;
      }
    }
  }
  return 0;
}
int min(int a,int b)
{
  if(a<b) return a;
  return b;
}
int dfs(int u,int a)
{
  int i,flow,v,sum=a;
  if(u==vt) return a;
  for(i=head[u];i!=-1;i=edge[i].next)
  {
    v=edge[i].to;
    if(level[v]==level[u]+1 && edge[i].f<edge[i].c)
    {
      flow=dfs(v,min(a,edge[i].c-edge[i].f));
      edge[i].f+=flow;
      edge[i-1].f-=flow;
      a-=flow;
    }
  }
  if(sum==a) level[u]=-1;
  return sum-a;
}
void dinic()
{
  while(bfs())
  dfs(vs,inf);/*dfs(v0,a)*/
}
void build(int u,int v,int c)
{
  edge[m1].c=c;
  edge[m1].to=v;
  edge[m1].next=head[u];
  head[u]=m1;
  m1++;
  edge[m1].c=0;
  edge[m1].to=u;
  edge[m1].next=head[v];
  head[v]=m1;
  m1++;
}
void out()
{
  int i,maxflow=0;
  for(i=head[vs];i!=-1;i=edge[i].next)
  maxflow+=edge[i].f;
  printf("%d\n",maxflow);
}
void init()
{
  int i,a,b,u,v,c;
  while(scanf("%d%d",&n,&m)!=EOF)
  {
    vs=0;
    vt=2*n+1;
    memset(head,0xff,sizeof(head));
    memset(edge,0,sizeof(edge));
    for(i=0;i<=2*m-1;i++)
      edge[i].next=-1;
    for(i=1;i<=n;i++)
    {
      build(i,i+n,inf);
      scanf("%d%d",&a,&b);
      build(vs,i,a);
      build(i+n,vt,b);
    }
    for(i=1;i<=m;i++)
    {
      scanf("%d%d%d",&u,&v,&c);
      build(u,v+n,c);
      build(v,u+n,c);
    }
    dinic();
    out();
  }
}
int main()
{
  init();
  return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator