BZOJ 1001 狼抓兔子 (最小割转化成最短路)
1001: [BeiJing2006]狼抓兔子
Time Limit: 15 Sec Memory Limit: 162 MB
Submit: 27715 Solved: 7134
[Submit][Status][Discuss]
Description

Input
Output
输出一个整数,表示参与伏击的狼的最小数量.
Sample Input
5 6 4
4 3 1
7 5 3
5 6 7 8
8 7 6 5
5 5 5
6 6 6
Sample Output
HINT
2015.4.16新加数据一组,可能会卡掉从前可以过的程序。
Source
析:很容易看出是裸板的最小割,然后可能会超时,边实在是太多了,有一种特殊的方法,可以把平面图转成最短路来求,也就是利用对偶图,把原图的而看成新图的点,原图的边与两个面相连的,加一条边,然后再多加一个起点和终点。跑一次最短路即可。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
//#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 2e6 + 10;
const int maxm = 100 + 10;
const ULL mod = 10007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
}
struct Edge{
int to, dist, next;
}; struct HeapNode{
int d, u;
bool operator < (const HeapNode &p) const{
return d > p.d;
}
};
Edge edge[maxn*3];
int head[maxn], cnt;
int d[maxn];
bool done[maxn]; inline void addEdge(int u, int v, int dist){
edge[cnt].to = v;
edge[cnt].dist = dist;
edge[cnt].next = head[u];
head[u] = cnt++;
} void dijkstra(int s){
priority_queue<HeapNode> pq;
ms(d, INF); d[s] = 0;
ms(done, 0);
pq.push((HeapNode){0, s});
while(!pq.empty()){
HeapNode x = pq.top(); pq.pop();
int u = x.u;
if(done[u]) continue;
done[u] = 1;
for(int i = head[u]; ~i; i = edge[i].next){
int v = edge[i].to, dist = edge[i].dist;
if(d[v] > d[u] + dist){
d[v] = d[u] + dist;
pq.push((HeapNode){d[v], v});
}
}
}
} int main(){
scanf("%d %d", &n, &m);
if(n == 1 || m == 1){
n = max(n, m);
int ans = INF;
for(int i = 0; i < n; ++i){
int x;
scanf("%d", &x);
ans = min(ans, x);
}
printf("%d\n", ans);
return 0;
}
ms(head, -1); cnt = 0;
int s = 0, t = 2 * n * m + 1;
FOR(i, 0, n) for(int j = 1; j < (m-1<<1); j += 2){
int dist;
scanf("%d", &dist);
int from = i == 0 ? s : (i-1)*(m-1<<1)+j+1;
int to = i + 1 == n ? t : i*(m-1<<1)+j;
addEdge(from, to, dist);
addEdge(to, from, dist);
}
FOR(i, 0, n-1) for(int j = 1; j <= m; ++j){
int dist;
scanf("%d", &dist);
int from = j == 1 ? t : i*(m-1<<1)+(j<<1)-3;
int to = j == m ? s : i*(m-1<<1)+(j<<1);
addEdge(from, to, dist);
addEdge(to, from, dist);
}
FOR(i, 0, n-1) for(int j = 1; j < m; ++j){
int dist;
scanf("%d", &dist);
int from = i*(m-1<<1)+(j<<1);
int to = from - 1;
addEdge(from, to, dist);
addEdge(to, from, dist);
}
dijkstra(s);
printf("%d\n", d[t]);
return 0;
}
BZOJ 1001 狼抓兔子 (最小割转化成最短路)的更多相关文章
- BZOJ 1001 - 狼抓兔子 - [Dinic最大流][对偶图最短路]
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1001 Description现在小朋友们最喜欢的"喜羊羊与灰太狼", ...
- [BJOI2006]狼抓兔子——最小割转对偶图最短路
其实这个题直接Dinic跑最小割可过. (小优化是: 无向图建网络流,一条边不用建成4条,可以正反容量都是边权即可.完全等价 ) [无效]网络流之转换对偶图 一个巧妙的事情是,如果建边合适的话,最小割 ...
- BZOJ1001: [BeiJing2006]狼抓兔子 [最小割 | 对偶图+spfa]
1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 19528 Solved: 4818[Submit][ ...
- bzoj1001: [BeiJing2006]狼抓兔子 -- 最小割
1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec Memory Limit: 162 MB Description 现在小朋友们最喜欢的"喜羊羊与灰太狼 ...
- BZOJ 1001: [BeiJing2006]狼抓兔子 最小割
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1001 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓 ...
- BZOJ 1001 狼抓兔子 (网络流最小割/平面图的对偶图的最短路)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1001 算法讨论: 1.可以用最大流做,最大流等于最小割. 2.可以把这个图转化其对偶图,然 ...
- [bzoj 1001][Beijing2006]狼抓兔子 (最小割+对偶图+最短路)
Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的, 而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一 ...
- bzoj 1001 狼抓兔子 —— 平面图最小割(最短路)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1001 平面图最小割可以转化成最短路问题: 建图时看清楚题目的 input ... 代码如下: ...
- BZOJ 1001 狼抓兔子 平面图的最小割
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1001 题目大意: 见链接 思路: 求最小割,平面图的最小割等价于对偶图的最短路 直接建 ...
随机推荐
- 【转】让开发变得简单一点- Visual Studio 2010几个让人印象深刻的新功能
原文网址:http://xhinker.blog.51cto.com/640011/313055/ 引言 "我们的目标,不仅仅是做出几个新功能,而是要回答一个问题:'如何让现在的开发人员生活 ...
- wamp安装后无法正常启动(80端口被占用)
关于wamp启动是80端口被占用的问题详解(win7系统下WAMP 80端口被Microsoft-HTTPAPI/2.0占用的解决办法) VS2010在更新了SP1后,会在开机时自动启动一个服务,占用 ...
- android studio安装须知
64位linux,默认会提示mksdcard错误什么的,需要安装一个库 sudo apt- android sdk的下载,自己找代理服务器吧,哎……
- make 写法练习
cc=g++ all:signal %:%.o $(cc) -o $< $@ %.cpp:%.o echo se $< $@ $* $^ g++ -c $< $@cl: rm -rf ...
- 学习笔记之Tips for Macbook
写给Mac新手的入门指南 - 威锋网 https://mp.weixin.qq.com/s/pqmqGZhNwevx57KeLnzZmg https://bbs.feng.com/read-htm-t ...
- migrating-vcenter-database-express-to-sql-2008-r2
migrating-vcenter-database-express-to-sql-2008-r2 一. 准备环境. ESXi5.0主机 IP:192.168.1.158 ...
- Fatal error: Call to undefined function Think\C() in /var/www/html/ceshi.hzheee.com/think/ThinkPHP/Library/Think/Think.class.php on line 334 这个问题解决
当APP_DEBUG为true时,包含图中这个文件,文件中又引导包含这些库文件,可以看出安装thinkphp3.2.3时ThinkPHP/Common/下是functions.php,把它改成func ...
- php 文件缓存类
//文件缓存类 class FileCache { private $cacheTime = 3600; //默认缓存时间 秒 private $cacheDir = './filecache'; / ...
- yum安装nagois
多少年前就装过了,今天再来用yum装一次,都忘干净了~~ 主监控机:CentOS 6.5 192.168.0.105被监控机:CentOS 6.5 192.168.0.107 主监控机设置:1.安装e ...
- Requests抓取火车票数据
1.数据接口 https://kyfw.12306.cn/otn/lcxxcx/query?purpose_codes=ADULT&queryDate=2016-08-01&from_ ...