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. WustOJ 1575 Gingers and Mints(快速幂 + dfs )

    1575: Gingers and Mints Time Limit: 1 Sec  Memory Limit: 128 MB   64bit IO Format: %lldSubmitted: 24 ...

  2. 3D objects key rendering steps

    Key steps of Rendering objects: 1 Create objects’ meshes, which we can use C++’s vector container to ...

  3. paip.sql索引优化----join 代替子查询法

    paip.sql索引优化----join 代替子查询法 作者Attilax ,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.n ...

  4. HTML5新增的一些属性和功能之八——web Worker

    Web Workers 为什么用web workers? 浏览器的原理中决定了页面打开只有一个主线程--UI渲染线程,如果线程中有耗时的程序(js)会阻塞线程,使得页面中其他的UI无法渲染,我们一般把 ...

  5. C++——try、throw、catch实例学习程序

    #include<iostream> #include<stdexcept> //exception/stdexcept/new/type_info头文件里都有定义的标准异常类 ...

  6. Android设置定时执行执行一次任务

    private Handler handler = new Handler(){ public void handleMessage(Message msg) { super.handleMessag ...

  7. TFS 用户与组管理(转)

    作者:frank.liu kaka.zhou 安装 Team Foundation Server 后,会创建以下全局组.可以使用这些全局组来控制 Team Foundation 用户的权限. 组 权限 ...

  8. FileUpload

    一upload原理: 1.表单的method必须是post方法 2.enctype属性必须是“mutipatr/form-data”类型 enctype默认的属性是“application/x-www ...

  9. jvm的client和server

    最近研究c++代码调用java的jar,发现64位的下的jvm在server路径,而32位的jvm则存在client路径下面,于是十分好奇,查了下,这里做个记录 JVM Server模式与client ...

  10. push方法的页面间跳转--

    一,自定义动画写push方法-- 添加coreGraphics.framework框架 在CATransitionAnimation.h文件里面引入-- #import <QuartzCore/ ...