Uva 11248 求只修改一条边的流量使得最大流>c 的所有可行边

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2205

题意:

给定一个有向网络,每条边均有一个容量。问是否存在一个从点1->n,流量为C的流。如果不存在,是否可以恰好修改一条弧的容量,使得这样的流存在?

 第一行输入 N ,E, C (n个点, e条边)

下面e行u v cap 表示边

若最大流>=c 直接输出 possible

若要修改 则输出possible option: 和弧的列表(左端点小到大排序,再右端点小到大排序)

若不存在 则输出 not possible

思路:

先求一次最大流,>=c直接输出答案

否则,修改边(修改有意义的边一定是满流的(即最小割中的边))

一个优化:求完最大流后把流量留着,以后每次在它的基础上增广

#include<stdio.h>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<string.h>
#include<iostream>
#include<math.h>
#include<queue>
#include<map>
using namespace std;
#define ll long long

#define N 1005
#define M 20000
#define inf 536870912
//N为点数 M为边数

struct Edge{
	int from, to, cap, nex;
}edge[M*10];//注意这个一定要够大 不然会re 还有反向弧

int head[N], edgenum;
void addedge(int u, int v, int cap){
	Edge E = { u, v, cap, head[u]};
	edge[ edgenum ] = E;
	head[u] = edgenum ++;

	Edge E2= { v, u, 0,   head[v]};
	edge[ edgenum ] = E2;
	head[v] = edgenum ++;
}
int sign[N], s, t;
bool BFS(int from, int to){
	memset(sign, -1, sizeof(sign));
	sign[from] = 0;

	queue<int>q;
	q.push(from);
	while( !q.empty() ){
		int u = q.front(); q.pop();
		for(int i = head[u]; i!=-1; i = edge[i].nex)
		{
			int v = edge[i].to;
			if(sign[v]==-1 && edge[i].cap)
			{
				sign[v] = sign[u] + 1, q.push(v);
				if(sign[to] != -1)return true;
			}
		}
	}
	return false;
}
int Stack[M*4], top, cur[M*4];
struct Change{
	int sign, val;
	Change(int a=0,int b=0):sign(a),val(b){}
}haha[M*10];
int hahatop;
void release(){
	for(int i = 0; i < hahatop; i++)
		edge[haha[i].sign].cap += haha[i].val;
}
int dinic(){
	hahatop = 0;
	int ans = 0;
	while( BFS(s, t) )
	{
		memcpy(cur, head, sizeof(head));
		int u = s;		top = 0;
		while(1)
		{
			if(u == t)
			{
				int flow = inf, loc;//loc 表示 Stack 中 cap 最小的边
				for(int i = 0; i < top; i++)
					if(flow > edge[ Stack[i] ].cap)
					{
						flow = edge[Stack[i]].cap;
						loc = i;
					}

					for(int i = 0; i < top; i++)
					{
						edge[ Stack[i] ].cap -= flow;
						edge[Stack[i]^1].cap += flow;
						haha[hahatop++] = Change(Stack[i], flow);
						haha[hahatop++] = Change(Stack[i]^1, -flow);
					}
					ans += flow;
					top = loc;
					u = edge[Stack[top]].from;
			}
			for(int i = cur[u]; i!=-1; cur[u] = i = edge[i].nex)//cur[u] 表示u所在能增广的边的下标
				if(edge[i].cap && (sign[u] + 1 == sign[ edge[i].to ]))break;
			if(cur[u] != -1)
			{
				Stack[top++] = cur[u];
				u = edge[ cur[u] ].to;
			}
			else
			{
				if( top == 0 )break;
				sign[u] = -1;
				u = edge[ Stack[--top] ].from;
			}
		}
	}
	return ans;
}
int n, e, c;
struct Ans{
	int f, t;
	bool operator<(const Ans a)const{
		if(a.f == f)return a.t>t;
		return a.f>f;
	}
	Ans(int a=0,int b=0):f(a),t(b){}
}ans[M*2];
int main(){
	int i, j, u, v, cap, Cas = 1;
	while(scanf("%d %d %d",&n,&e,&c), n+e+c){
		memset(head, -1, sizeof(head)); edgenum = 0;
		while(e--){
			scanf("%d %d %d",&u,&v,&cap);
			addedge(u,v,cap);
		}
		printf("Case %d: ", Cas++);
		s = 1; t = n;
		int maxflow = dinic();
		if(maxflow >= c){puts("possible");continue;}

		int anstop = 0;
		for(i = 0; i < edgenum; i+=2)if(edge[i].cap == 0){
			edge[i].cap += c;
			if(dinic()+maxflow>=c)ans[anstop++] = Ans(edge[i].from,edge[i].to);
			release();
			edge[i].cap = 0;
		}

		if(anstop==0)puts("not possible");
		else 
		{
			sort(ans, ans+anstop);
			printf("possible option");
			for(i=0;i<anstop;i++)printf("%c(%d,%d)",i?',':':',ans[i].f,ans[i].t);
			puts("");
		}
	}
	return 0;
}
/*
4 4 5
1 2 5
1 3 5
2 4 5
3 4 5
4 4 5
1 2 1
1 3 5
2 4 5
3 4 1
4 4 5
1 2 1
1 3 1
2 4 1
3 4 1
0 0 0

*/


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值