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 ...
随机推荐
- [leetcode.com]算法题目 - Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- Spring Cloud实践之集中配置Spring-config
将一个系统中各个应用的配置文件集中起来,方便管理. import org.springframework.boot.SpringApplication; import org.springframew ...
- 【算法python实现】 -- 最大子序和
原题:https://leetcode-cn.com/problems/maximum-subarray/ 问题描述: 输入:[-2, 1, -3, 4, -1, 2, 1, -5, 4], 输出:6 ...
- 自定义SpringBoot控制台输出的图案
pringboot启动的时候,控制台输出的图案叫banner banner?啥玩意儿?相信有些人,一定是一脸懵逼... ——这个就不陌生了吧,这个是我们启动springboot的时候,控制台输出的.. ...
- Python中super()的用法
参考链接:https://www.cnblogs.com/shengulong/p/7892266.html super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是 ...
- 全屏使用swiper.js过程中遇到的坑
概述 swiper.js确实是一个很好用的插件,下面记录下我在全屏使用过程中遇到的一些坑和解决办法,供以后开发时参考,相信对其他人也有用. 通用方案 一般来说,swiper需要放在body的下一层,虽 ...
- underscore.js源码研究(1)
概述 很早就想研究underscore源码了,虽然underscore.js这个库有些过时了,但是我还是想学习一下库的架构,函数式编程以及常用方法的编写这些方面的内容,又恰好没什么其它要研究的了,所以 ...
- vue项目警告There are multiple modules with names that only differ in casing
执行npm run dev后出现了警告提示: warning in ./src/components/Public/yearSelectCell.vue There are multiple modu ...
- (转)python-user-agents
原文:http://blog.topspeedsnail.com/archives/1958 Python3网络爬虫(四):使用User Agent和代理IP隐藏身份-------https://bl ...
- Choose GitLab for your next open source project
原文:https://b.agilob.net/choose-gitlab-for-your-next-project/ GitLab.com is a competitor of GIthub. I ...