A simple Gaussian elimination problem.(hdu4975)网络流+最大流
A simple Gaussian elimination problem.
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 728 Accepted Submission(s):
241
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?
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.
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!".
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <numeric>
using namespace std;
typedef long long LL;
const int MAXN = ;
const int MAXV = MAXN << ;
const int MAXE = * MAXN * MAXN;
const int INF = 0x3f3f3f3f;
struct ISAP
{
int head[MAXV], cur[MAXV], gap[MAXV], dis[MAXV], pre[MAXV];
int to[MAXE], next[MAXE], flow[MAXE];
int n, ecnt, st, ed;
void init(int n)
{
this->n = n;
memset(head + , -, n * sizeof(int));
ecnt = ;
}
void add_edge(int u, int v, int c)
{
to[ecnt] = v;
flow[ecnt] = c;
next[ecnt] = head[u];
head[u] = ecnt++;
to[ecnt] = u;
flow[ecnt] = ;
next[ecnt] = head[v];
head[v] = ecnt++; }
void bfs()
{
memset(dis + , 0x3f, n * sizeof(int));
queue<int> que;
que.push(ed);
dis[ed] = ;
while(!que.empty())
{
int u = que.front();
que.pop();
gap[dis[u]]++;
for(int p = head[u]; ~p; p = next[p])
{
int v = to[p];
if(flow[p ^ ] && dis[u] + < dis[v])
{
dis[v] = dis[u] + ;
que.push(v);
}
}
}
} int max_flow(int ss, int tt)
{
st = ss, ed = tt;
int ans = , minFlow = INF;
for(int i = ; i <= n; ++i)
{
cur[i] = head[i];
gap[i] = ; }
bfs();
int u = pre[st] = st;
while(dis[st] < n)
{
bool flag = false;
for(int &p = cur[u]; ~p; p = next[p])
{
int v = to[p];
if(flow[p] && dis[u] == dis[v] + )
{
flag = true;
minFlow = min(minFlow, flow[p]);
pre[v] = u;
u = v;
if(u == ed)
{
ans += minFlow;
while(u != st)
{
u = pre[u];
flow[cur[u]] -= minFlow;
flow[cur[u] ^ ] += minFlow; }
minFlow = INF; }
break; } }
if(flag) continue;
int minDis = n - ;
for(int p = head[u]; ~p; p = next[p])
{
int &v = to[p];
if(flow[p] && dis[v] < minDis)
{
minDis = dis[v];
cur[u] = p; }
}
if(--gap[dis[u]] == ) break;
++gap[dis[u] = minDis + ];
u = pre[u]; }
return ans; } int stk[MAXV], top;
bool sccno[MAXV], vis[MAXV];
bool dfs(int u, int f, bool flag)
{
vis[u] = true;
stk[top++] = u;
for(int p = head[u]; ~p; p = next[p]) if(flow[p])
{
int v = to[p];
if(v == f) continue;
if(!vis[v])
{
if(dfs(v, u, flow[p ^ ])) return true; }
else if(!sccno[v]) return true; }
if(!flag)
{
while(true)
{
int x = stk[--top];
sccno[x] = true;
if(x == u) break; } }
return false; }
bool acycle()
{
memset(sccno + , , n * sizeof(bool));
memset(vis + , , n * sizeof(bool));
top = ;
return dfs(ed, , ); }
} G;
int row[MAXN], col[MAXN];
int mat[MAXN][MAXN];
int n, m, k, ss, tt;
void solve()
{
int sumr = accumulate(row + , row + n + , );
int sumc = accumulate(col + , col + m + , );
if(sumr != sumc)
{
puts("So naive!");
return ; }
int res = G.max_flow(ss, tt);
if(res != sumc)
{
puts("So naive!");
return ; }
if(G.acycle())
{
puts("So young!"); }
else
{
puts("So simple!");
} }
int main()
{
int T,Case;
scanf("%d",&T); for(Case=;Case<=T;Case++)
{
scanf("%d%d",&n,&m);
k=;
for(int i = ; i <= n; ++i) scanf("%d", &row[i]);
for(int i = ; i <= m; ++i) scanf("%d", &col[i]);
ss = n + m + , tt = n + m + ;
printf("Case #%d: ",Case);
G.init(tt);
for(int i = ; i <= n; ++i) G.add_edge(ss, i, row[i]);
for(int i = ; i <= m; ++i) G.add_edge(n + i, tt, col[i]);
for(int i = ; i <= n; ++i)
{
for(int j = ; j <= m; ++j)
{
mat[i][j] = G.ecnt ^ ;
G.add_edge(i, n + j, k);
}
}
solve(); }
}
A simple Gaussian elimination problem.(hdu4975)网络流+最大流的更多相关文章
- hdu 4975 A simple Gaussian elimination problem.(网络流,推断矩阵是否存在)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4975 Problem Description Dragon is studying math. One ...
- hdu4975 A simple Gaussian elimination problem.(正确解法 最大流+删边判环)(Updated 2014-10-16)
这题标程是错的,网上很多题解也是错的. http://acm.hdu.edu.cn/showproblem.php?pid=4975 2014 Multi-University Training Co ...
- HDOJ 4975 A simple Gaussian elimination problem.
和HDOJ4888是一样的问题,最大流推断多解 1.把ISAP卡的根本出不来结果,仅仅能把全为0或者全为满流的给特判掉...... 2.在残量网络中找大于2的圈要用一种类似tarjian的方法从汇点開 ...
- HDU 4975 A simple Gaussian elimination problem.
A simple Gaussian elimination problem. Time Limit: 1000ms Memory Limit: 65536KB This problem will be ...
- hdu4975 A simple Gaussian elimination problem.(最大流+判环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4975 题意:和hdu4888基本一样( http://www.cnblogs.com/a-clown/ ...
- A simple Gaussian elimination problem.
hdu4975:http://acm.hdu.edu.cn/showproblem.php?pid=4975 题意:给你一个n*m的矩阵,矩阵中的元素都是0--9,现在给你这个矩阵的每一行和每一列的和 ...
- hdu - 4975 - A simple Gaussian elimination problem.(最大流量)
意甲冠军:要在N好M行和列以及列的数字矩阵和,每个元件的尺寸不超过9,询问是否有这样的矩阵,是独一无二的N(1 ≤ N ≤ 500) , M(1 ≤ M ≤ 500). 主题链接:http://acm ...
- hdu 4975 A simple Gaussian elimination problem 最大流+找环
原题链接 http://acm.hdu.edu.cn/showproblem.php?pid=4975 这是一道很裸的最大流,将每个点(i,j)看作是从Ri向Cj的一条容量为9的边,从源点除法连接每个 ...
- hdoj 3549 Flow Problem【网络流最大流入门】
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tota ...
随机推荐
- Ef-Code-First 使用实体类映射出数据库
最近面试时很多面试官都问到了EF框架 好记性不如烂笔头 赶紧记下来 code-first是EF框架中的一种,是使用实体类来进行数据库表的映射,所以实体类中的字段要规范(我认为) 比如: 如果有外键的话 ...
- Android------TabLayout的使用
https://www.jianshu.com/p/2b2bb6be83a8 主要放在 -------> Design库中的TabLayout的使用. margin和padding的区别 外边距 ...
- 通过Metasploit生成各种后门
生成windows后门 1.首先生成后门 [root@localhost ~]# msfvenom -p windows/meterpreter/reverse_tcp -e x86/shikata_ ...
- Git-工作区和暂存区的概念
工作区(Working Directory):就是在电脑里能看到的目录,如testcase文件夹就是一个工作区. 版本库(Repository):工作区有一个隐藏目录.git,是Git的版本库. ...
- 网易免费企业邮箱Foxmail设置方法
网易免费企业邮箱Foxmail7.0设置方法 第一步:启动 Foxmail 邮件客户端,点击工具->账号管理,弹出如下页面. 点击新建,如下: 填写自己企业邮箱账号,然后下一步,邮箱类型选择PO ...
- com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
出现上述bug的原因如下: 在默认设置下,Eureka服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为. 禁止方式如下:在application.propertie ...
- 安卓APP简单后端的搭建
写在前面: 此教程没有用到后端框架.只是单纯用servlet做一个例子,如果是学框架可以不用往下看了 本文适合哪些人:懂java的,会写android单机程序,懂得用HTTPClient等发送请求解析 ...
- 在matlab中实现线性回归和logistic回归
本文主要讲解在matlab中实现Linear Regression和Logistic Regression的代码,并不涉及公式推导.具体的计算公式和推导,相关的机器学习文章和视频一大堆,推荐看Andr ...
- 全网最详细的Sublime Text 3的安装Package Control插件管理包(图文详解)
不多说,直接上干货! 全网最详细的Windows里下载与安装Sublime Text *(图文详解) 全网最详细的Sublime Text 3的激活(图文详解) 全网最详细的Sublime Text ...
- 使用Java客户端对Redis进行操作
一.背景 上篇文章我们介绍了如何在centos7下面进行安装单机版redis以及redis集群.这篇文章,我们来聊一聊如何使用java客户端来进行操作redis.我们知道redis的java客户端有很 ...