记录编号 122488 评测结果 AAAAAAAAAA
题目名称 最小最大生成树 最终得分 100
用户昵称 GravatarHouJikan 是否通过 通过
代码语言 C++ 运行时间 0.453 s
提交时间 2014-09-23 21:04:18 内存使用 0.85 MiB
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <set>
#include <list>
#include <iterator>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <functional>
#include <deque>
#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=0x7fffffff;
const double eps=1e-6;
using namespace std;
//================struct declaration======================
struct adj
{
  int from,to,cap,flow,dist;
  adj(int from,int to,int cap,int flow,int dist):from(from),to(to),cap(cap),flow(flow),dist(dist){}
};
//================var declaration=-========================
const int MAXN=20010;
vector <adj> Edge;
vector <int> G[MAXN];
int n,m,flow=0,tot=0;
int s,e,l;
int gap[MAXN],pa[MAXN],cur[MAXN],d[MAXN];
//================function declaration====================
void bfs();
void isap1();
void isap2();
int argment();
//================main code===============================
int main()
{
  string ProgrammeName="mstmn";
  string FloderName="COGS";
  freopen((ProgrammeName+".in").c_str(),"r",stdin);
  freopen((ProgrammeName+".out").c_str(),"w",stdout);
#ifdef DEBUG
  clock_t Start_Time=clock();
   system(("cp C:\\Users\\Administrator\\Desktop\\"+FloderName+"\\standard.cpp C:\\Users\\Administrator\\Desktop\\"+FloderName+"\\submit.txt").c_str());
#endif
  scanf("%d%d",&n,&m);
  For(i,1,m)
  {
    int s,e,l;
    scanf("%d%d%d",&s,&e,&l);
    Edge.push_back(adj(s,e,1,0,l));
    G[s].push_back(tot++);
    Edge.push_back(adj(e,s,1,0,l));
    G[e].push_back(tot++);
  }
  scanf("%d%d%d",&s,&e,&l);
  isap1();
  isap2();
  printf("%d\n",flow);
#ifdef DEBUG
  clock_t End_Time=clock();
  printf("\n\nTime Used :%.6lf Ms\n",double(Start_Time-End_Time)/(-CLOCKS_PER_SEC));
#endif
  return 0;
}
//================function code===========================
void bfs()
{
  memset(d,-1,sizeof(d));
  memset(gap,0,sizeof(gap));
  d[e]=0;
  queue <int> Q;
  Q.push(e);
  while (!Q.empty())
  {
    int x=Q.front();Q.pop();
    int siz=G[x].size()-1;
    For(i,0,siz)
    {
      adj &e=Edge[G[x][i]];
      if (d[e.to]==-1)
      {
        gap[d[e.to]=d[x]+1]++;
        Q.push(e.to);
      }
    }
  }
}
void isap2()
{
  int x=s;
  memset(cur,0,sizeof(cur));
  bfs();
  while (d[s]<=n)
  {
     if (x==e)
     {
       flow+=argment();
       x=s;
     }
     bool suc=false;
     int siz=G[x].size()-1;
     for(int i=cur[x];i<=siz;i++)
     {
       adj &e=Edge[G[x][i]];
       if (e.dist>=l) continue;
       if (e.cap>e.flow&&d[e.to]==d[x]-1)
       {
         cur[x]=i;
         pa[e.to]=G[x][i];
         suc=true;
         x=e.to;
         break;
       }
     }
     if (!suc)
     {
       int mind=n;
       For(i,0,siz)
       {
         adj &e=Edge[G[x][i]];
         if (e.dist>=l) continue;
         if (e.cap<=e.flow) continue;
         if (d[e.to]<mind)
           mind=d[e.to];
       }
       if (--gap[d[x]]==0) break;
       gap[d[x]=mind+1]++;
       cur[x]=0;
       if (x!=s)
         x=Edge[pa[x]].from;
     }
  }
}
void isap1()
{
  int x=s;
  memset(cur,0,sizeof(cur));
  bfs();
  while (d[s]<=n)
  {
    if (x==e)
    {
      flow+=argment();
      x=s;
    }
     bool suc=false;
     int siz=G[x].size()-1;
     for(int i=cur[x];i<=siz;i++)
     {
       adj &e=Edge[G[x][i]];
       if (e.dist<=l) continue;
       if (e.cap>e.flow&&d[e.to]==d[x]-1)
       {
         cur[x]=i;
         pa[e.to]=G[x][i];
         suc=true;
         x=e.to;
         break;
       }
     }
     if (!suc)
     {
       int mind=n;
       For(i,0,siz)
       {
         adj &e=Edge[G[x][i]];
         if (e.dist<=l) continue;
         if (e.cap<=e.flow) continue;
         if (d[e.to]<mind)
           mind=d[e.to];
       }
       if (--gap[d[x]]==0) break;
       gap[d[x]=mind+1]++;
       cur[x]=0;
       if (x!=s)
         x=Edge[pa[x]].from;
     }
  }
}
int argment()
{
  int x=e;
  while (x!=s)
  {
    Edge[pa[x]].flow++;
    Edge[pa[x]^1].flow--;
    x=Edge[pa[x]].from;
  }
  return 1;
}