网络流

网络流

最大流

这里参考了hzwer(%%%)的模板

luogu3376

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;
template<typename T>
T Min(const T& a,const T& b){return a<b?a:b;}
const int maxn=10003;
const int maxm=100003;
const int inf=2147483647;
int n,m,s,t;
struct EDGE
{
	int to,nxt,w;
}edge[maxm<<1];
int ek=0;
int node[maxn];
int cur[maxn];
inline void addEdge(int from,int too,int ww)
{
	++ek;
	edge[ek].to=too;
	edge[ek].w=ww;
	edge[ek].nxt=node[from];
	node[from]=ek;
}
const int maxnode=maxn;
template<typename T>
struct QUE
{
	T ary[maxnode];
	int ff,tt;
	QUE(){ff=tt=1;}
	void clear(){ff=tt=1;}
	bool empty(){return ff==tt;}
	void push(const T& w){ary[tt++]=w;}
	void pop(){++ff;}
	T front(){return ary[ff];}
};
QUE<int> q;
int d[maxn];
bool findPath()//bfs初始化d 
{
	memset(d+1,0,n*(sizeof(int)));
	//memset(d,0,sizeof(d));
	d[s]=1;
	q.clear();
	q.push(s);
	while(!q.empty())
	{
		int f=q.front();q.pop();
		for(int i=node[f];i;i=edge[i].nxt)
		{
			int v=edge[i].to;
			if(edge[i].w&&!d[v])
			{
				d[v]=d[f]+1;
				q.push(v);
			}
		}
	}
	return d[t];
}
int dfs(int k,int flow)//当前点,当前最大流量 
{
	if(k==t)
		return flow;
	int used=0;//当前已用流量 
	for(int i=cur[k];i;i=edge[i].nxt)
	{
		int v=edge[i].to;
		if(d[v]==d[k]+1)
		{
			int tmp=dfs(v,Min(flow-used,edge[i].w));//这条边往下最大流 
			edge[i].w-=tmp;
			edge[i+i&1?1:-1].w+=tmp;//反向边 
			used+=tmp;
			if(edge[i].w)
				cur[k]=i;//标记,这条边以前的边在这次都流满了,这条边还有可能压榨 
			if(used==flow)//已经流满 
				return flow;
		}
	}
	if(!used)//这条边流不到汇点 
		d[k]=0;
	return used;
}
long long ans=0;
void dinic() 
{
	while(findPath())
	{
		memcpy(cur+1,node+1,n*sizeof(int));
		//memcpy(cur,node,sizeof(node));
		ans+=dfs(s,inf);
	}
}
void input()
{
	scanf("%d%d%d%d",&n,&m,&s,&t);
	for(int i=1;i<=m;++i)
	{
		int x,y,w;
		scanf("%d%d%d",&x,&y,&w);
		addEdge(x,y,w);
		addEdge(y,x,0);
	}
}
int main()
{
	input();
	dinic();
	printf("%lld\n",ans);
	return 0;
}

usaco4.2.1

最小割

最小割点集

例题:USACO5.4.1=luogu1345

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
ID:du331691
PROG:telecow
LANG:C++
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
using namespace std;
/*拆点*/ 
const int maxn=103;
const int maxm=603*2*2+maxn*2;
int n,m,s,t;
template<typename T>
T Max(const T& a,const T& b){return a>b?a:b;}
template<typename T>
T Min(const T& a,const T& b){return a<b?a:b;}
const int inf=2147483647;
struct EDGE
{
    int to,nxt,w,tw;
}edge[maxm];
int ek=0,node[maxn*2],cur[maxn*2];//x+n为出点 
int to[maxm],tk=0;
bool del[maxm];
void addEdge(int from,int too,int ww)
{
    ++ek;
    edge[ek].to=too;
    edge[ek].w=edge[ek].tw=ww;
    edge[ek].nxt=node[from];
    node[from]=ek;
}

