"BNUOJ19500" Matrix decompressing

Source: Internet
Author: User

HTTPS://WWW.BNUOJ.COM/V3/PROBLEM_SHOW.PHP?PID=19500 (Topic link)

Test instructions

A positive integer matrix of the R row C column is given, and the first ${a_i}$ item is the sum of all the elements of its front I row, the sum of all the elements of the preceding I column, the ${b_i}$ term is known r,c,a,b, and a matrix satisfying the condition is found. Each of these elements is a positive integer of 1~20.

Solution

To see this type of matrix, the first consideration is to construct a binary graph, the left set represents the row, and the right set represents the column, where each edge represents a point.

After this is done, we find that we can use the upper and lower bounds of the network flow to solve this problem. Add source points to the left set with an upper bound of ${a_i}$, the lower bound is also ${a_i}$; each point on the right set adds an upper bound of ${b_i}$ to the additional sinks, the lower bound is the ${b_i}$ edge, and the left set has an upper bound of 20 and 1 edges for each two points.

Code half, found that actually do not need to go up and down the bounds, directly to each element-1, the nether is 0, because the sum of rows and list and values equal, so the feasible flow is the maximum flow.

Details

The subscript of the adjacency table starts from 1.

Code
bnuoj19500#include<algorithm> #include <iostream> #include <cstring> #include <cstdlib># include<cstdio> #include <cmath> #include <queue> #define LL long long#define inf 2147483640#define MOD 10000#define Pi ACOs ( -1.0) #define FREE (a) freopen (a ".", "R", stdin), Freopen (a ". Out", "w", stdout), using namespace std; const int maxn=1010;struct Edge {int to,next,w;} E[maxn<<1];int head[maxn],d[maxn],r[maxn],c[maxn],a[maxn][maxn];int n,m,cnt=1,ans;void Link (int u,int v,int W) {e[++cnt]= (Edge) {v,head[u],w};head[u]=cnt;e[++cnt]= (edge) {u,head[v],0};head[v]=cnt;} void Init () {memset (head,0,sizeof (head)); cnt=1;ans=0;} BOOL BFs () {memset (d,-1,sizeof (d));queue<int> Q;q.push (1);d [1]=0;while (!q.empty ()) {int X=q.front (); Q.pop (); for (int i=head[x];i;i=e[i].next) if (e[i].w && d[e[i].to]<0) {D[e[i].to]=d[x]+1;q.push (e[i].to);}} return d[n+m+2]>0;} int dfs (int x,int f) {if (x==n+m+2 | | f==0) return f;int w,used=0;for (int i=head[x];i;i=e[i].next) if (e[i].W && d[e[i].to]==d[x]+1) {W=dfs (E[i].to,min (e[i].w,f-used)); Used+=w;e[i].w-=w;e[i^1].w+=w;if (used==f) return used;} if (!used) D[x]=-1;return used;} void Dinic () {while (BFS ()) Ans+=dfs (1,inf);} int main () {int t;scanf ("%d", &t); for (int case=1; case<=t; case++) {Init (); scanf ("%d%d", &n,&m), for (Int. i=1;i<=n;i++) scanf ("%d", &r[i]); for (int i=1;i<=m;i++ ) scanf ("%d", &c[i]), for (int i=n;i;i--) r[i]-=r[i-1];for (int i=m;i;i--) c[i]-=c[i-1];for (int i=1;i<=n;i++) Link (1,I+1,R[I]-M), for (int j=1;j<=m;j++) Link (j+n+1,n+m+2,c[j]-n), for (Int. i=1;i<=n;i++) for (int j=1;j<=m;j++) Link (i+1,j+n+1,19);D inic ();p rintf ("Matrix%d\n", case), and for (int i=1;i<=n;i++) for (int j=head[i+1];j;j=e[j].next) if (e[j].to>i) a[i][e[j].to-n-1]=20-e[j].w;for (int i=1;i<=n;i++) {for (int j=1;j<=m;j++) printf ("%d", A[i][j] );p UTS ("");} if (case<t) puts ("");} return 0;}

  

"BNUOJ19500" Matrix decompressing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.