hdoj--3046--Pleasant sheep and big big wolf(最小割经典)
2016-05-09 20:57
459 查看
Total Submission(s): 2699 Accepted Submission(s): 1114
[align=left]Problem Description[/align]
In ZJNU, there is a well-known prairie. And it attracts pleasant sheep and his companions to have a holiday. Big big wolf and his families know about this, and quietly hid in the big lawn. As ZJNU ACM/ICPC team, we have an
obligation to protect pleasant sheep and his companions to free from being disturbed by big big wolf. We decided to build a number of unit fence whose length is 1. Any wolf and sheep can not cross the fence. Of course, one grid can only contain an animal.
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.
[align=left]Input[/align]
There are many cases.
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.
[align=left]Output[/align]
For every case:
First line output “Case p:”, p is the p-th case;
The second line is the answer.
[align=left]Sample Input[/align]
4 6
1 0 0 1 0 0
0 1 1 0 0 0
2 0 0 0 0 0
0 2 0 1 1 0
[align=left]Sample Output[/align]
Case 1:
4
题意:n*m的图中1表示羊,2表示狼,要吧狼还有羊隔离,需要建篱笆,每一段篱笆需要1的花费,问需要的最小花费是多少
没学最小割之前真不懂啊,设立超级源点还有超级汇点,超级源点连所有的羊,超级汇点连所有的狼,边权均为INF表示该点不能删,狼还有羊可以向四周建边,边权为1,也是花费为1,0可以当做是中间节点,通过0浪可以到达羊,题目就变成要阻断所有的狼需要的花费
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define MAXN 40000+10
#define MAXM 1000000+10
#define INF 0x3f3f3f3f
using namespace std;
struct Edge
{
int from, to, cap, flow, next;
};
Edge edge[MAXM];
int head[MAXN], edgenum;
int dist[MAXN];
int cur[MAXN];
bool vis[MAXN];
int N, M;
int source, sink;
void init()
{
edgenum = 0;
memset(head, -1, sizeof(head));
}
int point(int x, int y)
{
return (x-1) * M + y;
}
void addEdge(int u, int v, int w)
{
Edge E1 = {u, v, w, 0, head[u]};
edge[edgenum] = E1;
head[u] = edgenum++;
Edge E2 = {v, u, 0, 0, head[v]};
edge[edgenum] = E2;
head[v] = edgenum++;
}
bool judge(int x, int y)
{
return x >= 1 && x <= N && y >= 1 && y <= M;
}
void getMap()
{
int a;
source = 0, sink = N * M + 1;
int move[4][2] = {0,1, 0,-1, 1,0, -1,0};
for(int i = 1; i <= N; i++)
{
for(int j = 1; j <= M; j++)
{
scanf("%d", &a);
for(int p = 0; p < 4; p++)
{
int x = i + move[p][0];
int y = j + move[p][1];
if(judge(x, y))
addEdge(point(i, j), point(x, y), 1);
}
if(a == 1)//?
addEdge(source, point(i, j), INF);//? ????
else if(a == 2)//?
addEdge(point(i, j), sink, INF);//?????
}
}
}
bool BFS(int s, int t)
{
queue<int> Q;
memset(dist, -1, sizeof(dist));
memset(vis, false, sizeof(vis));
dist[s] = 0;
vis[s] = true;
Q.push(s);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
for(int i = head[u]; i != -1; i = edge[i].next)
{
Edge E = edge[i];
if(!vis[E.to] && E.cap > E.flow)
{
dist[E.to] = dist[u] + 1;
if(E.to == t) return true;
vis[E.to] = true;
Q.push(E.to);
}
}
}
return false;
}
int DFS(int x, int a, int t)
{
if(x == t || a == 0) return a;
int flow = 0, f;
for(int &i = cur[x]; i != -1; i = edge[i].next)
{
Edge &E = edge[i];
if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(a, E.cap-E.flow), t)) > 0)
{
edge[i].flow += f;
edge[i^1].flow -= f;
flow += f;
a -= f;
if(a == 0) break;
}
}
return flow;
}
int Maxflow(int s, int t)
{
int flow = 0;
while(BFS(s, t))
{
memcpy(cur, head, sizeof(head));
flow += DFS(s, INF, t);
}
return flow;
}
int main()
{
int k = 1;
while(scanf("%d%d", &N, &M) != EOF)
{
init();
getMap();
printf("Case %d:\n%d\n", k++, Maxflow(source, sink));
}
return 0;
}
Pleasant sheep and big big wolf
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2699 Accepted Submission(s): 1114
[align=left]Problem Description[/align]
In ZJNU, there is a well-known prairie. And it attracts pleasant sheep and his companions to have a holiday. Big big wolf and his families know about this, and quietly hid in the big lawn. As ZJNU ACM/ICPC team, we have an
obligation to protect pleasant sheep and his companions to free from being disturbed by big big wolf. We decided to build a number of unit fence whose length is 1. Any wolf and sheep can not cross the fence. Of course, one grid can only contain an animal.
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.
[align=left]Input[/align]
There are many cases.
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.
[align=left]Output[/align]
For every case:
First line output “Case p:”, p is the p-th case;
The second line is the answer.
[align=left]Sample Input[/align]
4 6
1 0 0 1 0 0
0 1 1 0 0 0
2 0 0 0 0 0
0 2 0 1 1 0
[align=left]Sample Output[/align]
Case 1:
4
题意:n*m的图中1表示羊,2表示狼,要吧狼还有羊隔离,需要建篱笆,每一段篱笆需要1的花费,问需要的最小花费是多少
没学最小割之前真不懂啊,设立超级源点还有超级汇点,超级源点连所有的羊,超级汇点连所有的狼,边权均为INF表示该点不能删,狼还有羊可以向四周建边,边权为1,也是花费为1,0可以当做是中间节点,通过0浪可以到达羊,题目就变成要阻断所有的狼需要的花费
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define MAXN 40000+10
#define MAXM 1000000+10
#define INF 0x3f3f3f3f
using namespace std;
struct Edge
{
int from, to, cap, flow, next;
};
Edge edge[MAXM];
int head[MAXN], edgenum;
int dist[MAXN];
int cur[MAXN];
bool vis[MAXN];
int N, M;
int source, sink;
void init()
{
edgenum = 0;
memset(head, -1, sizeof(head));
}
int point(int x, int y)
{
return (x-1) * M + y;
}
void addEdge(int u, int v, int w)
{
Edge E1 = {u, v, w, 0, head[u]};
edge[edgenum] = E1;
head[u] = edgenum++;
Edge E2 = {v, u, 0, 0, head[v]};
edge[edgenum] = E2;
head[v] = edgenum++;
}
bool judge(int x, int y)
{
return x >= 1 && x <= N && y >= 1 && y <= M;
}
void getMap()
{
int a;
source = 0, sink = N * M + 1;
int move[4][2] = {0,1, 0,-1, 1,0, -1,0};
for(int i = 1; i <= N; i++)
{
for(int j = 1; j <= M; j++)
{
scanf("%d", &a);
for(int p = 0; p < 4; p++)
{
int x = i + move[p][0];
int y = j + move[p][1];
if(judge(x, y))
addEdge(point(i, j), point(x, y), 1);
}
if(a == 1)//?
addEdge(source, point(i, j), INF);//? ????
else if(a == 2)//?
addEdge(point(i, j), sink, INF);//?????
}
}
}
bool BFS(int s, int t)
{
queue<int> Q;
memset(dist, -1, sizeof(dist));
memset(vis, false, sizeof(vis));
dist[s] = 0;
vis[s] = true;
Q.push(s);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
for(int i = head[u]; i != -1; i = edge[i].next)
{
Edge E = edge[i];
if(!vis[E.to] && E.cap > E.flow)
{
dist[E.to] = dist[u] + 1;
if(E.to == t) return true;
vis[E.to] = true;
Q.push(E.to);
}
}
}
return false;
}
int DFS(int x, int a, int t)
{
if(x == t || a == 0) return a;
int flow = 0, f;
for(int &i = cur[x]; i != -1; i = edge[i].next)
{
Edge &E = edge[i];
if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(a, E.cap-E.flow), t)) > 0)
{
edge[i].flow += f;
edge[i^1].flow -= f;
flow += f;
a -= f;
if(a == 0) break;
}
}
return flow;
}
int Maxflow(int s, int t)
{
int flow = 0;
while(BFS(s, t))
{
memcpy(cur, head, sizeof(head));
flow += DFS(s, INF, t);
}
return flow;
}
int main()
{
int k = 1;
while(scanf("%d%d", &N, &M) != EOF)
{
init();
getMap();
printf("Case %d:\n%d\n", k++, Maxflow(source, sink));
}
return 0;
}
相关文章推荐
- css中的居中问题
- 老鼠的求爱之旅 (DP)
- Demo3 悬浮的按钮:FloatActionButton(Design Support Lib)
- HTML基础知识三(超链接、表单)
- SpringIOC容器-对象依赖
- 给自己一个机会
- cPanel下如何添加新的域名
- linux命令行下导出导入.sql文件
- 【2015百度之星】 IP聚合
- 【杂】搜集到的有用的知识点
- HTML基础知识二(页面属性、注释、表格、设置表框颜色)
- Fragment 初学理解
- POJ 2441 Arrange the Bulls(状态压缩DP)
- 自定义拦截器
- 第10、11周-项目3(2)
- 笔记8:Ping类
- HTML基础知识一(HTML、常见方式、文档结构、<meta>、 网站文件命名、文本修饰、文本样式、段落标记、居中标记、水平线标记、特殊字符、列表、图像)
- 创建第一个Hiberntae工程
- XML和Map集合之间的相互转换
- 数据库抽象层PDO