解题报告 之 ZOJ2332 Gems
来源:互联网 发布:雨林木风xp优化工具 编辑:程序博客网 时间:2024/05/22 16:48
解题报告 之 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
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
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;elsecout << "No" << endl;}return 0;}
- 解题报告 之 ZOJ2332 Gems
- HDU 4778 Gems Fight! 解题报告
- 解题报告之DisjointSet
- 解题报告之 非诚勿扰
- Nightmare之BFS解题报告
- Asteroids之BFS解题报告
- 解题报告之 字频统计
- 解题报告之 幸运数字
- 解题报告 之 UVA1347 Tour
- 解题报告 之 HDU4289 Control
- 解题报告 之 HDU4292 Food
- 解题报告 之 SOJ4426 Counting_3
- 解题报告 之 POJ1201 Intervals
- 解题报告 之 POJ1149 Pigs
- 解题报告 之 SGU326 Perspective
- 解题报告 之 UVA563 Crimewave
- 解题报告 之 POJ3281 Dining
- 解题报告 之 POJ3041 Asteroids
- mysqlhotcopy 只能用于MyISAM表不能用户InnoDB表
- Shell基础-概述
- TPService 编译过程
- Unity NGUI——3.8.2版本更新说明
- Java学习 - synchronized同步机制
- 解题报告 之 ZOJ2332 Gems
- Linux学习之CentOS(十三)--CentOS6.4下Mysql数据库的安装与配置
- 浙江 美女跑路老板 从非洲押解回国(图)
- PHP设计模式——原型模式
- hdu1247-字典树,单词拆分
- 数组分割
- 黑马程序员——Java基础:多线程及其应用
- 读写EXCEL
- Demacia of the Ancients