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$,并且所 ...
随机推荐
- lintcode-127-拓扑排序
127-拓扑排序 给定一个有向图,图节点的拓扑排序被定义为: 对于每条有向边A--> B,则A必须排在B之前 拓扑排序的第一个节点可以是任何在图中没有其他节点指向它的节点 找到给定图的任一拓扑排 ...
- servletContext的定义
- 【C++ 拾遗】extern 关键字
Separate compilation allows programs to be written in logical parts. let us split our programs into ...
- BZOJ 2502: 清理雪道 | 有上下界最小流
#include<cstdio> #include<algorithm> #include<cstring> #include<queue> #defi ...
- BZOJ1305 [CQOI2009]dance跳舞 【网络流】
1305: [CQOI2009]dance跳舞 Time Limit: 5 Sec Memory Limit: 162 MB Submit: 3714 Solved: 1572 [Submit][ ...
- 【NOIP 模拟赛】钟 模拟+链表
biubiu~~ 这道题实际上就是优化模拟,就是找到最先死的让他死掉,运用时间上的加速,题解上说,要用堆优化,也就是这个意思. 对于链表,单项链表和循环链表都不常用,最常用的是双向链表,删除和插入比较 ...
- 【BZOJ 2006】[NOI2010]超级钢琴 ST
我们先把所有最左端对应的最优右端入堆,eg: z 在[l,r](由题目给出的L,R决定)之间的最优解 y,然后出堆以后,再入堆z,y-1,z,y+1,那么我们只需要用st找最大前缀和就好了(ST是一 ...
- HTML5 视频直播
目前视频直播,尤其是移动端的视频直播已经火到不行了,基本上各大互联网公司都有了自己的直播产品,所以对于直播的一些基本知识和主要技术点也要有所了解,本次分享就向大家介绍一下其中的奥秘. 内容大体框架: ...
- mysql————表类型(存储引擎)的选择
表类型(存储引擎)的选择 7.1 mysql存储引擎概述 插件式存储引擎是mysql数据库最重要的特性之一,用户可以根据应用的需要选择ruhr存储和索引数据,是否使用事务等. InnoDB和BDB提供 ...
- 使用mysqldump命令备份恢复MySQL数据库
1.各种用法说明 A. 最简单的用法: mysqldump -uroot -pPassword [database name] > [dump file] 上述命令将指定数据库备份到某dump文 ...