解题报告 之 ZOJ2332 Gems

解题报告 之 ZOJ2332 Gems

Description

Wealthy alsomagic!

Supernatural alsomagic!

But also poor alsomagic!

Because he is now puzzled by a problem, and will go crazy if you can‘t help him.

alsomagic has a lot of gems with different colors and shapes. His supernatural power provides him the ability of transforming some gems‘ shape and color! Now he wants to give some of gems to his darling girlfriend as gift. But a problem came with him. His
girlfriend dislikes too many gems with the same color! And alsomagic, will also be disgusted with lots of gems with the same shape.

So he attempts to change some gems to solve this problem.

Input

The first line of a multiple input is an integer T, followed by T input blocks. The first line of each input block are two integers N, M (1 =< N,M <= 10) indicating alsomagic has N different shapes of gems with M different colors, then N lines of integers
follow, each line contains M integers. The integer K in row R column C states that alsomagic has K number of gems with shape R and color C.

Follow this is integer D, then D lines of integers. Each line contains four integers R1 C1 R2 C2(0 =< R1, R2 < N, 0 <= C1, C2 < M) indicating one kind of transfor way, by which he can change some of shape R1, color C1 gems to shape R2, color C2, and VICE
VERSA. But each way of transform can only be used ONCE!

Then N integers, the K-th integer is the number of gems with SHAPE K that alsomagic can tolerance.

Then M integers. The K-th integer is the number of gems with COLOR K that alsomagic‘s girlfriend can tolerance!

Output

One line percase, with "Yes" if there is a way to solve this problem, otherwise "No".

Sample Input

2

1 3

3 2 6

1

0 1 0 2

5

1 3 2

1 3

3 2 6

1

0 1 0 2

4

1 3 2

Sample Output

Yes

No

题目大意:石头有n种,m种形状。现在告诉你有着第 i 种形状,有第 j 种形状的石头有多少个,要你把他们按照一定要求分成两堆。特别地,有一些转化。能将R1形状C1颜色的石头转化为R2形状C2颜色的石头。分堆的要求是第一堆的第c种颜色不能超过tc种,而第二堆的第
s 种形状不能超过ts种。问你是否存在一种分配方案。

分析:首先吐槽出题人的英语也是有够魔性。说都说不清楚。首先alsomagic对形状的要求不说清楚是对送出去的还是剩下的,这样根本理解不了题意。第二那个转化魔法use once 没说清楚是只能改变一个石头,还是只能选择使用一个转化方向,还是每个方向只能一次且可转化随意数量的石头。事实证明是第三个意思。

好吧,言归正传。最大流嘛很明显。一开始我以为形状和颜色女友都有要求,于是乎老师构图不出。所以我们如下构图。超级源点连接每种石头(颜色或形状不同都算一种,则最多有100种:10*10),负载为该种石头的数量。然后形状一样石头分别连接到对应的形状汇总节点,也就是200+1~200+n。同理,颜色一样的石头分别连接到
300+1~300+m节点(我间隔一般取得大而不想老是用n、m来算)。并将所有形状汇总节点和颜色汇总节点分别连接到形状总结点803和颜色总结点804。803和804连接到超级汇点。然后转化魔法就连接两种石头即可,负载为INF。最后跑最大流看看超级汇点的流量是否等于石头总数,是的话就Yes,否则No。

上代码:

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

const int MAXN = 810;
const int MAXM = 11000;
const int INF = 0x3f3f3f3f;

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

Edge edge[MAXM];
int level[MAXN];
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,total=0;
	cin >> kase;
	src = 0, des = 805;
	while (kase--)
	{
		memset( head, -1, sizeof head );
		cnt = 0;
		total = 0;
		int n, m;
		cin >> n >> m;
		for (int i = 0; i < n; i++)
		{
			for (int j = 1; j <= m; j++)
			{
				int gem;
				cin >> gem;
				total += gem;
				addedge( src, i * 10 + j, gem );
				addedge( i * 10 + j, i + 200 + 1, INF );
				addedge( i * 10 + j, j + 300, INF );
			}
		}

		int change;
		cin >> change;
		while (change--)
		{
			int s1, c1, s2, c2;
			cin >> s1 >> c1 >> s2 >> c2;
			c1++;
			c2++;
			addedge( s1 * 10 + c1, s2 * 10 + c2 , INF );
			addedge( s2 * 10 + c2, s1 * 10 + c1 , INF );
		}

		for (int i = 1; i <= n; i++)
		{
			int ts;
			cin >> ts;
			addedge( 200 + i, 803, ts );
		}

		for (int i = 1; i <= m; i++)
		{
			int tc;
			cin >> tc;
			addedge( 300 + i, 804, tc );
		}

		addedge( 803, 805, INF );
		addedge( 804, 805, INF );
		if (Dinic() == total)
			cout << "Yes" << endl;
		else
			cout << "No" << endl;
	}

	return 0;
}

