"Redraw Beautiful Drawings", "the third game of the multi-school race", "The Mystery のwa"

Source: Internet
Author: User

Reference: http://blog.csdn.net/qian99/article/details/38276887

#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring > #include <queue> #include <vector> #include <algorithm>using namespace std;int m,n,k;const int INF =1<<30;const int max_v=888;struct Edge {int to,cap,rev;}; vector<edge> g[max_v];bool used[max_v];bool vis[max_v];int mat[max_v][max_v];int level[MAX_V];int Iter[MAX_V];    void Add_edge (int from,int to,int cap) {G[from].push_back (edge) {to,cap,g[to].size ()}); G[to].push_back (Edge) {from,0,g[from].size ()-1});}    void BFs (int s) {memset (level,-1,sizeof (level));    Queue<int> que;    level[s]=0;    Que.push (s);        while (!que.empty ()) {int V=que.front (); Que.pop ();            for (int i=0;i<g[v].size (); i++) {Edge &e=G[v][i];                if (e.cap>0 && level[e.to]<0) {level[e.to]=level[v]+1;            Que.push (e.to);  }}}}int dfs (int v,int t,int f) {if (v==t) return F;  for (int &i=iter[v];i<g[v].size (); i++) {Edge &e=G[v][i];            if (e.cap>0 && level[v]<level[e.to]) {int D=dfs (e.to,t,min (F,e.cap));                if (d>0) {e.cap-=d;                G[e.to][e.rev].cap+=d;            return D; }}} return 0;}    int max_flow (int s,int t) {int flow=0; for (;;)        {BFS (s);        if (level[t]<0) return flow;        memset (Iter,0,sizeof (ITER));        int F;        while ((F=dfs (S,t,inf)) >0) {flow+=f;    }}}bool mydfs (int now,int fa) {//vis[now]=true;//Why did I add this to WA?        for (int i=0;i<g[now].size (); i++) {int to=g[now][i].to;        if (TO==FA) continue;            if (G[now][i].cap) {if (Vis[to]) return true;            Vis[to]=true;            if (Mydfs (To,now)) return true;        Vis[to]=false; }} return false;}    void Printmat () {memset (mat,0,sizeof (MAT)); for (int i=1;i<=n;i++) {for (int j=0;j<        G[i].size (); j + +) {mat[i][g[i][j].to-n]=k-g[i][j].cap; }} for (int i=1;i<=n;i++) {for (int j=n+1;j<=n+m;j++) {printf ("%d%c", mat[i][j-n],j==n+m? ')        \ n ': ');    }//printf ("\ n");    }}int Main () {#ifndef Online_judge freopen ("G:/in.txt", "R", stdin);    #endif//Online_judge int tmp;        while (~SCANF ("%d%d%d", &n,&m,&k)) {for (int i=0;i<max_v;i++) {g[i].clear ();        } memset (used,0,sizeof (used));        memset (vis,0,sizeof (VIS));        int s=0,t=n+m+1;        int sumn=0,summ=0;            for (int i=1;i<=n;i++) {scanf ("%d", &tmp);            sumn+=tmp;        Add_edge (S,I,TMP);            } for (int i=1;i<=m;i++) {scanf ("%d", &tmp);            summ+=tmp;        Add_edge (N+I,T,TMP);            } for (int i=1;i<=n;i++) {for (int j=n+1;j<=n+m;j++) {Add_edge (i,j,k); }} int Flow=max_flow (s,t);            if (summ!=sumn) {printf ("impossible\n");        Continue            } if (Flow!=summ) {printf ("impossible\n");        Continue        } memset (Vis,0,sizeof (VIS));        BOOL Flag=false;                for (int i=1;i<=n;i++) {if (Mydfs (i,s)) {flag=true;            Break        }} if (flag) printf ("Not unique\n");            else{printf ("unique\n");        Printmat (); }    }}


 

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.