Catch the Theves

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65768/32768 K (Java/Others)
Total Submission(s): 1640    Accepted Submission(s): 514

Problem Description
A
group of thieves is approaching a museum in the country of zjsxzy,now
they are in city A,and the museum is in city B,where keeps many broken
legs of zjsxzy.Luckily,GW learned the conspiracy when he is watching
stars and told it to zjsxzy.
Zjsxzy decided to caught these
thieves,and he let the police to do this,the police try to catch them on
their way from A to B. Although the thieves might travel this way by
more than one group, zjsxzy's excellent police has already gather the
statistics that the cost needed on each road to guard it.
Now
,zjsxzy's conutry can be described as a N*N matrix A,Aij indicates the
city(i,j) have bidirectionals road to city(i+1,j) and city(i,j+1),gurad
anyone of them costs Aij.
Now give you the map,help zjsxzy to
calculate the minimium cost.We assume thieves may travel in any way,and
we will catch all passing thieves on a road if we guard it.
 
Input
The first line is an integer T,followed by T test cases.
In each test case,the first line contains a number N(1<N<=400).
The following N lines,each line is N numbers,the jth number of the ith line is Aij.
The city A is always located on (1,1) and the city B is always located on (n,n).
Of course,the city (i,j) at the last row or last line won't have road to (i,j+1) or (i+1,j).
 
Output
For each case,print a line with a number indicating the minimium cost to arrest all thieves.
 
Sample Input
1
3
10 5 5
6 6 20
4 7 9
 
Sample Output
18

Hint

The map is like this:

 
Source
 
 
题意:从左上角走到右下角的方案数.
题解:一看以为是最小割,但是点有1600000个,显然不现实,其实刘汝佳书里面有个类似的题,,不过没看懂方法。。然后到网上找题解,,发现构造对偶图然后求解对偶图的最短路。。好神奇
#include <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
const int INF = ;
const int N = ;
const int M = N*N;
int a[N][N];
struct Edge{
int v,w,next;
}edge[*M];
int head[M];
int tot,n;
void addEdge(int u,int v,int w,int &k){
edge[k].v = v,edge[k].w = w,edge[k].next = head[u],head[u] = k++;
}
void init(){
memset(head,-,sizeof(head));
tot = ;
}
bool vis[M];
int low[M];
int spfa(int s,int t){
for(int i=;i<=t;i++){
low[i] = INF;
vis[i] = false;
}
low[s] = ;
queue<int> q;
q.push(s);
while(!q.empty()){
int u = q.front();
// printf("%d\n",u);
q.pop();
vis[u] = false;
for(int k = head[u];k!=-;k = edge[k].next){
int v = edge[k].v,w=edge[k].w;
// printf("%d %d\n",v,w);
if(low[v]>low[u]+w){
low[v] = low[u]+w;
if(!vis[v]){
vis[v] = true;
q.push(v);
}
}
}
}
return low[t];
}
int main(){
int tcase;
scanf("%d",&tcase);
while(tcase--){
init();
scanf("%d",&n); for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
scanf("%d",&a[i][j]);
}
}
n-=;
int s = ,t = n*n+;
/**构造对偶图*/
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
int now = (i-)*n+j;
int next1 = (i-)*n+j+;
int next2 = (i-)*n+j+n;
if(j!=n) {
addEdge(now,next1,a[i][j+],tot);
addEdge(next1,now,a[i][j+],tot);
}
if(i!=n){
addEdge(now,next2,a[i+][j],tot);
addEdge(next2,now,a[i+][j],tot);
}
if(j==){
addEdge(s,now,a[i][j],tot);
addEdge(now,s,a[i][j],tot);
}
if(i==n){
addEdge(s,now,a[i+][j],tot);
addEdge(now,s,a[i+][j],tot);
}
if(i==){
addEdge(t,now,a[i][j],tot);
addEdge(now,t,a[i][j],tot);
}
if(j==n){
addEdge(t,now,a[i][j+],tot);
addEdge(now,t,a[i][j+],tot);
}
}
}
printf("%d\n",spfa(s,t));
}
return ;
}

