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

解析:平面图最小割,把面当成点,然后用最短路求最小割,详见百度

代码

#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef __int64 LL;
const LL INF=;
const int maxn=**;
int N,eid,head[maxn];
struct edge{ int v,w,next; }E[*maxn];
void AddEdge(int u,int v,int w)
{
E[++eid].v=v; E[eid].w=w; E[eid].next=head[u]; head[u]=eid;
E[++eid].v=u; E[eid].w=w; E[eid].next=head[v]; head[v]=eid;
}
int f(int x,int y){ return x*(N-)+y; }
struct node
{
int u;
LL d;
node(int u=,LL d=):u(u),d(d){}
bool operator < (const node& t) const{ return d>t.d; }
};
priority_queue<node> que;
LL D[maxn];
bool vis[maxn];
int Dij(int S,int T)
{
while(!que.empty()) que.pop();
for(int i=;i<maxn;i++) D[i]=INF;
D[S]=;
memset(vis,false,sizeof(vis));
que.push(node(S,));
while(!que.empty())
{
node t=que.top(); que.pop();
int u=t.u;
if(vis[u]) continue;
vis[u]=true;
for(int i=head[u];i!=-;i=E[i].next)
{
edge& e=E[i];
int v=e.v,w=e.w;
if(D[v]>D[u]+w)
{
D[v]=D[u]+w;
que.push(node(v,D[v]));
}
}
}
return D[T];
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&N);
int s=,t=(N-)*(N-)+;
memset(head,-,sizeof(head));
eid=;
int u,v,w;
for(int i=;i<=N;i++)
for(int j=;j<=N;j++)
{
scanf("%d",&w);
if(i==&&j<N)
{
u=s; v=j;
AddEdge(u,v,w);
}
if(i==N&&j<N)
{
u=f(i-,j); v=t;
AddEdge(u,v,w);
}
if(<i&&i<N&&j<N)
{
u=f(i-,j);
v=f(i-,j);
AddEdge(u,v,w);
}
if(j==&&i<N)
{
u=f(i-,j); v=t;
AddEdge(u,v,w);
}
if(j==N&&i<N)
{
u=f(i-,j-); v=s;
AddEdge(u,v,w);
}
if(<j&&j<N&&i<N)
{
u=f(i-,j-);
v=f(i-,j);
AddEdge(u,v,w);
}
}
printf("%d\n",Dij(s,t));
}
return ;
}

hdu3870-Catch the Theves(平面图最小割)的更多相关文章

  1. tyvj P1209 - 拦截导弹 平面图最小割&&模型转化

    P1209 - 拦截导弹 From admin    Normal (OI)总时限:6s    内存限制:128MB    代码长度限制:64KB 背景 Background 实中编程者联盟为了培养技 ...

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

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

  3. B20J_2007_[Noi2010]海拔_平面图最小割转对偶图+堆优化Dij

    B20J_2007_[Noi2010]海拔_平面图最小割转对偶图+堆优化Dij 题意:城市被东西向和南北向的主干道划分为n×n个区域.城市中包括(n+1)×(n+1)个交叉路口和2n×(n+1)条双向 ...

  4. 【平面图最小割】BZOJ1001- [BeiJing2006]狼抓兔子

    [题目大意]左上角点为(1,1),右下角点为(N,M)(上图中N=4,M=5).有以下三种类型的道路 1:(x,y)<==>(x+1,y) 2:(x,y)<==>(x,y+1) ...

  5. 【平面图最小割】BZOJ2007-[NOI2010]海拔

    [题目大意] 城市被东西向和南北向的主干道划分为n×n个区域,包括(n+1)×(n+1)个交叉路口和2n×(n+1)条双向道路.现得到了每天每条道路两个方向的人流量.每一个交叉路口都有海拔,每向上爬h ...

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

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

  7. bzoj 1001 狼抓兔子 —— 平面图最小割(最短路)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1001 平面图最小割可以转化成最短路问题: 建图时看清楚题目的 input ... 代码如下: ...

  8. 【BZOJ1001】狼抓兔子(平面图最小割转最短路)

    题意:有一张平面图,求它的最小割.N,M.表示网格的大小,N,M均小于等于1000. 左上角点为(1,1),右下角点为(N,M).有以下三种类型的道路  1:(x,y)<==>(x+1,y ...

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

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

随机推荐

  1. MYSQL中的语句

    MYSQL中的语句 decimal(8,2):最多存10位数的数字,小数点后保存两位.如:999999.99

  2. poj 3273 Monthly Expense(二分搜索之最大化最小值)

    Description Farmer John ≤ moneyi ≤ ,) that he will need to spend each day over the next N ( ≤ N ≤ ,) ...

  3. shell脚本一条命令直接发送http请求(xjl456852原创)

    我们知道nc命令是一个网络工具.可以连接tcp/udp.也能模拟发送http请求. 现在介绍通过shell脚本,一条命令直接发送http请求. 命令如下,可以对下面的地址等信息自行修改: #!/bin ...

  4. Java之面向对象相关问题集

    面向对象的特征有哪些方面  1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解所有问题,而仅仅是选择当中的一部分,临时不用部分细节. 抽 ...

  5. nignx日志格式

    web-master的nginx格式: log_format web_format '$remote_addr $remote_port $remote_user [$time_local] ' '& ...

  6. ORA-02069: global_names parameter must be set to TRUE for this operation

    原因:在对远程表增删改操作的时候,调用了本地函数.  比如:insert into trans_load_rate@DC values(rate_s(1)); trans_load_rate是DC库的 ...

  7. MySql 事务+异常处理+异常抛出

    -- 测试用表 -- innodb 支持事务 CREATE TABLE `tb_test` ( `id` ) NOT NULL AUTO_INCREMENT, `name` ) NOT NULL, P ...

  8. css实现两端对齐~

    今天做表单时遇到让上下两个字段对齐的情况,手机号码.用户名. 然后今天在网上找了找相关方法,发现确实是没有什么好的方法解决,特别是当需要兼容的时候.找到了两个我觉得相对还不错的方法: 方法一.是在司徒 ...

  9. 让DIV垂直居中的几种办法

    1.使用CSS3 的伸缩盒布局 <!doctype html> <html> <head> <meta charset="utf-8"&g ...

  10. Katana概述

    OWIN owin是web services和framework组件之间的抽象.抽象包括两个核心要素: environment dictionary 这个数据结构存储处理HTTP请求必须的状态和相关的 ...