题目大意:略。
题目分析:网络流模版题。不过数据很弱,只能测很烂的模版。
第一道网络流
详情请见代码:
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 205;
const int M = 410;
const int inf = 0x3f3f3f3f;
struct node
{
int to,next,pre,c,f;
}arc[M];
int num;
int head[N];
int que[N];//bfs用
int sta[N];//保存当前弧
int rpath[N];//保存反向弧
int cnt[N];
int dis[N];
int m,n;
void build(int s,int e,int cap)//建图
{
arc[num].to = e;
arc[num].c = cap;
arc[num].f = 0;
arc[num].next = head[s];
head[s] = num ++;
arc[num - 1].pre = num;//反向弧
arc[num].pre = num - 1;
arc[num].to = s;
arc[num].c = 0;
arc[num].f = 0;
arc[num].next = head[e];
head[e] = num ++;
}
void re_Bfs()
{
int i,front,rear;
front = rear = 0;
for(i = 1;i <= n;i ++)
{
dis[i] = inf;
cnt[i] = 0;
}
que[rear ++] = n;
cnt[0] = 1;
dis[n] = 0;
while(front != rear)
{
int u = que[front ++];
for(i = head[u];i != -1;i = arc[i].next)
{
if(arc[arc[i].pre].c == 0 || dis[arc[i].to] < inf)
continue;
dis[arc[i].to] = dis[u] + 1;
cnt[dis[arc[i].to]] ++;
que[rear ++] = arc[i].to;
}
}
}
int ISAP()
{
int i,u,v,ret = 0;
u = 1;
for(i = 1;i <= n;i ++)
sta[i] = head[i];
while(dis[1] < n)
{
if(u == n)
{
int curflow = inf;
for(i = 1;i != n;i = arc[sta[i]].to)
curflow = min(curflow,arc[sta[i]].c);
for(i = 1;i != n;i = arc[sta[i]].to)
{
arc[sta[i]].c -= curflow;
arc[arc[sta[i]].pre].c += curflow;
arc[sta[i]].f += curflow;
arc[arc[sta[i]].pre].f -= curflow;
}
ret += curflow;
u = 1;
}
for(i = sta[u];i != -1;i = arc[i].next)//寻找允许弧
if(arc[i].c > 0 && dis[arc[i].to] + 1 == dis[u])
break;
if(i != -1)
{
sta[u] = i;
rpath[arc[i].to] = arc[i].pre;
u = arc[i].to;
}
else
{
if((--cnt[dis[u]]) == 0)//gap优化
break;
sta[u] = head[u];
int Min = N;
for(i = head[u];i != -1;i = arc[i].next)
if(arc[i].c > 0)
Min = min(Min,dis[arc[i].to]);
dis[u] = Min + 1;
cnt[dis[u]] ++;
if(u != 1)
u = arc[rpath[u]].to;
}
}
return ret;
}
int main()
{
int i,u,v,c;
while(scanf("%d%d",&m,&n) != EOF)
{
memset(head,-1,sizeof(head));
num = 0;
while(m --)
{
scanf("%d%d%d",&u,&v,&c);
build(u,v,c);
}
re_Bfs();
printf("%d\n",ISAP());
}
return 0;
}