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$,并且所 ...
随机推荐
- pta数组作业
7-2 设计思路:本题要求处理数据并输出最大值及其对应的最小下标,首先输入n,然后定义一个长度为n的数组用于存储数据,定义m=a[0],n=0,从a[1]开始与m进行比较,若某项大于m,就把该项的值赋 ...
- iOS-调用百度地图,苹果自带地图,高德地图,谷歌地图导航方法
- (void)actionSheet : (ServiceNetworkModel *)model{ __block NSString *urlScheme = @"demoURI://& ...
- elasticsearch 6.2.3安装ik分词
下载 zip文件 上传到服务器 https://github.com/medcl/elasticsearch-analysis-ik/releases unzip elasticsearch-anal ...
- 【PHP】- session_cache_limiter(private,must-revalidate)是什么意思
session_cache_limiter(private,must-revalidate)是什么意思 表义一: 指定会话页面所使用的缓冲控制方法: 当session_cache_limiter('p ...
- 【bzoj4817】[Sdoi2017]树点涂色 LCT+LCA+线段树
题目描述 给出一棵n个点,以1为根的有根树,每个点初始染有互不相同的颜色.定义一条路径的权值为路径上的颜色种类数.现有m次操作,每次操作为以下三种之一: 1 x: 把点x到根节点的路径上所有的点染上一 ...
- BZOJ3211 花神游历各国 【树状数组 + 并查集】
题目 输入格式 输出格式 每次x=1时,每行一个整数,表示这次旅行的开心度 输入样例 4 1 100 5 5 5 1 1 2 2 1 2 1 1 2 2 2 3 1 1 4 输出样例 101 11 1 ...
- wya费用流
#include<bits/stdc++.h> using namespace std; #define M 1005 #define inf 0x7fffffff #define T 6 ...
- 安卓topbar编码实战
1.先在res->value下新建attrs.xml文件 <?xml version="1.0" encoding="utf-8"?> < ...
- BZOJ 2457 双端队列(思维
2457: [BeiJing2011]双端队列 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 582 Solved: 253[Submit][Sta ...
- Ecplise添加XML自动提示
这里以struts.xml为例 第一步: 首先找到 struts2的核心jar包,我这里是struts2-core-2.3.20.jar用压缩工具打开或者解压下来