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 ...
随机推荐
- 【友情链接】各位dalao的博客
同省神犇 HA队长 __stdcall HA chty_syq为文文讲过字符串 HA cdcq为文文讲过后缀数组① ② Bluesky007超强的 外省神犇 知名OIer黄学长 一个可爱的蓝孩子qwq ...
- Python 关于 encode与decode 中文乱码问题
字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(en ...
- 《Python黑帽子:黑客与渗透测试编程之道》 玩转浏览器
基于浏览器的中间人攻击: #coding=utf-8 import win32com.client import time import urlparse import urllib data_rec ...
- Flash 0day漏洞(CVE-2018-4878)复现
该漏洞影响 Flash Player 当前最新版本28.0.0.137以及之前的所有版本,而Adobe公司计划在当地时间2月5日紧急发布更新来修复此漏洞. 本文作者:i春秋作家——F0rmat 前言 ...
- js中的stopImmediatePropagation方法和stopPropagation方法的区别
看到e.stopImmediatePropagation()这个方法时,记忆有点模糊了.特地回顾一下. 基本概念 stopImmediatePropagation方法:该方法作用在当前节点及事件链的所 ...
- (原创)Callable、FutureTask中阻塞超时返回的坑点
直接上代码 import java.util.concurrent.Callable; public class MyCallable implements Callable<String> ...
- 关于a标签的onclick与href的执行顺序
onclick的事件被先执行,其次是href中定义的(页面跳转或者javascript), 同时存在两个定义的时候(onclick与href都定义了),如果想阻止href的动作,在onclick必须加 ...
- Dell R730服务器 Raid5配置
Dell R730服务器,有7块5t硬盘,默认做的RAID5.我们的目的是取其中6块硬盘做RAID5,留一块硬盘做热备. 一块SSD系统盘. 在这里,我具体解释一下 ①6块硬盘做成RAID5 ②6块硬 ...
- 解决当FORM的ENCTYPE="multipart/form-data" 时request.getParameter()获取不到值的方法
部分转载于: http://blog.csdn.net/georgejin/article/details/1706647 http://www.cnblogs.com/loveyunk/p/6089 ...
- Kafka消息存储原理
kafka消息存储机制 (一)关键术语 复习一下几个基本概念,详见上面的基础知识文章. Broker:消息中间件处理结点,一个Kafka节点就是一个broker,多个broker能够组成一个Kafka ...