"Multi-university Training Contest 3 1002"/"HDU 4888" Redraw Beautiful drawings

Source: Internet
Author: User

Not easy ah. Can finally fill the second question.

By the way: the template to write the residue do not blame the people ah ~ (This crippled template study for a long time to find fault)


Main topic:

There is a matrix of n*m. There will be a number in each lattice. Give you the sum of the numbers in each line and the number of each column.

Ask each position to fill in the number between 0~k. Suppose there are multiple possible outputs of "not Unique", assuming there is no output "impossible", assuming a set of solutions will output it.


Problem Solving Ideas:

Maximum flow: Impossible Condition: is the sum of rows and list and do not want to wait or build the maximum flow after the diagram with them do not want to wait.

The condition of multiple groups is that there is a ring with a length greater than 2 in the residual flow network after the maximum flow (the minimum is greater than 3 in practice).


Here's the code:

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include < vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h > #include <stdlib.h> #define EPS 1e-9#define pi acos ( -1.0) #define INF 107374182 #define INF64 115292150460684697 6 #define CLEAR1 (A, x, size) memset (A, X, sizeof (A[0)) * (size)) #define ClearAll (A, X) memset (A, X, sizeof (a)) #define Mem   Copy1 (A, X, SIZE) memcpy (A, X, sizeof (X[0)) * (size)) #define Memcopy1all (A, X) memcpy (A, X, sizeof (x)) #define MAX (x, y) (((x) > (y))?

(x): (y)) #define min (x, y) (((x) < (y))? (x): (y)) using namespace std;const int maxn=1000+10;const int maxm=200000+10;struct node{int v,w,next; Node (int v=0,int w=0,int next=0): V (v), W (W), Next (next) {};} Edge[maxm<<1];int head[maxn],d[maxn],nedge,s,t;bool vis[maxn];int n,m,k;void addedge (int from,int to,int cap) {E Dge[++nedge]=node (To,cap,head[from]); Head[from]=nedge; Edge[++nedge]=node (From,0,head[to]); Head[to]=nedge;} int Deep[maxn];bool BFs () {memset (deep,-1,sizeof (deep)); queue<int>q; Q.push (S);//source point deep[s]=0; while (!q.empty ()) {int U=q.front (); Q.pop (); int P=head[u]; while (p!=-1) {int v=edge[p].v; if (deep[v]==-1&&edge[p].w>0) {Q.push (v); deep[v]=deep[u]+1; } P=edge[p].next; }} return deep[t]!=-1; Meeting point}int DFS (int src, int flow) {if (src==t| | flow==0) return flow; int sum=0,temp; int P=HEAD[SRC]; while (p!=-1) {int v=edge[p].v; if (deep[v]==deep[src]+1&&edge[p].w>0) {Temp=dfs (V,min (FLOW-SUM,EDGE[P].W)); Edge[p].w-=temp; Edge[p^1].w+=temp; Sum+=temp; if (flow-sum==0) return sum; } P=edge[p].next; } deep[src]=-1; return sum;} BOOL Dfs (int u,int fa) {for (int k=head[u]; k!=-1; k=edge[k].next) {if (k== (fa^1)) continue; if (EDGE[K].W) {if (VIS[EDGE[K].V]) return true; Vis[edge[k].v]=true; if (Dfs (EDGE[K].V,K)) return true; Vis[edge[k].v]=false; }} return false;} int mat[404][404];void Ptmat () {printf ("unique\n"); memset (Mat,0,sizeof (MAT)); for (int u=1, u<=n; ++u) {for (int k=head[u]; k!=-1; k=edge[k].next) {int v=edge[k].v; if (v>n&&v<=n+m) MAT[U][V-N]=K-EDGE[K].W; }} foR (int i=1; i<=n; ++i) {for (int j=1; j<=m; ++j) {if (j>1) printf (""); printf ("%d", mat[i][j]); } printf ("\ n"); }}int Main () {while (scanf ("%d%d%d", &n,&m,&k)!=eof) {memset (head,-1,sizeof (head)); Nedge=-1; s=0,t=n+m+1; int sum1=0,sum2=0,w; for (int i=1; i<=n; ++i) {scanf ("%d", &w); Sum1+=w; Addedge (S,I,W); for (int j=1; j<=m; ++j) Addedge (i,j+n,k); } for (int i=1; i<=m; ++i) {scanf ("%d", &w); Sum2+=w; Addedge (I+N,T,W); } if (sum1!=sum2) printf ("impossible\n"); else {int flow=0; while (BFS ())) Flow+=dfs (S,inf); if (FLOW!=SUM1) printf ("impossible\n"); else {memset (vis,0,sizeof (VIS)); BOOL Flag=false; for (int i=1; i<=N ++i) {if (Dfs (i,-1)) {flag=true; Break }} if (flag) printf ("Not unique\n"); else Ptmat (); }}} return 0;}



"Multi-university Training Contest 3 1002"/"HDU 4888" Redraw Beautiful drawings

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.