POJ3469 代码

来源:互联网 发布:js时间戳怎么用 编辑:程序博客网 时间:2024/06/05 05:41

Source Code

Problem: 3469 User: forsonaMemory: 14596K Time: 3329MSLanguage: C++ Result: Accepted

  • Source Code
    #include"iostream"#include"cstdio"#include"algorithm"#define INF 999999999using 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;    }
原创粉丝点击