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. 软工实践Beta冲刺(3/7)

    队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 1.界面的修改与完善 展示GitHub当日代码/文档签入记 ...

  2. Could not resolve com.android.support.constraint:constraint-layout:1.1.3.

    原文地址: http://fanjiajia.cn/2018/09/25/Android%20Studio%20Could%20not%20resolve%20com.android.support. ...

  3. 数据结构—栈(Stack)

    栈的定义--Stack 栈是只允许在末端进行插入和删除的线性表.栈具有后进先出的特性(LIFO ,Last In Fast Out). 学过数据结构的人都知道:栈可以用两种方式来实现,一种方法是用数组 ...

  4. hdu 3172 Virtual Friends (并查集)

    Virtual Friends Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  5. [BZOJ1339] [Baltic2008] Mafia / 黑手党

    Description 匪徒准备从一个车站转移毒品到另一个车站,警方准备进行布控. 对于每个车站进行布控都需要一定的代价, 现在警方希望使用最小的代价控制一些车站,使得去掉这些车站后,匪徒无法从原定的 ...

  6. vim配置入门,到豪华版vim配置

    这几天一直研究vim的配置,许多版本总是不尽如人意,网上确实有许多优秀的文章值得参考,我的博客后面会贴上具有参考价值的博客链接,本文的将手把手教你配置一个功能详尽的vim. 首先你要明白的是linux ...

  7. JS让任意图片垂直水平居中且页面不滚动

    说一下以前遇到的一个问题: 假设有一张小图,要实现点击查看大图的功能,而这个图的宽高可能会超过浏览器的宽高,这时候我们通过JS来改变图片的宽高,从而实现图片在浏览器居中显示且不滚屏. 方法如下: 首先 ...

  8. makefile使用笔记(一)入门

    By francis_hao    Mar 2,2017 makefile makefile一个很简单的例子如下,该实例完成在执行make时,将main.c编译成可执行文件main的功能. 各项的含义 ...

  9. <video>标签的特性

    以前的网页视频 过去还没有HTML5的时候,我们处理网页视频的时候,都是通过Flash插件来实现的,然而,并非所有浏览器都有同样的插件. 现在有了HTML5带来的video元素,开发者能够很方便地将视 ...

  10. ShareSDK入门指南:Android 10分钟快速集成

    ShareSDK 官方已提供Android 快速集成教程,以官方教程为参考,本文重点指导大家在集成中遇到的问题. Android 快速集成官方教程:http://wiki.mob.com/Androi ...