Pleasant sheep and big big wolf HDU - 3046(最小割)
Pleasant sheep and big big wolf
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3316 Accepted Submission(s): 1360
Now, we ask to place the minimum fences to let pleasant sheep and his Companions to free from being disturbed by big big wolf and his companions.
For every case:
N and M(N,M<=200)
then N*M matrix:
0 is empty, and 1 is pleasant sheep and his companions, 2 is big big wolf and his companions.
First line output “Case p:”, p is the p-th case;
The second line is the answer.
1 0 0 1 0 0
0 1 1 0 0 0
2 0 0 0 0 0
0 2 0 1 1 0
4
解析:
求至少需要多少边使狼不能抓住羊,边嘛,肯定想到最小割,但一想最小割是删除边,那么就转化为把所有边都连上,求最小割叭
数组开小了 竟然T了一次 emm。。。
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 1e5 + , INF = 0x7fffffff;
int n, m, s, t;
int head[maxn], cur[maxn], d[maxn], vis[maxn], cnt;
int nex[maxn << ];
struct node
{
int u, v, c;
}Node[maxn << ]; void add_(int u, int v, int c)
{
Node[cnt].u = u;
Node[cnt].v = v;
Node[cnt].c = c;
nex[cnt] = head[u];
head[u] = cnt++;
} void add(int u, int v, int c)
{
add_(u, v, c);
add_(v, u, );
} bool bfs()
{
queue<int> Q;
mem(d, );
d[s] = ;
Q.push(s);
while(!Q.empty())
{
int u = Q.front(); Q.pop();
for(int i = head[u]; i != -; i = nex[i])
{
int v = Node[i].v;
if(!d[v] && Node[i].c > )
{
d[v] = d[u] + ;
Q.push(v);
if(v == t) return ;
}
}
}
return d[t] != ;
} int dfs(int u, int cap)
{
int ret = ;
if(u == t || cap == )
return cap;
for(int &i = cur[u]; i != -; i = nex[i])
{
int v = Node[i].v;
if(d[v] == d[u] + && Node[i].c > )
{
int V = dfs(v, min(cap, Node[i].c));
Node[i].c -= V;
Node[i ^ ].c += V;
ret += V;
cap -= V;
if(cap == ) break;
}
}
if(cap > ) d[u] = -;
return ret;
} int Dinic(int u)
{
int ans = ;
while(bfs())
{
memcpy(cur, head, sizeof(head));
ans += dfs(u, INF);
}
return ans;
} int main()
{
int tmp, kase = ;
while(scanf("%d%d", &n, &m) != EOF)
{
mem(head, -);
cnt = ;
s = , t = ;
rep(i, , n)
{
rap(j, , m)
{
rd(tmp);
if (tmp == )
add(i * m + j, t, INF);
else if (tmp == )
add(s, i * m + j, INF);
if(i != n - && j != m)
add(i * m + j, (i + ) * m + j, ), add(i * m + j, i * m + j + , ), add((i + ) * m + j, i * m + j, ), add(i * m + j + , i * m + j, );
else if(i != n - && j == m)
add(i * m + j, (i + ) * m + j, ), add((i + ) * m + j, i * m + j, );
else if (i == n - && j != m)
add(i * m + j, i * m + j + , ), add(i * m + j + , i * m + j, );
}
}
printf("Case %d:\n", ++kase);
pd(Dinic(s));
} return ;
}
Pleasant sheep and big big wolf HDU - 3046(最小割)的更多相关文章
- hdu 3046 最小割
每个栅栏其实就是一条边,修一些栅栏,使得狼不能抓到羊,其实就是求一个割,使得羊全在S中,狼全在T中. #include <cstdio> #include <cstring> ...
- HDU 3046Pleasant sheep and big big wolf(切最小网络流)
职务地址:HDU 3046 最小割第一发!事实上也没什么发不发的. ..最小割==最大流.. 入门题,可是第一次入手最小割连入门题都全然没思路... sad..对最小割的本质还是了解的不太清楚.. 这 ...
- HDU 3046 Pleasant sheep and big big wolf(最小割)
HDU 3046 Pleasant sheep and big big wolf 题目链接 题意:一个n * m平面上,1是羊.2是狼,问最少要多少围墙才干把狼所有围住,每有到达羊的路径 思路:有羊和 ...
- HDU 3046 Pleasant sheep and big big wolf
Pleasant sheep and big big wolf Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged ...
- hdu 3046 Pleasant sheep and big big wolf 最小割
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3046 In ZJNU, there is a well-known prairie. And it a ...
- Pleasant sheep and big big wolf
pid=3046">点击打开链接 题目:在一个N * M 的矩阵草原上,分布着羊和狼.每一个格子仅仅能存在0或1仅仅动物.如今要用栅栏将全部的狼和羊分开.问怎么放,栅栏数放的最少,求出 ...
- hdu 4289(最小割)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4289 思路:求最小花费,最小割应用,将点权转化为边权,拆点,(i,i+n)之间连边,容量为在城市i的花 ...
- hdu 1569 最小割
和HDU 1565是一道题,只是数据加强了,貌似轮廓线DP来不了了. #include <cstdio> #include <cstring> #include <que ...
- hdu 4289 最小割,分拆点为边
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2609 #include <cstdio> #incl ...
随机推荐
- .NetCore 2.1中的HttpClientFactory最佳实践
.NET Core 2.1中的HttpClientFactory最佳实践 ASP.NET Core 2.1中出现一个新的HttpClientFactory功能, 它有助于解决开发人员在使用HttpCl ...
- python 链接 redis 失败 由于目标计算机积极拒绝,无法连接
whereis redis-cli ps -ef |grep redis 1.启动redis redis-server & 2.查看redis 进程 ps -ef |grep redis 3. ...
- ORM 多表操作查询及增删改查
------------------------------------------只有对前途乐观的人,才能不怕黑暗,才能有力量去创造光明.乐观不是目的,而是人生旅途中的一种态度. 多表操作 创建模型 ...
- 面试题-如何测试一个APP
问: 假如给你一个APP,你应该如何测试,分别从哪些方面来针对该APP进行测试. --- 1.安装.卸载测试 测试软件在不同操作系统(Android.iOS)下安装是否正常.软件安装后的是否能够正常运 ...
- H5 17-兄弟选择器
17-兄弟选择器 我是标题 我是超链接 我是段落 我是段落 我是段落 我是标题 我是段落 我是段落 我是段落 --> 我是标题 我是超链接 我是段落 我是段落 我是超链接 我是段落 我是标题 我 ...
- Wannafly summer camp Day6 - D 区间权值
这道题实在是不该,我在化式子的时候,多此一举,把式子进行累加,导致自己当时化的式子是错的,这样导致自己卡了很久,也没想到好的思路,赛后重新分析一波,感觉巨™简单...难受的一逼. 这道题的关键在于,W ...
- p86商空间也是Banach空间
1.为什么要引入Zk? 2.为什么这个等式成立,和为什么要引入uk? 3.为什么为什么等于0? 属于M,则商空间是0元,p128最上面的第二个笔记
- Git远程分支的回退
下午发现上午提交的一个版本有问题,在回退本地分支后,发现还必须要回退远程分支的版本.网上查找到的资料如下: #新建old_master分支做备份 git branch old_master #push ...
- CSS3 transform-style 属性
语法 transform-style: flat | preserve-3d 语法项目 说明 初始值 flat 适用于 块元素和行内元素 可否继承 ...
- word的"bug"
发表博客发现,从word复制文本到chrome浏览器上的博客时, 如果复制完后立即关闭word,那么将无法粘贴到通过chrome浏览器访问的博客上,也无法粘贴到记事本上: 但是复制完立即关闭word后 ...