void input()
{
	scanf("%d%d%d%d",&n,&m,&s,&t);
	int x,y;
	for(int i=1;i<=m;++i)
	{
		scanf("%d%d",&x,&y);
		addEdge(x+n,y,inf);
		addEdge(y,x+n,0);
		addEdge(y+n,x,inf);
		addEdge(x,y+n,0);
	}
	for(int i=1;i<=n;++i)
	{
		if(i==t||i==s)
			addEdge(i,i+n,inf);
		else addEdge(i,i+n,1);
		if(i!=s&&i!=t)to[++tk]=ek;
		addEdge(i+n,i,0);
	}
	m=m*2+n;
}
bool cmp(int a,int b)
{
    return edge[a].w>edge[b].w;
}
int d[2*maxn];
template<typename T>
struct QUE
{
    T ary[2*maxn];
    int f,t;
    QUE(){f=t=1;}
    void clear(){f=t=1;}
    void push(const T& w){ary[t++]=w;}
    void pop(){++f;}
    bool empty(){return f==t;}
    T front(){return ary[f];}
};
QUE<int> q;
bool bfs()
{
    memset(d,0,sizeof(d));
    q.clear();
    d[s]=1;
    q.push(s);
    while(!q.empty())
    {
        int ff=q.front();q.pop();
        for(int i=node[ff];i;i=edge[i].nxt)
        {
            int v=edge[i].to;
            if(edge[i].w&&(!d[v])&&!del[i])
            {
                d[v]=d[ff]+1;
                q.push(v);
            }
        }
    }
    return d[t];
}
int dfs(int k,int flow)//当前点,当前最大流量 
{
    if(k==t)
        return flow;
    int used=0;//当前已用流量 
    for(int i=cur[k];i;i=edge[i].nxt)
    {
        int v=edge[i].to;
        if(d[v]==d[k]+1&&!del[i])
        {
            int tmp=dfs(v,Min(flow-used,edge[i].w));//这条边往下最大流 
            edge[i].w-=tmp;
            edge[i&1?i+1:i-1].w+=tmp;//反向边 
            used+=tmp;
            if(edge[i].w)
                cur[k]=i;//标记,这条边以前的边在这次都流满了,这条边还有可能压榨 
            if(used==flow)//已经流满 
                return flow;
        }
    }
    if(!used)//这条边流不到汇点 
        d[k]=0;
    return used;
}
int dinic() 
{
    int ans=0;
    while(bfs())
    {
        memcpy(cur+1,node+1,n*2*sizeof(int));
        //memcpy(cur,node,sizeof(node));
        ans+=dfs(s,inf);
    }
    return ans;
}
void recover()
{
    for(int i=1;i<=m*2;++i)
    {
        if(!del[i])
        edge[i].w=edge[i].tw;
    }
}
int maxflow;
int edgedel[maxm],edk=0;
void solve()
{
	sort(to+1,to+tk+1);
    maxflow=dinic();
    //printf("%d ",maxflow);
    for(int i=1;i<=tk&&maxflow;++i)
    {
        recover();
        del[to[i]]=true;
        del[to[i]&1?to[i]+1:to[i]-1]=true;
        int tflow=dinic();
        if(maxflow-tflow==edge[to[i]].tw)
        {
            edgedel[++edk]=edge[to[i]].to-n;
            //cout<<edge[to[i]].tw<<endl;
            maxflow=tflow;
        }
        else del[to[i]]=false,del[to[i]&1?to[i]+1:to[i]-1]=false;
    }
}
void output()
{
    sort(edgedel+1,edgedel+edk+1);
   	printf("%d\n",edk);
    for(int i=1;i<=edk;++i)
    {
        printf("%d",edgedel[i]);
        if(i!=edk)putchar(' ');
        else putchar('\n');
    }
}
int main()
{
	freopen("telecow.in","r",stdin);
	freopen("telecow.out","w",stdout);
	input();
	solve();
	output();
	return 0;
}

最小割边集

