解题报告 之 SOJ2835 Pick Up Points

解题报告 之 SOJ2835 Pick Up Points

Description

Description

Recently loy are very interested in playing a funny game. There is a board of n*m grid. Each grid has one point or not. If two adjacency grids both have one point, someone can pick them up, and then these two grids have no point. The two grids are adjacency only if the Manhattan distence between them is exactly 1. The purpose of the game is to gain as many points as possible.

Input

The first line is an interger T, means the number of test cases. Each test case begins with two interger n,m (1 <= n,m <= 100), means the size of the board is n*m. Next n line, each line has m intergers which is 1 or 0, means the grid has one point or not.

Output

Output the maximum point loy can get.

Sample Input

2 3 4 0 1 0 1 0 1 1 0 1 1 1 1 2 2 1 0 0 1

Sample Output

6 0

Source

LOY

题目大意:题意非常简单,有一个n*m的棋盘,每个格子有1个或0个棋子,每次操作可以取走相邻的两个棋子(相邻意味着两个格子共享一条边)。问最多一共可以取走多少个棋子。

分析:我感觉挺难的,不过看过题数比其他题还高。。。醉了。这个是CFW模型(牛吃草喝水模型,见另一篇博文POJ
3281 Dining http://blog.csdn.net/maxichu/article/details/45190521 ),但是很难将此问题转化为牛吃草(反正我没想到)。

具体操作如下,将棋盘错开,i+j为奇数的格子为条件一(CFW模型中的F);i+j为偶数的格子为条件二(CFW模型中的D)。然后超级源点向所有条件一的点连一条边,负载为1。所有条件二的点向超级汇点连一条边,负载为1。那么超级汇点的最大流代表最大的配对数。然后要注意遍历所有点,如果它是条件一的点,则连一条边到它周围点(即4个条件二点,如果存在的话)。注意如果是条件二点则不能向周围连一条边,否则流量会错误转移(见图解)。然后跑最大流再乘以2即可。

注意到此时如果5<->2是都双向连接的话那么最大流从2变为3,错误!

上代码:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cstdio>
#include<sstream>
#include<string>
#include<cmath>
using namespace std;

const int MAXN = 20100;
const int MAXM = 900000;
const int INF = 0x3f3f3f3f;

struct Edge
{
	int from, to, cap, next;
};

Edge edge[MAXM];
int level[MAXN];
int map[150][150];
int head[MAXN];
int src, des, cnt = 0;

void addedge( int from, int to, int cap )
{
	edge[cnt].from = from;
	edge[cnt].to = to;
	edge[cnt].cap = cap;
	edge[cnt].next = head[from];
	head[from] = cnt++;

	swap( from, to );

	edge[cnt].from = from;
	edge[cnt].to = to;
	edge[cnt].cap = 0;
	edge[cnt].next = head[from];
	head[from] = cnt++;
}

int bfs( )
{
	memset( level, -1, sizeof level );
	queue<int> q;
	while (!q.empty( ))
		q.pop( );
	level[src] = 0;
	q.push( src );

	while (!q.empty( ))
	{
		int u = q.front( );
		q.pop( );
		for (int i = head[u]; i != -1; i = edge[i].next)
		{
			int v = edge[i].to;
			if (edge[i].cap&&level[v] == -1)
			{
				level[v] = level[u] + 1;
				q.push( v );
			}
		}
	}
	return level[des] != -1;
}

int dfs( int u, int f )
{
	if (u == des) return f;

	int tem;
	for (int i = head[u]; i != -1; i = edge[i].next)
	{
		int v = edge[i].to;
		if (edge[i].cap&&level[v] == level[u] + 1)
		{
			tem = dfs( v, min( f, edge[i].cap ) );
			if (tem > 0)
			{
				edge[i].cap -= tem;
				edge[i ^ 1].cap += tem;
				return tem;
			}
		}
	}
	level[u] = -1;
	return 0;
}

int Dinic( )
{
	int ans = 0, tem;

	while (bfs( ))
	{
		while (tem = dfs( src, INF ))
		{
			ans += tem;
		}
	}
	return ans;
}

int main( )
{
	int kase, m, n;
	cin >> kase;
	src = 0; des = 20005;

	while (kase--)
	{
		cin >> n>>m;
		memset( head, -1, sizeof head );
		cnt = 0;

		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++)
			{
				cin >> map[i][j];
				if (map[i][j])
				{
					if ((i + j) % 2 == 1)
						addedge( src, (i - 1) *100 + j, 1 );
					else
						addedge( (i - 1) * 100 + j, des, 1 );
				}
			}
		}

		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++)
			{
				if (!map[i][j])	 continue;

				if (i+1<=n&&map[i+1][j])
				{
					addedge( (i - 1) * 100 + j, i * 100 + j, 1 );
				}
				if (j + 1 <= m&&map[i][j+1])
				{
					addedge( (i - 1) * 100 + j, (i - 1) * 100 + j + 1, 1 );
				}
				if (i - 1 >= 1 && map[i-1][j])
				{
					addedge( (i - 1) * 100 + j, (i - 2) * 100 + j , 1 );
				}
				if (j - 1 >= 1 && map[i][j-1])
				{
					addedge( (i - 1) * 100 + j, (i - 1) * 100 + j - 1, 1 );
				}
			}
		}
		cout << Dinic() * 2 << endl;
	}
	return 0;
}

