记录编号 117892 评测结果 AAAAAAAAAAAA
题目名称 草地排水 最终得分 100
用户昵称 GravatarHouJikan 是否通过 通过
代码语言 C++ 运行时间 0.005 s
提交时间 2014-09-02 20:59:29 内存使用 3.16 MiB
显示代码纯文本
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>
#include <vector>
#include <ctime>
#include <functional>
#define pritnf printf
#define scafn scanf
#define For(i,j,k) for(int i=(j);i<=(k);(i)++)
using namespace std;
typedef long long LL;
typedef unsigned int Uint; 
const int  INF=0x7ffffff;
//==============struct declaration==============
struct adj
{
  int from,to,cap,flow;
  adj(int from,int to,int cap,int flow):from(from),to(to),cap(cap),flow(flow){}
  adj(){}
};
//==============var declaration=================
const int MAXN=210;
int n,m,sumflow=0;
int d[MAXN];
int cur[MAXN];
int p[MAXN];
int gap[MAXN];
vector <adj> Edge;
vector <int> G[MAXN];
//==============function declaration============
void bfs();
void maxflow();
int argument();
//==============main code=======================
int main()
{  
  string FileName="ditch";//程序名 
  string FloderName="COGS";//文件夹名 
  freopen((FileName+".in").c_str(),"r",stdin);
  freopen((FileName+".out").c_str(),"w",stdout);
#ifdef DEBUG  
  system(("cp C:\\Users\\Administrator\\Desktop\\"+FloderName+"\\standard.cpp C:\\Users\\Administrator\\Desktop\\"+FloderName+"\\submit.txt").c_str());
#endif    
  scanf("%d%d",&m,&n);
  int s,e,f;
  For(i,0,m-1)
  {
    scanf("%d%d%d",&s,&e,&f);
    Edge.push_back(adj(s,e,f,0));
    Edge.push_back(adj(e,s,0,0));
    G[s].push_back(i*2);
    G[e].push_back(i*2+1);
  }
  maxflow();
  printf("%d\n",sumflow);
  return 0;
}
//================fuction code====================
void bfs()
{
   int x,siz;
   queue <int> Q;
   Q.push(n);
   d[n]=0;
   while (!Q.empty())
   {
     x=Q.front();Q.pop();
     gap[d[x]]++;
     siz=G[x].size()-1;
     For(i,0,siz)
     {
       adj &p=Edge[G[x][i]];
       if (d[p.to]==-1)
       {
         d[p.to]=d[x]+1;
         Q.push(p.to);
       }
     }
   }
   return ;
}
void maxflow()
{
  memset(d,-1,sizeof(d));
  memset(cur,0,sizeof(cur));
  memset(gap,0,sizeof(gap));
  bfs();
  int x=1;
  sumflow=0;
  while (d[1]<n)
  {
    if (x==n)
    {
      sumflow+=argument();
      x=1;
    }
    bool suc=false;
    int siz=G[x].size();
    for(int i=cur[x];i<siz;i++)
    {
      adj &e=Edge[G[x][i]];
      if (d[x]==d[e.to]+1&&e.cap>e.flow)
      {
        cur[x]=i;
        p[e.to]=G[x][i];
        x=e.to;
        suc=true;
        break;
      }
    }
    if (!suc)
    {
       int mind=n-1;
       For(i,0,siz-1)
       {
         adj &e=Edge[G[x][i]];
         if (e.cap>e.flow)
           mind=min(mind,d[e.to]);
       }
       gap[d[x]]--;
       if (gap[d[x]]==0) 
         break;
       d[x]=mind+1;
       gap[d[x]]++;
       cur[x]=0;
       if (x!=1)  x=Edge[p[x]].from;
    }
  }
}
int argument()
{
  int u=n;
  int flow=INF;
  while (u!=1)
  {
    adj &x=Edge[p[u]];
    flow=min(flow,x.cap-x.flow);
    u=x.from;
  }
  u=n;
  while (u!=1)
  {
    Edge[p[u]].flow+=flow;
    Edge[p[u]^1].flow-=flow;
    u=Edge[p[u]].from;
  }
  return flow;
}