例题:USACO4.4.2=luogu1344

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
ID:du331691
PROG:milk6
LANG:C++
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
/*
求最小割,及边数最小的割集 
枚举每一条边
如果属于最小割 
则有 删前最大流-删后最大流=边权  ① 

易知 当最终最大流为0时,
所有属于最小割的边都已经删完
那么贪心:每次选举边权大的满足①的边删,让最大流更快地减少 

如果这条边不符合①就加回来
不然就真正删掉并更新最大流的值 
*/
template<typename T>
T Max(const T& a,const T& b){return a>b?a:b;}
template<typename T>
T Min(const T& a,const T& b){return a<b?a:b;}
const int maxn=33;
const int maxm=2*1003;
const int inf=2147483647;
int n,m;
struct EDGE
{
	int to,nxt,w,tw;
}edge[maxm];
int ek=0,node[maxn],cur[maxn];
int to[maxm];
bool del[maxm];
void addEdge(int from,int too,int ww)
{
	++ek;
	edge[ek].to=too;
	edge[ek].w=edge[ek].tw=ww;
	edge[ek].nxt=node[from];
	node[from]=ek;
}
void input()
{
	scanf("%d%d",&n,&m);
	int x,y,ww;
	for(int i=1;i<=m;++i)
	{
		scanf("%d%d%d",&x,&y,&ww);
		addEdge(x,y,ww);
		addEdge(y,x,0);
	}
}
bool cmp(int a,int b)
{
	return edge[a].w>edge[b].w;
}
void initTo()
{
	for(int i=1;i<=m*2;i+=2)
	{
		to[(i+1)/2]=i;
	}
	sort(to+1,to+m+1,cmp);
}
int d[maxn];
template<typename T>
struct QUE
{
	T ary[maxn];
	int f,t;
	QUE(){f=t=1;}
	void clear(){f=t=1;}
	void push(const T& w){ary[t++]=w;}
	void pop(){++f;}
	bool empty(){return f==t;}
	T front(){return ary[f];}
};
QUE<int> q;
bool bfs()
{
	memset(d,0,sizeof(d));
	q.clear();
	d[1]=1;
	q.push(1);
	while(!q.empty())
	{
		int ff=q.front();q.pop();
		for(int i=node[ff];i;i=edge[i].nxt)
		{
			int v=edge[i].to;
			if(edge[i].w&&(!d[v])&&!del[i])
			{
				d[v]=d[ff]+1;
				q.push(v);
			}
		}
	}
	return d[n];
}
int dfs(int k,int flow)//当前点,当前最大流量 
{
	if(k==n)
		return flow;
	int used=0;//当前已用流量 
	for(int i=cur[k];i;i=edge[i].nxt)
	{
		int v=edge[i].to;
		if(d[v]==d[k]+1&&!del[i])
		{
			int tmp=dfs(v,Min(flow-used,edge[i].w));//这条边往下最大流 
			edge[i].w-=tmp;
			edge[i&1?i+1:i-1].w+=tmp;//反向边 
			used+=tmp;
			if(edge[i].w)
				cur[k]=i;//标记,这条边以前的边在这次都流满了,这条边还有可能压榨 
			if(used==flow)//已经流满 
				return flow;
		}
	}
	if(!used)//这条边流不到汇点 
		d[k]=0;
	return used;
}
int dinic() 
{
	int ans=0;
	while(bfs())
	{
		memcpy(cur+1,node+1,n*sizeof(int));
		//memcpy(cur,node,sizeof(node));
		ans+=dfs(1,inf);
	}
	return ans;
}
void recover()
{
	for(int i=1;i<=m*2;++i)
	{
		if(!del[i])
		edge[i].w=edge[i].tw;
	}
}
int maxflow;
int edgedel[maxm],edk=0;
void solve()
{
	maxflow=dinic();
	printf("%d ",maxflow);
	for(int i=1;i<=m&&maxflow;++i)
	{
		recover();
		del[to[i]]=true;
		del[to[i]&1?to[i]+1:to[i]-1]=true;
		int tflow=dinic();
		if(maxflow-tflow==edge[to[i]].tw)
		{
			edgedel[++edk]=(to[i]+1)/2;
			//cout<<edge[to[i]].tw<<endl;
			maxflow=tflow;
		}
		else del[to[i]]=false,del[to[i]&1?to[i]+1:to[i]-1]=false;
	}
}
void output()
{
	sort(edgedel+1,edgedel+edk+1);
	printf("%d\n",edk);
	for(int i=1;i<=edk;++i)
	{
		printf("%d\n",edgedel[i]);
	}
}
int main()
{
	freopen("milk6.in","r",stdin);
	freopen("milk6.out","w",stdout);
	input();
	initTo();
	solve();
	output();
	return 0;
}
0%