HDU 4975 A simple Gaussian elimination problem.
A simple Gaussian elimination problem.
This problem will be judged on HDU. Original ID: 4975
64-bit integer IO format: %I64d Java class name: Main
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?
Input
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.
Output
Sample Input
3
1 1
5
5
2 2
0 10
0 10
2 2
2 2
2 2
Sample Output
Case #1: So simple!
Case #2: So naive!
Case #3: So young!
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
struct arc{
int to,flow,next;
arc(int x = ,int y = ,int z = -){
to = x;
flow = y;
next = z;
}
}e[];
int head[maxn],d[maxn],cur[maxn],tot,S,T;
bool vis[maxn],hv[maxn];
void add(int u,int v,int flow){
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,,head[v]);
head[v] = tot++;
}
bool bfs(){
queue<int>q;
memset(d,-,sizeof d);
d[S] = ;
q.push(S);
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].flow && d[e[i].to] == -){
d[e[i].to] = d[u] + ;
q.push(e[i].to);
}
}
}
return d[T] > -;
}
int dfs(int u,int low){
if(u == T) return low;
int tmp = ,a;
for(int &i = cur[u]; ~i; i = e[i].next){
if(e[i].flow && d[e[i].to] == d[u]+&&(a=dfs(e[i].to,min(low,e[i].flow)))){
e[i].flow -= a;
e[i^].flow += a;
low -= a;
tmp += a;
if(!low) break;
}
}
if(!tmp) d[u] = -;
return tmp;
}
int dinic(){
int ret = ;
while(bfs()){
memcpy(cur,head,sizeof head);
ret += dfs(S,INF);
}
return ret;
}
bool dfs2(int u,int fa){
if(vis[u]) return true;
vis[u] = true;
for(int i = head[u]; ~i; i = e[i].next)
if(!hv[e[i].to] && e[i].flow && e[i].to != fa && dfs2(e[i].to,u)) return true;
hv[u] = true;
return vis[u] = false;
}
int main(){
int Ts,n,m,tmp,sum,sum2,cs = ;
scanf("%d",&Ts);
while(Ts--){
scanf("%d %d",&n,&m);
memset(head,-,sizeof head);
memset(hv,false,sizeof hv);
sum2 = sum = S = tot = ;
T = n + m + ;
for(int i = ; i <= n; ++i){
scanf("%d",&tmp);
add(S,i,tmp);
sum += tmp;
for(int j = ; j <= m; ++j)
add(i,j+n,);
}
for(int i = ; i <= m; ++i){
scanf("%d",&tmp);
add(i+n,T,tmp);
sum2 += tmp;
}
if(sum == sum2){
if(sum == dinic()){
bool flag = false;
memset(vis,false,sizeof vis);
for(int i = ; i <= n; ++i)
if(flag = dfs2(i,-)) break;
if(flag) printf("Case #%d: So young!\n",cs++);
else printf("Case #%d: So simple!\n",cs++);
}else printf("Case #%d: So naive!\n",cs++);
}else printf("Case #%d: So naive!\n",cs++);
}
return ;
}
HDU 4975 A simple Gaussian elimination problem.的更多相关文章
- hdu 4975 A simple Gaussian elimination problem.(网络流,推断矩阵是否存在)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4975 Problem Description Dragon is studying math. One ...
- 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 4975 A simple Gaussian elimination problem.
和HDOJ4888是一样的问题,最大流推断多解 1.把ISAP卡的根本出不来结果,仅仅能把全为0或者全为满流的给特判掉...... 2.在残量网络中找大于2的圈要用一种类似tarjian的方法从汇点開 ...
- hdu4975 A simple Gaussian elimination problem.(正确解法 最大流+删边判环)(Updated 2014-10-16)
这题标程是错的,网上很多题解也是错的. http://acm.hdu.edu.cn/showproblem.php?pid=4975 2014 Multi-University Training Co ...
- A simple Gaussian elimination problem.(hdu4975)网络流+最大流
A simple Gaussian elimination problem. Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65 ...
- A simple Gaussian elimination problem.
hdu4975:http://acm.hdu.edu.cn/showproblem.php?pid=4975 题意:给你一个n*m的矩阵,矩阵中的元素都是0--9,现在给你这个矩阵的每一行和每一列的和 ...
- hdu4975 A simple Gaussian elimination problem.(最大流+判环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4975 题意:和hdu4888基本一样( http://www.cnblogs.com/a-clown/ ...
- hdu 4972 A simple dynamic programming problem(高效)
pid=4972" target="_blank" style="">题目链接:hdu 4972 A simple dynamic progra ...
随机推荐
- 二维码框架ZBarSDK的使用和自己定义二维码扫描界面方法
假设你不知道ZBarSDK怎么用,请下载demo http://download.csdn.net/detail/u013686641/7858917 假设你已经配置好ZBarSDK .那么以下这个类 ...
- Domino 使用递归算法获取视图值
在关系数据库中,有两字段是父子关系.通过递归算法,输入一个父ID,能够获取全部相应的子ID.这种数据结构在组织架构中常常使用.显示一般使用树形结构.在Domino中相同能够处理这种情况,下面是个小de ...
- less07 important
less .foo (@bg: #f5f5f5, @color: #900) { background: @bg; color: @color; font-size: 16px; font-weigh ...
- Windows10+VS2013+caffe+Python2.7+CUDA8.0 部署配置
所需环境工具: 1. Windows 10 2. VS2013 3. Windows版本的caffe工具包,地址:https://github.com/Microsoft/caffe 4. Anaco ...
- java9新特性-21-java的动态编译器
1. 官方Feature 243: Java-Level JVM Compiler Interface 295: Ahead-of-Time Compilation 2. 产生背景 Oracle 一直 ...
- java9新特性-16-Deprecated的相关API
1.官方Feature 211: Elide Deprecation Warnings on Import Statements 214: Remove GC Combinations Depreca ...
- springMVC怎么接收日期类型的参数?
springMVC怎么接收日期类型的参数? springMVC的controller中用实体接受页面传递的参数,并且实体中的属性类型为日期类型,怎么接收呢?如果接收不到会进不到controller中. ...
- App开发Native.js入门指南
概述 Native.js技术,简称NJS,是一种将手机操作系统的原生对象转义,映射为JS对象,在JS里编写原生代码的技术.如果说Node.js把js扩展到服务器世界,那么Native.js则把js扩展 ...
- HDU-2303 The Embarrassed Cryptographer 高精度算法(大数取模)
题目链接:https://cn.vjudge.net/problem/HDU-2303 题意 给一个大数K,和一个整数L,其中K是两个素数的乘积 问K的是否存在小于L的素数因子 思路 枚举素数,大数取 ...
- php后端控制可跨域的域名,允许图片跨域上传
跨域问题经常需要面对,前端需要做的比较直接要么选择ajax异步提交,XML或者jsonp,要么表单提交前端常见跨域解决方案 jsonp基本可以搞定大部分跨域问题,但问题也比较明显,只能通过get方式提 ...