標籤:acm algorithm 圖論 網路流
http://acm.hdu.edu.cn/showproblem.php?pid=4975
A simple Gaussian elimination problem.
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 669 Accepted Submission(s): 222
Problem DescriptionDragon is studying math. One day, he drew a table with several rows and columns, randomly wrote numbers on each elements of the table. Then he counted the sum of each row and column. Since he thought the map will be useless after he got the sums, he destroyed the table after that.
However Dragon‘s mom came back and found what he had done. She would give dragon a feast if Dragon could reconstruct the table, otherwise keep Dragon hungry. Dragon is so young and so simple so that the original numbers in the table are one-digit number (e.g. 0-9).
Could you help Dragon to do that?
InputThe first line of input contains only one integer, T(<=30), the number of test cases. Following T blocks, each block describes one test case.
There are three lines for each block. The first line contains two integers N(<=500) and M(<=500), showing the number of rows and columns.
The second line contains N integer show the sum of each row.
The third line contains M integer show the sum of each column.
OutputEach output should occupy one line. Each line should start with "Case #i: ", with i implying the case number. For each case, if we cannot get the original table, just output: "So naive!", else if we can reconstruct the table by more than one ways, you should output one line contains only: "So young!", otherwise (only one way to reconstruct the table) you should output: "So simple!".
Sample Input
31 1552 20 100 102 22 22 2
Sample Output
Case #1: So simple!Case #2: So naive!Case #3: So young!
Source2014 Multi-University Training Contest 10
出題人是個蛤粉,蛤蛤蛤蛤蛤蛤蛤蛤蛤。。。。
題意:
每個格子只能填0~9這10個整數,給出行和及列和,求是否有合法方案,如果有並判斷唯一性。
分析:
看到這題就感覺熟悉啊,和之前某場多校的題目是一樣的,當初還不會網路流呢,現在雖然還不會建圖,但是套套模板還是沒有問題的。
建立二分圖,行為X部,列為Y部,每個X部的點向Y部連一條容量為9的邊,增加源點S,S向X部的所有點連邊,容量為行和,增加匯點,每個Y部的點向匯點連邊,容量為列和,在該圖中跑一邊網路最大流,如果滿流則有合法方案。然後在殘留網路中找環(不要立即走反向弧),如果有環則有多種。找環之前最好重建立圖,這樣能避免判斷大量滿流邊。
#include <cstdio>#include <algorithm>#include <cstring>#define LL long long#define itn int#define maxn 1007#define maxm 2333333#define INF 0x3f3f3f3fusing namespace std;int a[maxn],b[maxn];int fir[maxn];itn u[maxm],v[maxm],cap[maxm],flow[maxm],nex[maxm];int e_max;itn q[maxn<<2];itn lv[maxn],iter[maxn];void add_edge(int _u,int _v,int _w){ int e=e_max++; u[e]=_u;v[e]=_v;cap[e]=_w; nex[e]=fir[u[e]];fir[u[e]]=e; e=e_max++; u[e]=_v;v[e]=_u;cap[e]=0; nex[e]=fir[u[e]];fir[u[e]]=e;}void dinic_bfs(itn s){ int f,r; lv[s]=0; q[f=r=0]=s; while (f<=r) { int x=q[f++]; for (int e=fir[x];~e;e=nex[e]) { if (cap[e]>flow[e] && lv[v[e]]<0) { lv[v[e]]=lv[u[e]]+1; q[++r]=v[e]; } } }}int dinic_dfs(int s,int t,int _f){ if (s==t) return _f; for (int &e=iter[s];~e;e=nex[e]) { if (cap[e]>flow[e] && lv[s]<lv[v[e]]) { int _d=dinic_dfs(v[e],t,min(cap[e]-flow[e],_f)); if (_d>0) { flow[e]+=_d; flow[e^1]-=_d; return _d; } } } return 0;}itn max_flow(int s,int t){ int total_flow=0; memset(flow,0,sizeof flow); for (;;) { memset(lv,-1,sizeof lv); dinic_bfs(s); if (lv[t]==-1) break; memcpy(iter,fir,sizeof fir); itn _f=0; while ((_f=dinic_dfs(s,t,INF))>0) total_flow+=_f; } return total_flow;}int vis[maxn];bool dfs(itn s,int iter){ for (int e=fir[s];~e;e=nex[e]) { if ((e^1)!=iter) { if (vis[v[e]]==-1) return true; vis[v[e]]=-1; if (dfs(v[e],e)) return true; vis[v[e]]=0; } } return false;}int main(){ int n,m; itn T_T,cas=0; scanf("%d",&T_T); while(T_T--) { printf("Case #%d: ",++cas); scanf("%d%d",&n,&m); itn s=0,t=n+m+1; itn sr=0,sc=0; e_max=0; memset(fir,-1,sizeof fir); for (int i=1;i<=n;i++) { scanf("%d",a+i); add_edge(s,i,a[i]); sr+=a[i]; } for (int i=1;i<=m;i++) { scanf("%d",b+i); add_edge(i+n,t,b[i]); sc+=b[i]; } if (sr!=sc) { printf("So naive!\n"); continue; } for (int i=1;i<=n;i++) { for (int j=1;j<=m;j++) { add_edge(i,j+n,9); } } int res=max_flow(s,t); if (res!=sr) { printf("So naive!\n"); continue; } itn mm=e_max; e_max=0; memset(fir,-1,sizeof fir); for (int e=0;e<mm;e++) { if (cap[e]>flow[e]) { u[e_max]=u[e]; v[e_max]=v[e]; nex[e_max]=fir[u[e_max]]; fir[u[e_max]]=e_max++; } } memset(vis,0,sizeof vis); bool cir=false; for (int i=1;i<=n;i++) { if (vis[i]==0 && dfs(i,-1)) { cir=true; break; } } if (cir) { printf("So young!\n"); } else { printf("So simple!\n"); } } return 0;}
HDU 4975 A simple Gaussian elimination problem.(網路最大流)