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)个交 ...
随机推荐
- CF724C: Ray Tracing
传送门 CF的题质量真心不低,这道题的标准解法(应该)是exgcd,打比赛的时候想到了具体的推导公式了,也意识到了需要用exgcd,但是因为寝室要锁门了(其实就是太弱,就放弃了. 首先很显然,这条线所 ...
- Yocto开发笔记之《Tip-设置程序开机启动》(QQ交流群:519230208)
QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 IMX6UL,转载请注明出处 ============================================== ...
- 9.22 JS脚本语言DOM
通用:onclick 鼠标单击ondblclick 鼠标双击onmouseover 鼠标放上onmouseout 鼠标离开onmousemove 鼠标移动 表单:onchang 表单的值改变onblu ...
- 介绍ping中的TTL是什么意思
ping是icmp报文的一种应用.用来测试网络中各设备的连通性.在这几天的实验课上,我又用到了这个非常常用的命令,但是这次我发现了一些以前没有太注意的地方,那就是我在Ping不同的地址时所返回的TTL ...
- 9月6日表格标签(table、行、列、表头)(补)
一.<table> <table>代表表格标签. <table></table> 1.width 表示表格宽度,宽度表达方式有像素和百分比两种.网 ...
- How to convert any valid date string to a DateTime.
DateTimeFormatInfo pattern = new DateTimeFormatInfo() { ShortDatePattern = "your date pattern&q ...
- phpmyadmin 链接远程mysql
这个只是自己的笔记 新手 不记下来以后又忘记了~ 在这以前已经给mysql设置了可以远程连接的账户 版本 phpMyAdmin-4.2.11-all-languages 解压到D盘下www 本地环 ...
- Windwos下常用DOS命令
1.添加用户命令: net user 用户名 密码 /add 2.将用户加入组的命令: net localgroup administrators 用户名 /add 3.在dos命令行模式下启用用户: ...
- js中,还真不了解 console
参考链接: https://segmentfault.com/a/1190000000481884
- CSS学习点滴
1.CSS :link 选择器 a:link { background-color:yellow;text-decoration:none } 参考:http://www.w3school.com.c ...