有上下界的最大流解方程组的解
http://poj.org/problem?id=2396
Budget
Time Limit: 3000MS | Memory Limit: 65536K | |||
Total Submissions: 5873 | Accepted: 2229 | Special Judge |
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.
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.
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
题意:给出一个n*m的矩阵,然后给出每行和每列的和,接下来c行,每行对矩阵有限制要求,包括(a b) c d,(0,0)代表对全部矩阵进行限制,(0,b)代表对b列的所有元素进行限制,(a,0)代表对a行整个元素进行限制,(a,b)代表对改坐标的元素进行限制;然后判断是否有解,如果有解的话输出该矩阵,否则输出impossible;
分析:加入此题没有下线的时候和直接用最大流解的建边方式相同,即:建立一个虚拟源点st和一个虚拟汇点sd,st和每行进行连边,权值给行和,每列与sd建边权值是列和,然后每行和每列两两建边,对应的容量为具有下限的权值,然后求解的过程就转化为了具有源汇点的有上下界的网络流,接下来的建边就不多说了。
需要注意的地方:此题的建边略微麻烦,首先对于大于和小于号,每个点大大取较大,小小取较小,在比较等于是否冲突,如果冲突则不行,然后比较如果每个点的下限小于等于上限,才能形成建边条件,否则也是无解的;
程序:
#include"stdio.h" #include"string.h" #include"queue" #include"stdlib.h" #include"iostream" #include"algorithm" #define M 300 #define inf 100000000 using namespace std; struct node { int u,v,w,c,next; }edge[M*M]; int t,head[M],work[M],dis[M]; void init() { t=0; memset(head,-1,sizeof(head)); } void add(int u,int v,int w,int c) { edge[t].u=u; edge[t].v=v; edge[t].w=w; edge[t].c=c; edge[t].next=head[u]; head[u]=t++; edge[t].u=v; edge[t].v=u; edge[t].w=0; edge[t].c=c; edge[t].next=head[v]; head[v]=t++; } void Clear(int u,int v,int w,int c) { edge[t].u=0; edge[t].v=0; edge[t].w=0; edge[t].c=0; edge[t].next=head[u]; head[u]=-1; edge[t].u=0; edge[t].v=0; edge[t].w=0; edge[t].c=0; edge[t].next=head[v]; head[v]=-1; } int bfs(int start,int endl) { queue<int>q; memset(dis,-1,sizeof(dis)); q.push(start); dis[start]=0; while(!q.empty()) { int u=q.front(); q.pop(); for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(edge[i].w&&dis[v]==-1) { dis[v]=dis[u]+1; q.push(v); if(v==endl) return 1; } } } return 0; } int dfs(int u,int a,int endl) { if(u==endl) return a; for(int &i=work[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(edge[i].w&&dis[v]==dis[u]+1) { int tt=dfs(v,min(a,edge[i].w),endl); if(tt) { edge[i].w-=tt; edge[i^1].w+=tt; return tt; } } } return 0; } int Dinic(int start,int endl) { int ans=0; while(bfs(start,endl)) { memcpy(work,head,sizeof(head)); while(int tt=dfs(start,inf,endl)) ans+=tt; } return ans; } struct L { int l,r; }mp[222][22]; int main() { int T,m,k,n,i,j,row[222],col[222],sum,ans1; cin>>T; while(T--) { scanf("%d%d",&n,&m); for(i=1;i<=n;i++) scanf("%d",&row[i]); for(j=1;j<=m;j++) scanf("%d",&col[j]); scanf("%d",&k); init(); int a,b,c; char str[3]; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { mp[i][j].l=0; mp[i][j].r=inf/100; } } int fuck=0; int kk=0; while(k--) { scanf("%d%d%s%d",&a,&b,str,&c); if(str[0]=='>') { if(a==0&&b==0) { for(i=1;i<=n;i++) for(j=1;j<=m;j++) mp[i][j].l=max(mp[i][j].l,c+1); } else if(a==0&&b) { for(i=1;i<=n;i++) mp[i][b].l=max(mp[i][b].l,c+1); } else if(a&&b==0) { for(j=1;j<=m;j++) mp[a][j].l=max(mp[a][j].l,c+1); } else { mp[a][b].l=max(mp[a][b].l,c+1); } } else if(str[0]=='<') { if(a==0&&b==0) { for(i=1;i<=n;i++) for(j=1;j<=m;j++) mp[i][j].r=min(mp[i][j].r,c-1); } else if(a==0&&b) { for(i=1;i<=n;i++) mp[i][b].r=min(mp[i][b].r,c-1); } else if(a&&b==0) { for(j=1;j<=m;j++) mp[a][j].r=min(mp[a][j].r,c-1); } else { mp[a][b].r=min(mp[a][b].r,c-1); } } else { if(a==0&&b==0) { for(i=1;i<=n;i++) for(j=1;j<=m;j++) { if(c>=mp[i][j].l&&c<=mp[i][j].r) mp[i][j].l=mp[i][j].r=c; else fuck++; } } else if(a==0&&b) { for(i=1;i<=n;i++) { if(c>=mp[i][b].l&&c<=mp[i][b].r) mp[i][b].l=mp[i][b].r=c; else fuck++; } } else if(a&&b==0) { for(j=1;j<=m;j++) { if(c>=mp[a][j].l&&c<=mp[a][j].r) mp[a][j].l=mp[a][j].r=c; else fuck++; } } else { if(c>=mp[a][b].l&&c<=mp[a][b].r) mp[a][b].l=mp[a][b].r=c; else fuck++; } } } for(i=1;i<=n;i++) { for(j=1;j<=m;j++) if(mp[i][j].r<mp[i][j].l) fuck++; } if(kk!=0) printf("\n"); kk++; if(fuck)//限制条件自相矛盾 { printf("IMPOSSIBLE\n"); continue; } init(); int st=0; int sd=m+n+1; int source=m+n+2; int sink=m+n+3; for(i=1;i<=n;i++) for(j=1;j<=m;j++) add(i,n+j,mp[i][j].r-mp[i][j].l,mp[i][j].r); int cnt=t; for(i=1;i<=n;i++) add(st,i,row[i],0); for(j=1;j<=m;j++) add(j+n,sd,col[j],0); sum=0; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { add(i,sink,mp[i][j].l,0); add(source,j+n,mp[i][j].l,0); sum+=mp[i][j].l; } } add(sd,st,inf,0); ans1=Dinic(source,sink); if(ans1!=sum)//此题无解 { printf("IMPOSSIBLE\n"); continue; } Dinic(st,sd);//走完剩余流量 int pp=1; for(i=0;i<cnt;i+=2) { if(pp%m==1||(m==1&&pp%m==0)) printf("%d",edge[i].c-edge[i].w); else printf(" %d",edge[i].c-edge[i].w); if(pp%m==0) printf("\n"); pp++; } } }