嗯嗯就是这样,看来还是要继续修炼。编程之美好想要Azure的衣服啊!!

时间: 2024-08-03 18:27:35

解题报告 之 SOJ2835 Pick Up Points的相关文章

Winter-2-STL-E Andy&#39;s First Dictionary 解题报告及测试数据

use stringstream Time Limit:3000MS     Memory Limit:0KB Description Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thin

解题报告 之 HOJ2816 Power Line

解题报告 之 HOJ2816 Power Line Problem Description While DUT is hosting this NECPC Contest, in order to control the expenditure, careful considerations should be given in many respects, such as the layout of the contest venue. When the contest venue is ar

解题报告 之 POJ3680 Intervals

解题报告 之 POJ3680 Intervals Description You are given N weighted open intervals. The ith interval covers (ai, bi) and weighs wi. Your task is to pick some of the intervals to maximize the total weights under the limit that no point in the real axis is c

【LeetCode】Symmetric Tree 解题报告

Symmetric Tree 解题报告 [LeetCode] https://leetcode.com/problems/symmetric-tree/ Total Accepted: 106639 Total Submissions: 313969 Difficulty: Easy Question Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For

解题报告 之 ZOJ3861 Valid Pattern Lock

解题报告 之 ZOJ3861 Valid Pattern Lock Description Pattern lock security is generally used in Android handsets instead of a password. The pattern lock can be set by joining points on a 3 × 3 matrix in a chosen order. The points of the matrix are registere

解题报告 之 WHU1124 Football Coach

解题报告 之 WHU1124 Football Coach Description It is not an easy job to be a coach of a football team. The season is almost over, only a few matches are left to play. All of sudden the team manager comes to you and tells you bad news: the main sponsor of

解题报告 之 POJ3041 Asteroids

解题报告 之 POJ3041 Asteroids Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K asteroids (1 <= K <= 10,000), which are conveniently located at t

zoj1032解题报告

一道几何题,开始不知道pick定理,于是就暴力..在一定范围内判断该点是否在多边形内,大致做法就是用该点作一条平行于x的射线,看与多边形的交点个数,其中注意的是若交点恰好为多边形的顶点要忽略.判断点是否在线段上,和求多边形面积可以用模板,结果超时..后来搜了下发现了pick定理,瞬间简单了很多. Pick定理:设以整数点为顶点的多边形的面积为S, 多边形内部的整数点数为N, 多边形边界上的整数点数为L, 则 N + L/2 - 1 = S. 果然像几何题,数论题不知道公式不行啊,只能多做这样题慢

ACM-ICPC 2017 Asia HongKong 解题报告

ACM-ICPC 2017 Asia HongKong 解题报告 任意门:https://nanti.jisuanke.com/?kw=ACM-ICPC%202017%20Asia%20HongKong 按AC次序: D - Card collection In an online game, a player can collect different types of power cards. Each power card can enable a player to have a uni