【poj2396】Budget 有源汇上下界可行流
2016-03-04 17:08
295 查看
Description
We are supposed to make a budget proposal for this multi-site competition. The budget proposal is a matrix where the rows represent different kinds of expenses and the columns represent different sites. We had a meeting about this, some time ago where we discussed the sums over different kinds of expenses and sums over different sites. There was also some talk about special constraints: someone mentioned that Computer Center would need at least 2000K Rials for food and someone from Sharif Authorities argued they wouldn’t use more than 30000K Rials for T-shirts. Anyway, we are sure there was more; we will go and try to find some notes from that meeting.And, by the way, no one really reads budget proposals anyway, so we’ll just have to make sure that it sums up properly and meets all constraints.
Input
The first line of the input contains an integer N, giving the number of test cases. The next line is empty, then, test cases follow: The first line of each test case contains two integers, m and n, giving the number of rows and columns (m <= 200, n <= 20). The second line contains m integers, giving the row sums of the matrix. The third line contains n integers, giving the column sums of the matrix. The fourth line contains an integer c (c < 1000) giving the number of constraints. The next c lines contain the constraints. There is an empty line after each test case.
Each constraint consists of two integers r and q, specifying some entry (or entries) in the matrix (the upper left corner is 1 1 and 0 is interpreted as “ALL”, i.e. 4 0 means all entries on the fourth row and 0 0 means the entire matrix), one element from the set {<, =, >} and one integer v, with the obvious interpretation. For instance, the constraint 1 2 > 5 means that the cell in the 1st row and 2nd column must have an entry strictly greater than 5, and the constraint 4 0 = 3 means that all elements in the fourth row should be equal to 3.
Output
For each case output a matrix of non-negative integers meeting the above constraints or the string “IMPOSSIBLE” if no legal solution exists. Put one empty line between matrices.Sample Input
2 2 3 8 10 5 6 7 4 0 2 > 2 2 1 = 3 2 3 > 2 2 3 < 5 2 2 4 5 6 7 1 1 1 > 10
Sample Output
2 3 3 3 3 4 IMPOSSIBLE
Source
Tehran 2003 Preliminary题意:给一个矩形,告诉你每行的和,每列的和,以及对某些格子填的数的区间的范围,然后求一个合法的填数方案。
和无源汇上下界可行流做法差不多,详见这里->【SGU194】Reactor Cooling 无源汇上下界可行流
把源和汇之间连一条边就当无源汇做就行了…
细节处理蛋疼……
顺便一说,为什么随便写一个题代码就这么长……
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<queue> using namespace std; const int INF = 0x3fffffff; const int SZ = 1000010; int head[SZ],nxt[SZ],tot = 1,n,m,s,e; struct edge{ int t; int d; }l[SZ]; void build(int f,int t,int d) { l[++ tot].t = t; l[tot].d = d; nxt[tot] = head[f]; head[f] = tot; } void insert(int f,int t,int d) { build(f,t,d); build(t,f,0); } int deep[SZ]; queue<int> q; bool bfs() { memset(deep,0,sizeof(deep)); deep[s] = 1; while(q.size()) q.pop(); q.push(s); while(q.size()) { int u = q.front(); q.pop(); for(int i = head[u];i;i = nxt[i]) { int v = l[i].t; if(!deep[v] && l[i].d) { deep[v] = deep[u] + 1; q.push(v); if(v == e) return true; } } } return false; } int dfs(int u,int flow) { if(u == e || flow == 0) return flow; int rest = flow; for(int i = head[u];i;i = nxt[i]) { int v = l[i].t; if(deep[v] == deep[u] + 1 && l[i].d) { int f = dfs(v,min(rest,l[i].d)); if(f > 0) { l[i].d -= f; l[i ^ 1].d += f; rest -= f; if(rest == 0) break; } else deep[v] = 0; } } if(flow == rest) deep[u] = 0; return flow - rest; } int dinic() { int ans = 0; while(bfs()) { int tmp = dfs(s,INF); if(!tmp) break; ans += tmp; } return ans; } int minn[233][233],maxn[233][233]; int inb[SZ],outb[SZ]; bool max_flow() { s = n * m + 3,e = n * m + 4; for(int u = 1;u <= n;u ++) { for(int v = 1;v <= m;v ++) { insert(u,v + n,maxn[u][v] - minn[u][v]); inb[v + n] += minn[u][v]; outb[u] += minn[u][v]; } } int sum = 0; for(int i = 1;i <= n + m + 2;i ++) { int tmp = inb[i] - outb[i]; if(tmp > 0) insert(s,i,tmp),sum += tmp; else insert(i,e,-tmp); } int ans = dinic(); // printf("%d %d\n",ans,sum); return ans == sum; } void change(int i,int j,char opt[],int z) { if(opt[0] == '>') minn[i][j] = max(minn[i][j],z + 1); else if(opt[0] == '=') minn[i][j] = maxn[i][j] = z; else maxn[i][j] = min(maxn[i][j],z - 1); } void deal(int x,int y,char opt[],int z) { if(x == 0 && y == 0) for(int i = 1;i <= n;i ++) for(int j = 1;j <= m;j ++) change(i,j,opt,z); else if(x == 0) for(int i = 1;i <= n;i ++) change(i,y,opt,z); else if(y == 0) for(int j = 1;j <= m;j ++) change(x,j,opt,z); else change(x,y,opt,z); } void init() { tot = 1; memset(head,0,sizeof(head)); memset(minn,0,sizeof(minn)); memset(maxn,63,sizeof(maxn)); memset(inb,0,sizeof(inb)); memset(outb,0,sizeof(outb)); } int main() { // freopen("haha.txt","r",stdin); // freopen("my.txt","w",stdout); int T; scanf("%d",&T); while(T --) { init(); scanf("%d%d",&n,&m); int s = n + m + 1,e = n + m + 2; for(int i = 1;i <= n;i ++) { int x; scanf("%d",&x); inb[i] += x; outb[s] += x; insert(s,i,0); } for(int i = 1;i <= m;i ++) { int x; scanf("%d",&x); insert(i + n,e,0); inb[e] += x; outb[i + n] += x; } int xz; scanf("%d",&xz); while(xz --) { int x,y; int z; char opt[3]; scanf("%d%d%s%d",&x,&y,opt,&z); deal(x,y,opt,z); } insert(e,s,INF); /* for(int i = 1;i <= n;i ++,puts("")) for(int j = 1;j <= m;j ++) printf("%11d",minn[i][j]); for(int i = 1;i <= n;i ++,puts("")) for(int j = 1;j <= m;j ++) printf("%11d",maxn[i][j]); */ if(!max_flow()) puts("IMPOSSIBLE"); else { for(int u = 1;u <= n;u ++) { for(int v = 1;v <= m;v ++) { int now = 3 + (n + m) * 2 + ((u - 1) * m + v) * 2; printf("%d ",minn[u][v] + l[now].d); // printf("%d ",3 + (n + m) * 2 + ((u - 1) * m + v) * 2); } puts(""); } } puts(""); } return 0; }
相关文章推荐
- huhx的android封神之路-------->ContentProvider的使用
- SQLServer数据库备份
- Java中通过jsch来连接远程服务器执行linux命令
- 【错误总结】makefile的编写问题:一定要注意不能有随随便便的tab、空格等
- 含有注释的SQL生成器升级
- android环境搭建
- java内存溢出和内存泄露
- iOS IQKeyBoardManager 引起的界面漂移bug 解决
- Android Studio 2.0 Beta 6 中配置强大的版本控制系统(Git篇)
- Spring Cache
- 百度地图经常崩溃的问题
- Cache缓存对象缓存对象
- 为什么你一直突破不了自己?
- 字符编码常识及问题解析
- 并发环境下mysql插入检查方案
- Android 5.0以上版本禁止锁屏与休眠
- 90、setStatusBarStyle无效问题 修改状态栏颜色 只有白色黑色可选UIStatusBarStyleLightContent
- jsonp跨域请求
- EventSource 对象用于接收服务器发送事件通知,是网页自动获取来自服务器的更新
- 二叉树:基本术语和重要性质