「POJ2396」Budget
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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
1 2 3 4 |
2 3 3 3 3 4 IMPOSSIBLE |
题解
真是坑爹题,搞了半个晚上。。。
首先建图不难
每一行的和为x,S到每行连[x,x]的边
每一列的和为y,每列到T连[y,y]的边
对于一个点i,j,i行向j列连[l,r]的边
然后正常的有源上下界网络流
还要处理输入自身的矛盾
注意初始化,并且l和r初始为+-1000即可,否则可能爆long long
注意输入是>和<,不能理解为>=和<=
注意数组大小
注意测试数据有负数
注意读完数据,不能读入过程中发现矛盾就退出了,有人说似乎读入每组数据最后要读入一个空行
此题不会卡常数,tle就是写错了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
#include<iostream> #include<cstdio> #include<cstring> #define inf 1000000000 using namespace std; inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x*=10;x+=ch-'0';ch=getchar();} return x*f; } bool flag; int S,T,SS,TT; int n,m,cnt; int head[1005],cur[1005],h[1005],q[1005],in[1005]; int l[305][305],r[305][305],ans[305][305]; struct data{int to,next,v;}e[1000005]; void ins(int u,int v,int w) {e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;e[cnt].v=w;} void insert(int u,int v,int w) {ins(u,v,w);ins(v,u,0);} bool bfs() { memset(h,-1,sizeof(h)); int t=0,w=1;q[0]=SS;h[SS]=0; while(t!=w) { int now=q[t];t++; for(int i=head[now];i;i=e[i].next) if(e[i].v&&h[e[i].to]==-1) { h[e[i].to]=h[now]+1; q[w++]=e[i].to; } } if(h[TT]==-1)return 0; return 1; } int dfs(int x,int f) { if(x==TT)return f; int w,used=0; for(int i=cur[x];i;i=e[i].next) if(h[e[i].to]==h[x]+1) { w=f-used;w=dfs(e[i].to,min(e[i].v,w)); e[i].v-=w;if(e[i].v)cur[x]=i;e[i^1].v+=w; used+=w;if(used==f)return f; } if(!used)h[x]=1; return used; } void dinic() {while(bfs()){for(int i=0;i<=TT;i++)cur[i]=head[i];dfs(SS,inf);}} bool solve() { for(int i=S;i<=T;i++) { if(in[i]>0)insert(SS,i,in[i]); if(in[i]<0)insert(i,TT,-in[i]); } dinic(); for(int i=head[SS];i;i=e[i].next) if(e[i].v)return 0; return 1; } void jud(int x,int y,int v) {if(v<l[x][y]||v>r[x][y])flag=1;} int main() { int test=read(); while(test--) { for(int i=0;i<=300;i++) for(int j=0;j<=300;j++) l[i][j]=-1000,r[i][j]=1000; memset(head,0,sizeof(head));cnt=1; memset(in,0,sizeof(in)); flag=0; n=read();m=read(); S=0;T=n+m+1; SS=T+1;TT=SS+1; int x; for(int i=1;i<=n;i++) x=read(),insert(S,i,0),in[S]-=x,in[i]+=x; for(int i=1;i<=m;i++) x=read(),insert(n+1,T,0),in[n+i]-=x,in[T]+=x; int q=read(); while(q--) { char ch[5]; int x=read(),y=read(); scanf("%s",ch);int v=read(); if(x==0&&y!=0) for(int j=1;j<=n;j++) { if(ch[0]=='>')l[j][y+n]=max(l[j][y+n],v+1); else if(ch[0]=='='){jud(j,y+n,v);l[j][y+n]=r[j][y+n]=v;} else r[j][y+n]=min(r[j][y+n],v-1); } if(x!=0&&y==0) for(int j=1;j<=m;j++) { if(ch[0]=='>')l[x][j+n]=max(l[x][j+n],v+1); else if(ch[0]=='='){jud(x,j+n,v);l[x][j+n]=r[x][j+n]=v;} else r[x][j+n]=min(r[x][j+n],v-1); } if(x==0&&y==0) for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { if(ch[0]=='>')l[i][j+n]=max(l[i][j+n],v+1); else if(ch[0]=='='){jud(i,j+n,v);l[i][j+n]=r[i][j+n]=v;} else r[i][j+n]=min(r[i][j+n],v-1); } if(x!=0&&y!=0) { if(ch[0]=='>')l[x][y+n]=max(l[x][y+n],v+1); else if(ch[0]=='='){jud(x,y,v);l[x][y+n]=r[x][y+n]=v;} else r[x][y+n]=min(r[x][y+n],v-1); } } for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { if(r[i][j+n]<l[i][j+n])flag=1; insert(i,j+n,r[i][j+n]-l[i][j+n]); in[i]-=l[i][j+n]; in[j+n]+=l[i][j+n]; } if(flag){printf("IMPOSSIBLE\n");continue;} insert(T,S,inf); if(solve()) { int t=n+m+1; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { printf("%d",e[(t<<1)^1].v+l[i][j+n]); if(j!=m)printf(" "); t++; } puts(""); } } else printf("IMPOSSIBLE\n"); } return 0; } |
这道题报道上出现了偏差,zoj上wa了