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)个交 ...
随机推荐
- PHP设计模式(三)
注册器模式 这种模式比较简单好理解,在PHP框架中会经常用到,在某些比较大的PHP框架中,会在初始化时将一些常用的类实例放在注册器中,实际是存在注册器类中的一个静态数组中,以后想去用它的话,直接根据名 ...
- Hackerrank Going to the Office
传送门 Problem Statement Ms.Kox enjoys her job, but she does not like to waste extra time traveling to ...
- [Android] View.setTag(key,Object) (java.lang.IllegalArgumentException: The key must be an application-specific resource id.)
转自: http://blog.csdn.net/brokge/article/details/8536906 setTag是android的view类中很有用的一个方法,可以用它来给空间附加一些信息 ...
- Alpha版本十天冲刺——Day 7
站立式会议 祝曹鑫杰和常松童鞋生日快乐!短短几天冲刺,就迎来了三位队员的生日,希望也给我们的Alpha版本带来好运,加油! 会议总结 队员 今天完成 遇到的问题 明天要做 感想 鲍亮 上传图片接口 无 ...
- RNN 入门教程 Part 1 – RNN 简介
转载 - Recurrent Neural Networks Tutorial, Part 1 – Introduction to RNNs Recurrent Neural Networks (RN ...
- [COCI2012Final]Pro1
校内OJ上的题. 数据范围非常小,用暴搜就可以,加点剪枝阶乘级别的复杂度竟然可以跑得比$O(N^4)$的算法还要快QAQ. 我用的是Floyd,参考了别人的代码.大概就是先跑个Floyd把点点之间路径 ...
- Docker入门教程(二)命令
Docker入门教程(二)命令 [编者的话]DockerOne组织翻译了Flux7的Docker入门教程,本文是系列入门教程的第二篇,介绍了Docker的基本命令以及命令的用法和功能. 在Docker ...
- 最简单的jQuery插件
<script src="./jquery-1.7.1.min.js"></script><script>;(function($,undefi ...
- Spring MVC学习笔记——注解式控制器
- HTML学习笔记——选择器
1> ID选择器.交叉选择器.群组选择器.子代选择器 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN& ...