hdu 3870(平面图最小割转最短路)的更多相关文章

  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. BZOJ1001 [BeiJing2006]狼抓兔子(平面图最小割转最短路)

    ..和HDU3870类似..注意n=1和m=1的情况. #include<cstdio> #include<cstring> #include<queue> #in ...

  4. BZOJ1001/LG4001 「ICPC Beijing2006」狼抓兔子 平面图最小割转对偶图最短路

    问题描述 BZOJ1001 LG4001 题解 平面图最小割=对偶图最短路 假设起点和终点间有和其他边都不相交的一条虚边. 如图,平面图的若干条边将一个平面划分为若干个图形,每个图形就是对偶图中的一个 ...

  5. [BZOJ 2007] [Noi2010] 海拔 【平面图最小割(对偶图最短路)】

    题目链接:BZOJ - 2007 题目分析 首先,左上角的高度是 0 ,右下角的高度是 1.那么所有点的高度一定要在 0 与 1 之间.然而选取 [0, 1] 的任何一个实数,都可以用整数 0 或 1 ...

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

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

  7. BZOJ 2007 海拔(平面图最小割转对偶图最短路)

    首先注意到,把一个点的海拔定为>1的数是毫无意义的.实际上,可以转化为把这些点的海拔要么定为0,要么定为1. 其次,如果一个点周围的点的海拔没有和它相同的,那么这个点的海拔也是可以优化的,即把这 ...

  8. BZOJ2007/LG2046 「NOI2010」海拔 平面图最小割转对偶图最短路

    问题描述 BZOJ2007 LG2046 题解 发现左上角海拔为 \(0\) ,右上角海拔为 \(1\) . 上坡要付出代价,下坡没有收益,所以有坡度的路越少越好. 所以海拔为 \(1\) 的点,和海 ...

  9. bzoj2007/luoguP2046 海拔(平面图最小割转对偶图最短路)

    bzoj2007/luoguP2046 海拔(平面图最小割转对偶图最短路) 题目描述: bzoj  luogu 题解时间: 首先考虑海拔待定点的$h$都应该是多少 很明显它们都是$0$或$1$,并且所 ...

随机推荐

  1. lintcode-76-最长上升子序列

    76-最长上升子序列 给定一个整数序列,找到最长上升子序列(LIS),返回LIS的长度. 说明 最长上升子序列的定义: 最长上升子序列问题是在一个无序的给定序列中找到一个尽可能长的由低到高排列的子序列 ...

  2. jquery $.getJSON 注意细节

    服务端: var json = "{\"title\": \"Recent Uploads tagged mountrainier\",\" ...

  3. Codeforces Round #518 Div. 1没翻车记

    A:设f[i][j][0/1]为前i个数第i位为j且第i位未满足/已满足限制的方案数.大力dp前缀和优化即可. #include<iostream> #include<cstdio& ...

  4. 【题解】JSOI2009游戏

    真的没想到...果然反应太迟钝,看到题目毫无思路,一点联想都没有. 按照网上博客的说法:一眼棋盘染色二分->二分图->最大匹配->BINGO?果然我还是太弱了…… 我们将棋盘黑白染色 ...

  5. [洛谷P1131][ZJOI2007]时态同步

    题目大意:给你一棵树,每条边有边权,要求增加一些边的边权,使得根节点到每个叶子节点的距离相等,求出最少共增加多少边权. 题解:树形$DP$,对于每个点,如果它到它的子树中的叶子节点距离不同,一定要在这 ...

  6. C++——内存使用

    内存分配方式: (1)从静态存储区域分配.内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.例如全局变量,static变量. (2)在栈上创建.在执行函数时,函数内局部变量的存储单 ...

  7. Java代码管理工具SVN系列

    SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上很多版本控制服务已从CVS迁移到Subversion ...

  8. hadoop基础----hadoop实战(七)-----hadoop管理工具---使用Cloudera Manager安装Hadoop---Cloudera Manager和CDH5.8离线安装

    hadoop基础----hadoop实战(六)-----hadoop管理工具---Cloudera Manager---CDH介绍 简介 我们在上篇文章中已经了解了CDH,为了后续的学习,我们本章就来 ...

  9. [8.16模拟赛] 玩具 (dp/字符串)

    题目描述 儿时的玩具总是使我们留恋,当小皮还是个孩子的时候,对玩具更是情有独钟.小皮是一个兴趣爱好相当广泛且不专一的人,这这让老皮非常地烦恼.也就是说,小皮在不同时刻所想玩的玩具总是会不同,而有心的老 ...

  10. Codeforces Round #525 (Div. 2)A. Ehab and another construction problem

    A. Ehab and another construction problem 题目链接:https://codeforc.es/contest/1088/problem/A 题意: 给出一个x,找 ...