hdu_1281

ek超时,简单的dinic或者isap的应用
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
#define MAXROW 109
#define MAXCOL 109
#define INF 0x7fffffff
using namespace std;

typedef struct p
{
	int x;
	int y;
}Point;

int row;
int col;
int cnt;
int vertex;
int max_flow;
int level[MAXROW+MAXCOL];
int r[MAXROW+MAXCOL][MAXROW+MAXCOL];
int t_r[MAXROW+MAXCOL][MAXROW+MAXCOL];
Point arr[MAXROW*MAXCOL];

bool bfs(void)
{
	int cur;
	queue<int> q;

	memset(level, -1, sizeof(level));
	level[0] = 0;
	q.push(0);

	while( !q.empty() )
	{
		cur = q.front(); q.pop();

		for(int i = 0; i <= vertex; i ++)
		{
			if( -1 != level[i] || !r[cur][i] )
				continue;

			level[i] = level[cur]+1;
			q.push(i);
		}
	}
	return (-1 == level[vertex])? false : true;
}

int dfs(int u, int cur_val)
{
	int tmp_val;
	int val;

	if( u == vertex )
		return cur_val;

	tmp_val = cur_val;
	for(int v = 0; v <= vertex; v ++)
	{
		if( (level[v] != level[u]+1) || !r[u][v])
			continue;

		val = dfs(v, min(tmp_val, r[u][v]));
		tmp_val -= val;
		r[u][v] -= val;
		r[v][u] += val;
	}
	return cur_val - tmp_val;
}

int dinic(void)
{
	int val;
	int ans;

	ans = val = 0;
	while( true == bfs() )
		while( val = dfs(0, INF) )
			ans += val;
	return ans;
}

void copy_arr(const int from[][MAXROW+MAXCOL], int to[][MAXROW+MAXCOL])
{
	for(int i = 0; i <= vertex; i ++)
		for(int j = 0; j <= vertex; j ++)
			to[i][j] = from[i][j];
}

int main(int argc, char const *argv[])
{
	//freopen("test.in", "r", stdin);
	int from;
	int to;
	int val;
	int ans;
	int cnts;

	cnts = 1;
	while( ~scanf("%d %d %d", &row, &col, &cnt) )
	{
		vertex = row+col+1;
		memset(r, 0, sizeof(r));

		for(int i = 1; i <= row; i ++)
			r[0][i] = 1;

		for(int i = row+1; i < vertex; i ++)
			r[i][vertex] = 1;

		for(int i = 0; i < cnt; i ++)
		{
			scanf("%d %d", &from, &to);
			r[from][to+row] ++;
			arr[i].x = from;
			arr[i].y = to+row;
		}

		copy_arr(r, t_r);
		max_flow = dinic();
		
		ans = 0;
		copy_arr(t_r, r);
		for(int i = 0; i < cnt; i ++)
		{
			r[arr[i].x][arr[i].y] --;
			val = dinic();
			if( val != max_flow )
				ans ++;
			copy_arr(t_r, r);
		}
		printf("Board %d have %d important blanks for %d chessmen.\n", cnts ++, ans, max_flow);
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值