Animal Run

Time Limit:6000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

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.

Figure 1: This is a 3 x 4 nodes grid, the number beside the path indicating how many staff shall be sent to block this path

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 nm 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

For each case, output how many staff at least shall be sent to block all animals. Please output in the following format.

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)的更多相关文章

  1. 【BZOJ1001】狼抓兔子(平面图最小割转最短路)

    题意:有一张平面图,求它的最小割.N,M.表示网格的大小,N,M均小于等于1000. 左上角点为(1,1),右下角点为(N,M).有以下三种类型的道路  1:(x,y)<==>(x+1,y ...

  2. HDU3870 Catch the Theves(平面图最小割转最短路)

    题目大概说给一个n×n的方格,边有权值,问从求(1,1)到(n,n)的最小割. 点达到了160000个,直接最大流不好.这题的图是平面图,求最小割可以转化成求其对偶图的最短路,来更高效地求解: 首先源 ...

  3. [bzoj 1001][Beijing2006]狼抓兔子 (最小割+对偶图+最短路)

    Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的, 而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一 ...

  4. BZOJ1001 [BeiJing2006]狼抓兔子 最小割 对偶图 最短路

    原文链接http://www.cnblogs.com/zhouzhendong/p/8686871.html 题目传送门 - BZOJ1001 题意 长成上面那样的网格图求最小割. $n,m\leq ...

  5. Luogu2046 NOI2010 海拔 平面图、最小割、最短路

    传送门 首先一个不知道怎么证的结论:任意点的\(H\)只会是\(0\)或\(1\) 那么可以发现原题的本质就是一个最小割,左上角为\(S\),右下角为\(T\),被割开的两个部分就是\(H=0\)与\ ...

  6. BZOJ.2007.[NOI2010]海拔(最小割 对偶图最短路)

    题目链接 想一下能猜出,最优解中海拔只有0和1,且海拔相同的点都在且只在1个连通块中. 这就是个平面图最小割.也可以转必须转对偶图最短路,不然只能T到90分了..边的方向看着定就行. 不能忽略回去的边 ...

  7. bzoj 2007 [Noi2010]海拔——最小割转最短路

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2007 一个点的高度一定不是0就是1.答案一定形如一个左上角的连通块全是0的点.一个右下角的连 ...

  8. P2046 [NOI2010]海拔 平面图转对偶图(最小割-》最短路)

    $ \color{#0066ff}{ 题目描述 }$ YT市是一个规划良好的城市,城市被东西向和南北向的主干道划分为n×n个区域.简单起见,可以将YT市看作 一个正方形,每一个区域也可看作一个正方形. ...

  9. 【bzoj2007】[Noi2010]海拔 最小割+对偶图+最短路

    题目描述 YT市是一个规划良好的城市,城市被东西向和南北向的主干道划分为n×n个区域.简单起见,可以将YT市看作一个正方形,每一个区域也可看作一个正方形.从而,YT城市中包括(n+1)×(n+1)个交 ...

随机推荐

  1. Distance Between Points

    I need some help. I have to create a function that will calculate the distance between points (x1,y1 ...

  2. mysql 某周的起始和结束日期

    转自:http://bbs.csdn.net/topics/370096126 t_table有数据如下:year    Week2011    22011    32011    42011     ...

  3. python 培训之爬虫

    1. 输入文件为 fufang_list.txt yaofang_a aaiwan 阿艾丸 yaofang_a aaiwulingsan 阿艾五苓散 yaofang_a acaitang 阿菜汤 ya ...

  4. Yocto开发笔记之《应用程序架构》(QQ交流群:519230208)

    QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 ======================================================== Eclip ...

  5. glade2支持C++代码的输出(1)

    开发了一个基类,用于支持GTK2的信号回调 见BaseObject.zip 为了便于快速通过glade设计界面,并生成相应的C++代码,我对glade-2 2.12.2的代码进行了修改 原始代码:gl ...

  6. C#------各种常见错误解决方法

    1.错误:模型生成过程中检测到一个或多个验证错误 表示实体中的数据列没有和SQLServer数据库里面的表中的数据列完全相同,比如SQLServer中有ID,Name,Post,那么实体中也应该有ID ...

  7. 查询centos查看系统内核版本,系统版本,32位还是64位

    [root@centos01 ~]# lsb_release -a           #查看centos 版本为6.4LSB Version: :base-4.0-amd64:base-4.0-no ...

  8. 浏览器本地存储(browser-storage,HTML5-localStorage > IE-UserData > Cookie)

    https://www.baidufe.com/component/browser-storage/index.html BrowserStorage是浏览器本地存储的一个解决方案,存储优先级依次为: ...

  9. MathML + MathJax在网页中插入公式

    http://www.mathjax.org/download/ http://www.w3.org/Math/Software/mathml_software_cat_editors.html ht ...

  10. $().index() 两种用法

    第一种:获得第一个 p 元素的名称和值: $(this).index() <script type="text/javascript"> $(document).rea ...