可当模板
sto[]与sto1[]存的是最小割的两部分点
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=1e3+10;
int q,k,k1;
struct point{
int fr,to,next,val;
}pt[maxn];
int head[maxn],dep[maxn],vis[maxn],sto[maxn],sto1[maxn];
void init()
{
q=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int val)
{
pt[q].next=head[u];
pt[q].fr=u;
pt[q].to=v;
pt[q].val=val;
head[u]=q++;
}
int que[maxn];
int bfs(int start,int end)
{
int front,rear;
front=rear=0;
memset(dep,-1,sizeof(dep));
que[rear++]=start;
dep[start]=0;
while (front!=rear)
{
int u=que[front++];
if (front==maxn) front=0;
for (int i=head[u];i!=-1;i=pt[i].next)
{
int v=pt[i].to;
if (pt[i].val>0&&dep[v]==-1)
{
dep[v]=dep[u]+1;
que[rear++]=v;
if (rear>=maxn)
rear=0;
if (v==end)
return 1;
}
}
}
return 0;
}
int stack[maxn],cur[maxn];
int dinic(int start,int end)
{
int res=0;
int top;
while (bfs(start,end))
{
//cout<<"$$$$$$"<<endl;
memcpy(cur,head,sizeof(head));
int u=start;
top=0;
while (1)
{
if (u==end)
{
int min=inf;
int po;
for (int i=0;i<top;i++)
if (min>pt[stack[i]].val)
{
min=pt[stack[i]].val;
po=i;
}
for (int i=0;i<top;i++)
{
pt[stack[i]].val-=min;
pt[stack[i]^1].val+=min;
}
res+=min;
top=po;;
u=pt[stack[top]].fr;
}
for (int i=cur[u];i!=-1;cur[u]=i=pt[i].next)
if (pt[i].val!=0&&dep[u]+1==dep[pt[i].to])
break;
if (cur[u]!=-1)
{
stack[top++]=cur[u];
u=pt[cur[u]].to;
}
else
{
if (top==0)
break;
dep[u]=-1;
u=pt[stack[--top]].fr;
}
}
}
return res;
}
void dfs(int st,int f)
{
for (int i=head[st];i!=-1;i=pt[i].next)
{
int to=pt[i].to;
if (!vis[to]&&pt[i].val>0)
{
if (f)
sto[k++]=to;
else
sto1[k1++]=to;
vis[to]=1;
dfs(to,f);
}
}
}
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,m,c,s,t,u,v;
while (cin>>n>>m)
{
memset(vis,0,sizeof(vis));
k=k1=0;
init();
for (int i=1;i<=n;i++)
{
cin>>u>>v>>c;
add(u,v,c);
add(v,u,0);
}
cout<<dinic(1,m)<<endl;
vis[1]=1;
dfs(1,1);
vis[m]=1;
dfs(m,0);
for (int i=0;i<k;i++)
cout<<sto[i]<<"$$$$"<<endl;
for (int i=0;i<k1;i++)
cout<<sto1[i]<<endl;
}
return 0;
}
4279

被折叠的 条评论
为什么被折叠?



