网络最大流解方程组
2014-07-30 18:34
316 查看
多校联赛第三场(hdu4888)
Total Submission(s): 1320 Accepted Submission(s): 268
[align=left]Problem Description[/align]
Alice and Bob are playing together. Alice is crazy about art and she has visited many museums around the world. She has a good memory and she can
remember all drawings she has seen.
Today Alice designs a game using these drawings in her memory. First, she matches K+1 colors appears in the picture to K+1 different integers(from 0 to K). After that, she slices the drawing into grids and there are N rows and M columns. Each grid has an integer
on it(from 0 to K) representing the color on the corresponding position in the original drawing. Alice wants to share the wonderful drawings with Bob and she tells Bob the size of the drawing, the number of different colors, and the sum of integers on each
row and each column. Bob has to redraw the drawing with Alice's information. Unfortunately, somtimes, the information Alice offers is wrong because of Alice's poor math. And sometimes, Bob can work out multiple different drawings using the information Alice
provides. Bob gets confused and he needs your help. You have to tell Bob if Alice's information is right and if her information is right you should also tell Bob whether he can get a unique drawing.
[align=left]Input[/align]
The input contains mutiple testcases.
For each testcase, the first line contains three integers N(1 ≤ N ≤ 400) , M(1 ≤ M ≤ 400) and K(1 ≤ K ≤ 40).
N integers are given in the second line representing the sum of N rows.
M integers are given in the third line representing the sum of M columns.
The input is terminated by EOF.
[align=left]Output[/align]
For each testcase, if there is no solution for Bob, output "Impossible" in one line(without the quotation mark); if there is only one solution for Bob, output "Unique" in one line(without the quotation
mark) and output an N * M matrix in the following N lines representing Bob's unique solution; if there are many ways for Bob to redraw the drawing, output "Not Unique" in one line(without the quotation mark).
[align=left]Sample Input[/align]
2 2 4
4 2
4 2
4 2 2
2 2 5 0
5 4
1 4 3
9
1 2 3 3
[align=left]Sample Output[/align]
Not Unique
Impossible
Unique
1 2 3 3
题意:给出一个n行m列的矩阵和一个整数k,接下来n个数第i个数代表第i行所有数的总和,接下来给出m个数,第i个数代表第i列所有数的总和,问是否存在这样一个矩阵满足条件,且矩阵中的每个数都不大于k,不存在输出impossible,存在且有唯一解输出该矩阵,否则输出Not Unique;
建图:建立二部图,把行标列为x部,把列表列为Y部,设立源点source和汇点sink,把行标和源点建边,流量是行和,把列标和汇点建边流量是列和,然后把行标和列标两两相连,代表第i行第j列的数,流量为k,跑完一次最大流(Dinic或Isap)如果最大流量不等于所有数的总和,则无解,如果存在残留网络中存在一个正环(顶点个数>2且环的残余流量>0)则为多解,否则为唯一解,然后遍历遍历完所有的行标i以及行标j对应的列标,则<i,j>所代表的反向边的流量就是坐标为(i,j)的值,存储记录输出即可;
程序;
Redraw Beautiful Drawings
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1320 Accepted Submission(s): 268
[align=left]Problem Description[/align]
Alice and Bob are playing together. Alice is crazy about art and she has visited many museums around the world. She has a good memory and she can
remember all drawings she has seen.
Today Alice designs a game using these drawings in her memory. First, she matches K+1 colors appears in the picture to K+1 different integers(from 0 to K). After that, she slices the drawing into grids and there are N rows and M columns. Each grid has an integer
on it(from 0 to K) representing the color on the corresponding position in the original drawing. Alice wants to share the wonderful drawings with Bob and she tells Bob the size of the drawing, the number of different colors, and the sum of integers on each
row and each column. Bob has to redraw the drawing with Alice's information. Unfortunately, somtimes, the information Alice offers is wrong because of Alice's poor math. And sometimes, Bob can work out multiple different drawings using the information Alice
provides. Bob gets confused and he needs your help. You have to tell Bob if Alice's information is right and if her information is right you should also tell Bob whether he can get a unique drawing.
[align=left]Input[/align]
The input contains mutiple testcases.
For each testcase, the first line contains three integers N(1 ≤ N ≤ 400) , M(1 ≤ M ≤ 400) and K(1 ≤ K ≤ 40).
N integers are given in the second line representing the sum of N rows.
M integers are given in the third line representing the sum of M columns.
The input is terminated by EOF.
[align=left]Output[/align]
For each testcase, if there is no solution for Bob, output "Impossible" in one line(without the quotation mark); if there is only one solution for Bob, output "Unique" in one line(without the quotation
mark) and output an N * M matrix in the following N lines representing Bob's unique solution; if there are many ways for Bob to redraw the drawing, output "Not Unique" in one line(without the quotation mark).
[align=left]Sample Input[/align]
2 2 4
4 2
4 2
4 2 2
2 2 5 0
5 4
1 4 3
9
1 2 3 3
[align=left]Sample Output[/align]
Not Unique
Impossible
Unique
1 2 3 3
题意:给出一个n行m列的矩阵和一个整数k,接下来n个数第i个数代表第i行所有数的总和,接下来给出m个数,第i个数代表第i列所有数的总和,问是否存在这样一个矩阵满足条件,且矩阵中的每个数都不大于k,不存在输出impossible,存在且有唯一解输出该矩阵,否则输出Not Unique;
建图:建立二部图,把行标列为x部,把列表列为Y部,设立源点source和汇点sink,把行标和源点建边,流量是行和,把列标和汇点建边流量是列和,然后把行标和列标两两相连,代表第i行第j列的数,流量为k,跑完一次最大流(Dinic或Isap)如果最大流量不等于所有数的总和,则无解,如果存在残留网络中存在一个正环(顶点个数>2且环的残余流量>0)则为多解,否则为唯一解,然后遍历遍历完所有的行标i以及行标j对应的列标,则<i,j>所代表的反向边的流量就是坐标为(i,j)的值,存储记录输出即可;
程序;
#include"stdio.h" #include"string.h" #include"iostream" #include"map" #include"string" #include"queue" #include"stdlib.h" #include"math.h" #define M 200000 #define N 10000 #define eps 1e-10 #define inf 1000000000 #define mod 2333333 using namespace std; struct node { int u,v,w,next; }edge[M*2]; int t,head ,row ,col ,q ,dis ,work ,use ,flag; void init() { t=0; memset(head,-1,sizeof(head)); } void add(int u,int v,int w) { edge[t].u=u; edge[t].v=v; edge[t].w=w; edge[t].next=head[u]; head[u]=t++; edge[t].u=v; edge[t].v=u; edge[t].w=0; edge[t].next=head[v]; head[v]=t++; } int bfs(int S,int T) { int rear=0; memset(dis,-1,sizeof(dis)); dis[S]=0; q[rear++]=S; for(int i=0;i<rear;i++) { for(int j=head[q[i]];j!=-1;j=edge[j].next) { int v=edge[j].v; if(edge[j].w&&dis[v]==-1) { dis[v]=dis[q[i]]+1; q[rear++]=v; if(v==T) return 1; } } } return 0; } int dfs(int cur,int a,int T) { if(cur==T) return a; for(int &i=work[cur];i!=-1;i=edge[i].next) { int v=edge[i].v; if(edge[i].w&&dis[v]==dis[cur]+1) { int tt=dfs(v,min(a,edge[i].w),T); if(tt) { edge[i].w-=tt; edge[i^1].w+=tt; return tt; } } } return 0; } int Dinic(int S,int T) { int ans=0; while(bfs(S,T)) { memcpy(work,head,sizeof(head)); while(int tt=dfs(S,inf,T)) ans+=tt; } return ans; } int mp[444][444]; int DFS(int u,int f) { use[u]=1; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(edge[i].w&&v!=f) { if(use[v]) return 1; if(DFS(v,u)) return 1; } } use[u]=0; return 0; } int judge(int n,int m) { //printf("%d\n",DFS(1,1)); for(int i=1;i<=n;i++) { memset(use,0,sizeof(use)); if(DFS(i,i)) return 1; } return 0; } int main() { int n,m,k,i,j; while(scanf("%d%d%d",&n,&m,&k)!=-1) { int r=0,c=0; for(i=1;i<=n;i++) { scanf("%d",&row[i]); r+=row[i]; } for(j=1;j<=m;j++) { scanf("%d",&col[j]); c+=col[j]; } if(r!=c) { printf("Impossible\n"); continue; } init(); for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { add(i,n+j,k); } } for(i=1;i<=n;i++) add(0,i,row[i]); for(j=1;j<=m;j++) add(j+n,m+n+1,col[j]); int ans=Dinic(0,m+n+1); if(ans<r) { printf("Impossible\n"); continue; } if(judge(n,m)) { printf("Not Unique\n"); continue; } printf("Unique\n"); t=0; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { if(j==1) printf("%d",edge[t^1].w); else printf(" %d",edge[t^1].w); t=t+2; } printf("\n"); } } return 0; }
</pre><pre code_snippet_id="438794" snippet_file_name="blog_20140730_1_6325119" name="code" class="cpp">加矩阵DP优化判是否唯一:时间171ms
#include"stdio.h" #include"string.h" #include"iostream" #include"map" #include"string" #include"queue" #include"stdlib.h" #include"math.h" #define M 800000 #define N 1900 #define eps 1e-10 #define inf 1000000000 #define mod 2333333 using namespace std; struct node { int u,v,w,next; }edge[M]; int t,head ,row ,col ,q ,dis ,work ,use ,flag; void init() { t=0; memset(head,-1,sizeof(head)); } void add(int u,int v,int w) { edge[t].u=u; edge[t].v=v; edge[t].w=w; edge[t].next=head[u]; head[u]=t++; edge[t].u=v; edge[t].v=u; edge[t].w=0; edge[t].next=head[v]; head[v]=t++; } int bfs(int S,int T) { int rear=0; memset(dis,-1,sizeof(dis)); dis[S]=0; q[rear++]=S; for(int i=0;i<rear;i++) { for(int j=head[q[i]];j!=-1;j=edge[j].next) { int v=edge[j].v; if(edge[j].w&&dis[v]==-1) { dis[v]=dis[q[i]]+1; q[rear++]=v; if(v==T) return 1; } } } return 0; } int dfs(int cur,int a,int T) { if(cur==T) return a; for(int &i=work[cur];i!=-1;i=edge[i].next) { int v=edge[i].v; if(edge[i].w&&dis[v]==dis[cur]+1) { int tt=dfs(v,min(a,edge[i].w),T); if(tt) { edge[i].w-=tt; edge[i^1].w+=tt; return tt; } } } return 0; } int Dinic(int S,int T) { int ans=0; while(bfs(S,T)) { memcpy(work,head,sizeof(head)); while(int tt=dfs(S,inf,T)) ans+=tt; } return ans; } int mp[555][555]; int G[555][555]; int judge(int n,int m,int k) { int i,j,r; t=0; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { G[i][j]=edge[t^1].w; t+=2; } } memset(mp,0,sizeof(mp)); for(i=1;i<=n;i++) { if(row[i]==0||row[i]==k*m)continue; for(j=1;j<=m;j++) { if(col[j]==0||col[j]==k*n)continue; for(r=j+1;r<=m;r++) { int f1=0,f2=0; if(G[i][j]<k&&G[i][r]>0) { if(mp[r][j]) return 1; f1++; } if(G[i][j]>0&&G[i][r]<k) { if(mp[j][r]) return 1; f2++; } if(f1)mp[j][r]=1; if(f2)mp[r][j]=1; } } } return 0; } int main() { int n,m,k,i,j; while(scanf("%d%d%d",&n,&m,&k)!=-1) { int r=0,c=0; for(i=1;i<=n;i++) { scanf("%d",&row[i]); r+=row[i]; } for(j=1;j<=m;j++) { scanf("%d",&col[j]); c+=col[j]; } if(r!=c) { printf("Impossible\n"); continue; } int flag=0; for(i=1;i<=n;i++) if(m*k<row[i]) flag++; for(i=1;i<=m;i++) if(n*k<col[i]) flag++; if(flag) { printf("Impossible\n"); continue; } init(); for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { add(i,n+j,k); } } for(i=1;i<=n;i++) add(0,i,row[i]); for(j=1;j<=m;j++) add(j+n,m+n+1,col[j]); int ans=Dinic(0,m+n+1); if(ans<r) { printf("Impossible\n"); continue; } if(judge(n,m,k)) { printf("Not Unique\n"); continue; } printf("Unique\n"); t=0; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { if(j==1) printf("%d",edge[t^1].w); else printf(" %d",edge[t^1].w); t=t+2; } printf("\n"); } } return 0; }
相关文章推荐
- 网络最大流解方程组
- 美国是全球最大的网络攻击源 中国是最大受害国
- 中国移动建成全球最大软交换网络 适合向3G过渡
- 全球最大CDN服务商:中国网络攻击量世界第一
- 秦火火——揭秘中国最大的网络黑社会
- 福布斯:雅虎最大竞争对手是整个网络
- 没日一算法 (网络最大流)3.12
- 如何修改计算机系统的网络最大连接数
- 网络最大流和最小费用流
- 最大网络流
- Google霸气掩盖全球最大网络“安全隐患”
- 如何修改计算机系统的网络最大连接数?
- Kraken:最大,最坏的僵尸网络
- 微软公布IE8更多资料 最大程度支持网络标准
- 高性能网络编程,第 1 部分: 最大程度地利用您的网络资源
- 〖强暴贴〗号称全国最大开发者网络的CSDN多次出现低级错误, 验证码形同虚设, 开发人员竟是MVP!
- 最大可上传文件限制为120MB,总量为1GB的网络U盘
- 8月第1周安全回顾 0Day漏洞成企业最大威胁 应重视网络监听
- 2008年第33届ACM/ICPC亚洲区预赛(哈尔滨)网络预选赛 1007__The Accomodation of Students 二分图最大匹配+染色
- Windows让我们养成了什么臭毛病 本篇文章来源于 黑基网-中国最大的网络安全站点 原文链接:http://www.hackbase.com/news/2010-05-10/35154.html