hdu 3870(平面图最小割转最短路)
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
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.
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).
3
10 5 5
6 6 20
4 7 9
The map is like this:
#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(平面图最小割转最短路)的更多相关文章
- 【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个,直接最大流不好.这题的图是平面图,求最小割可以转化成求其对偶图的最短路,来更高效地求解: 首先源 ...
- BZOJ1001 [BeiJing2006]狼抓兔子(平面图最小割转最短路)
..和HDU3870类似..注意n=1和m=1的情况. #include<cstdio> #include<cstring> #include<queue> #in ...
- BZOJ1001/LG4001 「ICPC Beijing2006」狼抓兔子 平面图最小割转对偶图最短路
问题描述 BZOJ1001 LG4001 题解 平面图最小割=对偶图最短路 假设起点和终点间有和其他边都不相交的一条虚边. 如图,平面图的若干条边将一个平面划分为若干个图形,每个图形就是对偶图中的一个 ...
- [BZOJ 2007] [Noi2010] 海拔 【平面图最小割(对偶图最短路)】
题目链接:BZOJ - 2007 题目分析 首先,左上角的高度是 0 ,右下角的高度是 1.那么所有点的高度一定要在 0 与 1 之间.然而选取 [0, 1] 的任何一个实数,都可以用整数 0 或 1 ...
- Luogu2046 NOI2010 海拔 平面图、最小割、最短路
传送门 首先一个不知道怎么证的结论:任意点的\(H\)只会是\(0\)或\(1\) 那么可以发现原题的本质就是一个最小割,左上角为\(S\),右下角为\(T\),被割开的两个部分就是\(H=0\)与\ ...
- BZOJ 2007 海拔(平面图最小割转对偶图最短路)
首先注意到,把一个点的海拔定为>1的数是毫无意义的.实际上,可以转化为把这些点的海拔要么定为0,要么定为1. 其次,如果一个点周围的点的海拔没有和它相同的,那么这个点的海拔也是可以优化的,即把这 ...
- BZOJ2007/LG2046 「NOI2010」海拔 平面图最小割转对偶图最短路
问题描述 BZOJ2007 LG2046 题解 发现左上角海拔为 \(0\) ,右上角海拔为 \(1\) . 上坡要付出代价,下坡没有收益,所以有坡度的路越少越好. 所以海拔为 \(1\) 的点,和海 ...
- bzoj2007/luoguP2046 海拔(平面图最小割转对偶图最短路)
bzoj2007/luoguP2046 海拔(平面图最小割转对偶图最短路) 题目描述: bzoj luogu 题解时间: 首先考虑海拔待定点的$h$都应该是多少 很明显它们都是$0$或$1$,并且所 ...
随机推荐
- 用vs调试项目页面无样式
ASP.NET Development Server 上的文件授权 在文件系统网站中,静态文件(例如图像和样式表)遵守 ASP.NET 授权.例如,如果禁用了对静态文件的匿名访问,匿名用户则不能使用文 ...
- lintcode-74-第一个错误的代码版本
74-第一个错误的代码版本 代码库的版本号是从 1 到 n 的整数.某一天,有人提交了错误版本的代码,因此造成自身及之后版本的代码在单元测试中均出错.请找出第一个错误的版本号. 你可以通过 isBad ...
- Luogu3952 NOIP2017时间复杂度
搞一个栈模拟即可.对比一下和一年前考场上的代码233 //2018.11.8 #include<iostream> #include<cstdio> #include<c ...
- [ZJOI2005]沼泽鳄鱼 矩阵乘法
---题面--- 题解: 乍一看还是挺懵逼的.和HH去散步很像,思路也是类似的. 复制一段我在HH去散步的题解里面写的一段话吧: 考虑f[i][j]表示i和j是否右边相连,有为1,否则为0,那么f同时 ...
- vue.js 三种方式安装--npm安装
Vue.js是一个构建数据驱动的 web 界面的渐进式框架. Vue.js 的目标是通过简单的 API 实现响应的数据绑定和组合的视图组件.它不仅易上手,便于与第三方库或既有项目整合. ...
- BZOJ1787 [Ahoi2008]Meet 紧急集合 【LCA】
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MB Submit: 3578 Solved: 1635 [Submi ...
- 使用google api material icons在网页中插入图标
在<head></head>中加入这一句: <link rel='stylesheet' href='http://fonts.googleapis.com/icon?f ...
- centos的网络设置问题
遭遇了多次centos的网络连接问题,现将正确配置总结下: 这里是使用vmware虚拟平台,因为涉及到中间这层,所以需要设置下: 保证centos也能连上网,首先物理机连上网,接着物理机的vmware ...
- charles 踩坑记录
charles破解教程:http://www.jianshu.com/p/12e75eb8f53d 1.需注意软件和破解脚本的版本是否正确(例如3.x.x版本的破解脚本不能用于4.x.x版本的char ...
- 有关getClassLoader().getResourceAsStream(fileName)、class.getResourceAsStream(fileName)和().getContextClassLoader().getResourceAsStream(fileName)的区别
一:前言 在自己获取属性时,碰见了XX.class.getResourceAsStream(fileName),自己对这个其实不是很理解,上网查了下资料,又看到了上述的几个,所以就研究了下. 二:内容 ...