时间: 2025-01-07 00:36:02

解题报告 之 ZOJ2332 Gems的相关文章

解题报告 之 POJ3057 Evacuation

解题报告 之 POJ3057 Evacuation Description Fires can be disastrous, especially when a fire breaks out in a room that is completely filled with people. Rooms usually have a couple of exits and emergency exits, but with everyone rushing out at the same time

hdu 1541 Stars 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 题目意思:有 N 颗星星,每颗星星都有各自的等级.给出每颗星星的坐标(x, y),它的等级由所有比它低层(或者同层)的或者在它左手边的星星数决定.计算出每个等级(0 ~ n-1)的星星各有多少颗. 我只能说,题目换了一下就不会变通了,泪~~~~ 星星的分布是不是很像树状数组呢~~~没错,就是树状数组题来滴! 按照题目输入,当前星星与后面的星星没有关系.所以只要把 x 之前的横坐标加起来就可以了

【百度之星2014~初赛(第二轮)解题报告】Chess

声明 笔者最近意外的发现 笔者的个人网站http://tiankonguse.com/ 的很多文章被其它网站转载,但是转载时未声明文章来源或参考自 http://tiankonguse.com/ 网站,因此,笔者添加此条声明. 郑重声明:这篇记录<[百度之星2014~初赛(第二轮)解题报告]Chess>转载自 http://tiankonguse.com/ 的这条记录:http://tiankonguse.com/record/record.php?id=667 前言 最近要毕业了,有半年没做

2016 第七届蓝桥杯 c/c++ B组省赛真题及解题报告

2016 第七届蓝桥杯 c/c++ B组省赛真题及解题报告 勘误1:第6题第4个 if最后一个条件粗心写错了,答案应为1580. 条件应为abs(a[3]-a[7])!=1,宝宝心理苦啊.!感谢zzh童鞋的提醒. 勘误2:第7题在推断连通的时候条件写错了,后两个if条件中是应该是<=12 落了一个等于号.正确答案应为116. 1.煤球数目 有一堆煤球.堆成三角棱锥形.详细: 第一层放1个, 第二层3个(排列成三角形), 第三层6个(排列成三角形), 第四层10个(排列成三角形). -. 假设一共

[noip2011]铺地毯(carpet)解题报告

最近在写noip2011的题,备战noip,先给自己加个油! 下面是noip2011的试题和自己的解题报告,希望对大家有帮助,题目1如下 1.铺地毯(carpet.cpp/c/pas) [问题描述]为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形地毯.一共有n 张地毯,编号从1 到n.现在将这些地毯按照编号从小到大的顺序平行于坐标轴先后铺设,后铺的地毯覆盖在前面已经铺好的地毯之上.地毯铺设完成后,组织者想知道覆盖地面某个点的最上面的那张地毯的

ACdream 1203 - KIDx&#39;s Triangle(解题报告)

KIDx's Triangle Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description One day, KIDx solved a math problem for middle students in seconds! And than he created this problem. N

解题报告 之 CodeForces 91B Queue

解题报告 之 CodeForces 91B Queue Description There are n walruses standing in a queue in an airport. They are numbered starting from the queue's tail: the 1-st walrus stands at the end of the queue and the n-th walrus stands at the beginning of the queue.

解题报告 之 POJ1226 Substrings

解题报告 之 POJ1226 Substrings Description You are given a number of case-sensitive strings of alphabetic characters, find the largest string X, such that either X, or its inverse can be found as a substring of any of the given strings. Input The first li

解题报告 之 UVA563 Crimewave

解题报告 之 UVA563 Crimewave Description Nieuw Knollendam is a very modern town. This becomes clear already when looking at the layout of its map, which is just a rectangular grid of streets and avenues. Being an important trade centre, Nieuw Knollendam a