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)个交 ...
随机推荐
- OpenGLES入门笔记三
在入门笔记一中比较详细的介绍了顶点着色器和片面着色器. 在入门笔记二中讲解了简单的创建OpenGL场景流程的实现,但是如果在场景中渲染任何一种几何图形,还是需要入门笔记一中的知识:Vertex Sha ...
- POJ1236Network of Schools(强连通分量 + 缩点)
题目链接Network of Schools 参考斌神博客 强连通分量缩点求入度为0的个数和出度为0的分量个数 题目大意:N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后 ...
- bigdecimal类型数据的min方法
java.math.BigDecimal.min(BigDecimal val) 返回此BigDecimal和val的最小值. 声明 以下是java.math.BigDecimal.min()方法的声 ...
- [转发]Linux的系统调用宏
原来在linux/include/linux/syscalls.h 中定义了如下的宏: 复制代码#define SYSCALL_DEFINE1(name, ...) SYSCALL_DEFINEx(1 ...
- c语言程序
汇编语言嵌入到c语言中 #include<stdio.h> int main(void) { int a,b,c; a=4; b=5; _asm { mov eax,a; add eax, ...
- hadoop2.6.4运行wordcount
hadoop用户登录,启动服务: start-dfs.sh && start-yarn.sh 创建输入目录: hadoop df -mkdir /input 把测试文件导入/input ...
- js 处理日期 看着比较全,备用
http://www.cnblogs.com/endora/archive/2012/12/06/endorahe.html js 处理日期 看着比较全,备用
- My latest news (--2016.10)
2016.10.31 22:44 一个“程序”,打代码占40%.思考占60% 2016.10.30 20:53 周末,话说今天有晚上讲座,还点名,了,悲催.之前学习的Qt有点问题,悲催.推荐个博文:h ...
- Xcode如何编译Debug版和Release版
在Run和Stop按钮的右边有一个工程名 点击工程名,选择Manage Schemes 选择Edit... 左侧选择Run ProjectName.app 右侧选择Info页,在Build Confi ...
- Occlusion Culling
遮挡剔除 http://www.bjbkws.com/online/1092/ unity遮挡剔除(应用) http://www.unitymanual.com/thread-37302-1-1.ht ...