UVA1376.Animal Run (最小割转为最短路 && dijkstra)
Description

Animals are living their painful lives in the zoo. Their activities are limited in a small area without any fun of snacks, alcohol, love or games. They are so upset that they decide to escape in a night.
As shown in Figure 1, the paths in the zoo can be expressed by a grid with nxm nodes. All the paths in the grid are two-way, horizontal or vertical or diagonal. Animals start from the upper left corner, and they are free if they can reach the lower right through paths.

To protect public safety, the police are sent to block some paths to catch all the escaping animals. As it needs certain police staff to block a path, you are required to write a program for the police officer, and tell him how many staff at least shall be sent in order to defeat this Animal Escape.
Input
Input contains several cases, each describes one escape action.
Each case begins with two integers n and m, 3 n, m
1000.
For the following n lines, there are m - 1 integers in each line, indicating how many staff shall be sent to block the horizontal paths respectively.
For the following n - 1 lines, there are m integers in each line, indicating how many staff shall be sent to block the vertical paths respectively.
For the following n - 1 lines, there are m - 1 integers in each line, indicating how many staff shall be sent to block the diagonal paths respectively.
Each line describes the paths from left to right. All integers in input file are no more than 1, 000, 000.
The last case is followed by a line containing two zeros. The size of the input data is about 16MB.
Output
Sample Input
3 4
5 6 4
4 3 1
7 5 3
5 6 7 8
8 7 6 5
5 5 5
6 6 6
0 0
Sample Output
Case 1: Minimum = 14
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
typedef long long ll ;
const int M = + ;
const int maxn = ** ;
int hori[M][M] , vert[M][M] , diag[M][M] ;
int st , ed ;
const int inf = 0x3f3f3f3f ;
struct edge
{
int u , v , w ;
}; struct heapnode
{
ll d , u ;
bool operator < (const heapnode & rhs ) const
{
return d > rhs.d ;
}
}; int n , m ;
std::vector <edge> edges ;
std::vector <int> g[maxn] ;
bool done[maxn] ;
ll d[maxn] ;
int p[maxn] ; void init (int n)
{
for (int i = ; i <= n ; i ++ ) g[i].clear ();
edges.clear () ;
} void addedge (int u , int v , int w)
{
edges.push_back ( (edge) {u , v , w}) ;
int m = edges.size () ;
g[u].push_back (m - ) ;
} void dijkstra (int s)
{
std::priority_queue <heapnode> q ;
for (int i = ; i <= ed ; i ++) d[i] = inf ;
d[s] = ;
memset (done , , sizeof(done)) ;
q.push ( (heapnode) { , s}) ;
while ( !q.empty ()) {
heapnode x = q.top () ; q.pop () ;
int u = x.u ;
if (done[u]) continue ;
done[u] = true ;
for (int i = ; i < g[u].size () ; i ++) {
edge e = edges[g[u][i]] ;
// printf ("%d > %d + %d\n" , d[e.v] , d[u] , e.w ) ;
if ( d[e.v] > d[u] + e.w ) {
d[e.v] = d[u] + 1ll * e.w ;
p[e.v] = g[u][i] ;
q.push ( (heapnode) {d[e.v] , e.v }) ;
}
}
}
} void build ()
{
for (int i = ; i < n - ; i ++) {
for (int j = ; j < m - ; j ++) {
int u = i * * (m - ) + * j , v = i * * (m - ) + * j + ;
addedge (u , v , diag[i][j]) ;
addedge(v , u , diag[i][j]) ;
// printf ("m=%d\n" , m ) ;
}
}
for (int i = ; i < n - ; i ++) {
for (int j = ; j < m - ; j ++) {
int u = (i - ) * * (m - )+ * j , v = i * * (m - )+ * j + ;
addedge (u , v , hori[i][j]) ;
addedge (v , u , hori[i][j]) ;
}
}
for (int j = ; j < m - ; j ++) {
addedge ( * j + , st , hori[][j]) ;
addedge (ed , (n - ) * * (m - ) + * j , hori[n - ][j]) ;
}
for (int i = ; i < n - ; i ++) {
for (int j = ; j < m - ; j ++) {
int u = i * * (m - ) + * j - , v = i * * (m - ) + * j ;
addedge (u , v , vert[i][j]) ;
addedge (v , u , vert[i][j]) ;
}
}
for (int i = ; i < n - ; i ++) {
addedge (ed , i * * (m - ), vert[i][] ) ;
addedge ((i + ) * * (m - ) - , st , vert[i][m - ]) ;
}
// for (int i = 0 ; i < edges.size () ; i ++) printf ("%d--->%d(%d)\n" , edges[i].u , edges[i].v , edges[i].w ) ;
} int main ()
{
// freopen ("a.txt" , "r" , stdin ) ;
int cas = ;
while (~ scanf ("%d%d" , &n , &m ) ) {
if (n == && m == ) break ;
st = (n - ) * * (m - ) ; ed = (n - ) * * (m - ) + ;//源点 , 汇点
int rhs = std::max (n , m ) ;
init ( * rhs * rhs ) ;
for (int i = ; i < n; i ++) for (int j = ; j < m - ; j ++) scanf ("%d" , &hori[i][j]) ;
for (int i = ; i < n - ; i ++) for (int j = ; j < m ; j ++) scanf ("%d" , &vert[i][j]) ;
for (int i = ; i < n - ; i ++) for (int j = ; j < m - ; j ++) scanf ("%d" , &diag[i][j]) ;
build () ;
dijkstra (ed) ;
// for (int i = 0 ; i <= ed ; i ++) printf ("%2d ", i) ; puts ("") ;
// for (int i = 0 ; i <= ed ; i ++) printf ("%2d " , d[i]) ; puts ("") ;
printf ("Case %d: Minimum = %lld\n" , cas ++ , d[st]) ;
}
return ;
}
建图真心好难,一开始一直在yy怎么设点:比如说一个个命名 ,先把数据存图 , 再在图上搜相邻点间的关系。。。但结果感觉都非常奇怪。
后来想到了一个出路,就是他给的数据存到hori[][] , vert[][] , diag[][] .
然后用i , j来表示一个个三角形的下标。并分别将以上三种边相邻的三角形建立关系。
然后套套dijkstra模板过了。
UVA1376.Animal Run (最小割转为最短路 && dijkstra)的更多相关文章
- 【BZOJ1001】狼抓兔子(平面图最小割转最短路)
题意:有一张平面图,求它的最小割.N,M.表示网格的大小,N,M均小于等于1000. 左上角点为(1,1),右下角点为(N,M).有以下三种类型的道路 1:(x,y)<==>(x+1,y ...
- HDU3870 Catch the Theves(平面图最小割转最短路)
题目大概说给一个n×n的方格,边有权值,问从求(1,1)到(n,n)的最小割. 点达到了160000个,直接最大流不好.这题的图是平面图,求最小割可以转化成求其对偶图的最短路,来更高效地求解: 首先源 ...
- [bzoj 1001][Beijing2006]狼抓兔子 (最小割+对偶图+最短路)
Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的, 而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一 ...
- BZOJ1001 [BeiJing2006]狼抓兔子 最小割 对偶图 最短路
原文链接http://www.cnblogs.com/zhouzhendong/p/8686871.html 题目传送门 - BZOJ1001 题意 长成上面那样的网格图求最小割. $n,m\leq ...
- Luogu2046 NOI2010 海拔 平面图、最小割、最短路
传送门 首先一个不知道怎么证的结论:任意点的\(H\)只会是\(0\)或\(1\) 那么可以发现原题的本质就是一个最小割,左上角为\(S\),右下角为\(T\),被割开的两个部分就是\(H=0\)与\ ...
- BZOJ.2007.[NOI2010]海拔(最小割 对偶图最短路)
题目链接 想一下能猜出,最优解中海拔只有0和1,且海拔相同的点都在且只在1个连通块中. 这就是个平面图最小割.也可以转必须转对偶图最短路,不然只能T到90分了..边的方向看着定就行. 不能忽略回去的边 ...
- bzoj 2007 [Noi2010]海拔——最小割转最短路
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2007 一个点的高度一定不是0就是1.答案一定形如一个左上角的连通块全是0的点.一个右下角的连 ...
- P2046 [NOI2010]海拔 平面图转对偶图(最小割-》最短路)
$ \color{#0066ff}{ 题目描述 }$ YT市是一个规划良好的城市,城市被东西向和南北向的主干道划分为n×n个区域.简单起见,可以将YT市看作 一个正方形,每一个区域也可看作一个正方形. ...
- 【bzoj2007】[Noi2010]海拔 最小割+对偶图+最短路
题目描述 YT市是一个规划良好的城市,城市被东西向和南北向的主干道划分为n×n个区域.简单起见,可以将YT市看作一个正方形,每一个区域也可看作一个正方形.从而,YT城市中包括(n+1)×(n+1)个交 ...
随机推荐
- CentOS同步时间
用date查看系统当前时间,date -R 可查看时区. CentOS 同步时间由ntp服务提供,可以用"yum install ntp -y"安装. 装完后运行命令 ntpdat ...
- .bash_profile for mac‘ envionment variables
A typical install of OS X won't create a .bash_profile for you. When you want to run functions from ...
- 启动和关闭ADB服务(adb start-server和adb kill-server)
1 Android SDK中的常用命令行工具 在<Android SDK安装目录>\tools目录中带了很多命令行工具.虽然一般的开发人员并不需要完全掌握这些工具的使用方法,但了解这些工 ...
- Linux以外的开源操作系统大汇总
开源操作系统即公开源代码的操作系统软件,它遵循开源协议使用.编译和发布.自由和开放源代码软件中最著名的是Linux,它是一种类Unix的操作系统.Linux可安装在各种计算机硬件设备中,比如手机.平板 ...
- JavaScript中的事件对象
JavaScript中的事件对象 JavaScript中的事件对象是非常重要的,恐怕是我们在项目中使用的最多的了.在触发DOM上的某个事件时,会产生一个事件对象event,这个对象中包含这所有与事件有 ...
- layer图层常见属性
把layer常见图层属性总结了一下^-^欢迎大家讨论~~~~来吧 ,代码属性 #import "CZViewController.h" @interface CZViewContr ...
- js012-DO2和DOM3
js012-DO2和DOM3 本章内容: DOM2和DOM3的变化 操作样式的ODM API DOM 遍历与范围 DOM2级核心:在一级核心基础上构建,为节点添加了更多方法和属性 DOM2级视图:为文 ...
- awk过滤数据
awk -F ',' '{if($2 ~/\./ ) {print $1,$2 }}' 20160905_names > ttt1 awk -F ',' '{if($2 !~/[0-9]+\.[ ...
- Java数据结构——栈
//================================================= // File Name : Stack_demo //-------------------- ...
- javascript的propertyIsEnumerable()方法使用介绍
hasOwnProperty() 方法用来判断某个对象是否含有指定的自身属性. propertyIsEnumerable()是用来检测属性是否属于某个对象的,如果检测到了,返回true,否则返回fal ...