题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1421
题目大意:求最小割的割边
正常的跑一遍最大流,然后dfs去找能与源点相连的点,最后遍历输入的边来找最小割即可
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxm=10010;
const int maxn=1010;
const int inf=0x3f3f3f3f;
struct Node
{
int from;
int to;
int capa;
int next;
}edge[maxm];
struct LDJ
{
int x,y;
}point[maxn];
int cnt;
int source,sink;
int head[maxn];
int dep[maxn];
bool vis[maxn];
bool cut_vis[maxn];
void init()
{
memset(head,-1,sizeof(head));
memset(cut_vis,false,sizeof(cut_vis));
cnt=0;
return;
}
void add(int u,int v,int capa)
{
edge[cnt].from=u;
edge[cnt].to=v;
edge[cnt].capa=capa;
edge[cnt].next=head[u];
head[u]=cnt++;
edge[cnt].from=v;
edge[cnt].to=u;
edge[cnt].capa=capa;
edge[cnt].next=head[v];
head[v]=cnt++;
return;
}
bool bfs()
{
queue<int> que;
que.push(source);
memset(dep,-1,sizeof(dep));
dep[source]=0;
while(!que.empty())
{
int node=que.front();
que.pop();
for(int i=head[node];~i;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].capa>0&&dep[v]==-1)
{
dep[v]=dep[node]+1;
if(v==sink) return true;
que.push(v);
}
}
}
return dep[sink]!=-1;
}
int dfs(int node,int minn)
{
if(node==sink||minn==0)
{
return minn;
}
int r=0;
for(int i=head[node];~i;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].capa>0&&dep[v]==dep[node]+1)
{
int tmp=dfs(v,min(edge[i].capa,minn));
if(tmp>0)
{
edge[i].capa-=tmp;
edge[i^1].capa+=tmp;
r+=tmp;
minn-=tmp;
if(!minn) break;
}
}
}
if(!r) dep[node]=-1;
return r;
}
int dinic()
{
int maxflow=0;
while(bfs())
{
maxflow+=dfs(source,inf);
}
return maxflow;
}
void cut_dfs(int node)
{
cut_vis[node]=true;
for(int i=head[node];~i;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].capa>0&&!cut_vis[v])
{
cut_dfs(v);
}
}
return;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,m;
while(~scanf("%d%d",&n,&m))
{
if(!n&&!m) break;
init();
for(int i=0;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
point[i].x=u;
point[i].y=v;
add(u,v,w);
}
source=1;
sink=2;
int ans=dinic();
cut_dfs(source);
for(int i=0;i<m;i++)
{
int u=point[i].x;
int v=point[i].y;
if((cut_vis[u]&&!cut_vis[v])||(!cut_vis[u]&&cut_vis[v]))
{
cout<<u<<' '<<v<<endl;
}
}
puts("");
}
return 0;
}