hdu_3491

Relatively easy to see it is a minimal cut.
I use minimum cut + demolition point.
Title means: a gang of thieves think T city stealing, they are currently in the S city.
Now the police want this gang of thieves to be pulled over.
They can only intercept thief passing city, not in the S or T.
In each city to intercept certain costs.
Ask how much cost to a minimum certain the thief to intercept them.
   
Split for each point (x-> x''), and the costs required by the capacity between the two points in the city interception.
Start and end after the demolition point, between two points capacity inf, because they can not intercept.
In side the two cities, a, b, connected to a'' -> b, b'' -> a.
Find the smallest cut is the least cost.
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define MV	402
#define ME	80001
#define INF		0x7fffffff
using namespace std;

struct e{
	int v, va, n;
}e[ME];

int e_c;
int h[MV], c[MV], p[MV], d[MV], g[MV];

void insert_arc(int u, int v, int w)
{
	e[e_c].v = v; e[e_c].va = w;
	e[e_c].n = h[u];	h[u] = e_c ++;
	swap(u, v);
	e[e_c].v = v; e[e_c].va = 0;
	e[e_c].n = h[u];	h[u] = e_c ++;
}

void bfs(int de)
{
	int u, v;
	queue<int> q;
	memset(g, 0, sizeof(g));
	memset(d, -1, sizeof(d));
	d[de] = 0;	g[0] = 1;
	q.push(de);
	while( !q.empty() ) {
		u = q.front(); q.pop();
		for(int i = h[u]; i != -1; i = e[i].n) {
			v = e[i].v;
			if( e[i].va || d[v] != -1 ) 	continue;
			d[v] = d[u]+1;	q.push(v);	g[d[v]] ++;
		}
	}
}

int i_sap(int s, int de, int VN)
{
	int u(s), v, min_d, m_f(0), p_f(INF);
	bfs(s);	p[u] = u;
	for(int i = 0; i <= VN; i ++)
		c[i] = h[i];
	while( d[s] < VN ) {
loop:
		for(int &i = c[u]; i != -1; i = e[i].n) {
			v = e[i].v;
			if( !e[i].va || d[u] != d[v]+1 )	continue;
			p[v] = u;	p_f = min(p_f, e[i].va);
			u = v;
			if( v == de ) {
				for(u = p[u]; v != s; v = u, u = p[u]) {
					e[c[u]].va -= p_f;	e[c[u]^1].va += p_f;
				}
				m_f += p_f; p_f = INF;
			}
			goto loop;
		}
		min_d = VN;
		for(int i = h[u]; i != -1; i = e[i].n) {
			v = e[i].v;
			if( e[i].va && min_d > d[v] ) {
				c[u] = i; min_d = d[v];
			}
		}
		g[d[u]] --;
		if( !g[d[u]] )	break;
		d[u] = min_d+1;
		g[d[u]] ++; u = p[u];
	}
	return m_f;
}

int main(int argc, char const *argv[])
{
	int ver, arc, s, de, v, f, t, ss, ee;
	while( ~scanf("%d %d %d %d", &ver, &arc, &s, &de) ) {
		e_c = 0;	memset(h, -1, sizeof(h));
		ss = 0;	ee = 2*ver+1;
		insert_arc(ss, s, INF);	insert_arc(de+ver, ee, INF);
		for(int i = 1; i <= ver; i ++) {
			scanf("%d", &v);
			insert_arc(i, i+ver, v);
		}
		for(int i = 0; i < arc; i ++) {
			scanf("%d %d", &f, &t);
			insert_arc(f+ver, t, INF);
			insert_arc(t+ver, f, INF);
		}
		printf("%d\n", i_sap(ss, ee, ee));
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值