Poj -1273 Drainage Ditches (网络流-ISAP)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_41156591/article/details/81911181




        这里使用的是最大流-ISAP模板(ISAP + 当前弧优化 + GAP优化)   


using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int maxn = (int)1e5 + 500;
struct Edge {
	int v;//弧尾    
	int cap;
	int n;//指向下一条从同一个弧头出发的弧    
} edge[maxn];
int adj[maxn];//前向星的表头    
int Q[maxn], head, tail;//队列    
int d[maxn], cur[maxn], pre[maxn], num[maxn];
int nv;//编号修改的上限    
int top;
void add(int u, int v, int cap) {
	edge[top].v = v;
	edge[top].cap = cap;
	edge[top].n = adj[u];
	adj[u] = top++;
	edge[top].v = u;
	edge[top].cap = 0;
	edge[top].n = adj[v];
	adj[v] = top++;
void rev_bfs(int t) {//反向BFS标号    
	memset(num, 0, sizeof(num));
	memset(d, -1, sizeof(d));//没标过号则为-1    
	d[t] = 0;//汇点默认为标过号    
	num[0] = 1;
	head = tail = 0;
	Q[tail++] = t;
	while (head != tail) {
		int u = Q[head++];
		for (int i = adj[u]; ~i; i = edge[i].n) {
			int v = edge[i].v;
			if (~d[v]) continue;//已经标过号    
			d[v] = d[u] + 1;//标号    
			Q[tail++] = v;
int max_flow(int s, int t) {
	memcpy(cur, adj, sizeof(cur));//复制,当前弧优化    
	int flow = 0, u = pre[s] = s;
	while (d[t] < nv) {//最长也就是一条链,其中最大的标号只会是nv - 1,
		if (u == t) { //如果已经找到了一条增广路,则沿着增广路修改流量    
			int f = inf, neck;
			for (int i = s; i != t; i = edge[cur[i]].v) {
				if (f > edge[cur[i]].cap) {
					f = edge[cur[i]].cap;//不断更新需要减少的流量    
					neck = i;//记录回退点,目的是为了不用再回到起点重新找    
			for (int i = s; i != t; i = edge[cur[i]].v) { //修改流量  
				edge[cur[i]].cap -= f;
				edge[cur[i] ^ 1].cap += f;
			flow += f;//更新    
			u = neck;//回退    
		int i;
		for (i = cur[u]; ~i; i = edge[i].n)
			if (d[edge[i].v] + 1 == d[u] && edge[i].cap)
		if (~i) {//如果存在可行增广路,更新    
			cur[u] = i;//修改当前弧    
			pre[edge[i].v] = u;
			u = edge[i].v;
		else { //否则回退,重新找增广路    
			if (0 == (--num[d[u]])) //GAP间隙优化,如果出现断层,可以知道
			int mind = nv;
			for (i = adj[u]; ~i; i = edge[i].n) {
				if (edge[i].cap && mind > d[edge[i].v]) {//寻找可以增广的最小标号  
					cur[u] = i;//修改当前弧    
					mind = d[edge[i].v];
			d[u] = mind + 1;
			u = pre[u];//回退    
	return flow;
void init() {
	memset(adj, -1, sizeof(adj));
	top = 0;
int main() {
	int m, n;
	while (~scanf("%d%d", &m, &n)) {
		int u, v, w;
		for (int i = 0; i < m; ++i) {
			scanf("%d%d%d", &u, &v, &w);
			add(u, v, w);
		nv = n + 1;
		printf("%d\n", max_flow(1, n));//这个是从1到n的
	return 0;


Drainage Ditches


Problem DescriptionnEvery time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch. nFarmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network. nGiven all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle. n nnInputnThe input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.n nnOutputnFor each case, output a single integer, the maximum rate at which water may emptied from the pond. n nnSample Inputn5 4n1 2 40n1 4 20n2 4 20n2 3 30n3 4 10n nnSample Outputn50n 问答