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. 一个session已经ACTIVE20多小时,等待事件SQL*Net more data from client

    问题描述: 一个session已经ACTIVE20多小时,等待事件SQL*Net more data from client 有一人session,从昨天上午11点多登陆(v$session.logi ...

  2. <php>PDO链接方法

    <?php //定义数据源 $dsn = "mysql:dbname=mydb;host=localhost"; //$dsn = "sqlsrv:dbname=m ...

  3. web前端代码规范——css代码规范

    Bootstrap CSS编码规范 语法 用两个空格来代替制表符(tab) -- 这是唯一能保证在所有环境下获得一致展现的方法. 为选择器分组时,将单独的选择器单独放在一行. 为了代码的易读性,在每个 ...

  4. JSF和Struts的区别概述

    JSF和Struts的区别概述,都采用taglib来处理表示层:在jsp页面中,二者都是采用一套标记库来处理页面的表示和model层的交互. 据说JSF的主要负责人就是struts的主要作者,所以二者 ...

  5. mysql 1449 : The user specified as a definer (&#39;root&#39;@&#39;%&#39;) does not exist 解决方法

    权限问题,授权 给 root  全部sql 权限 mysql> grant all privileges on *.* to root@"%" identified by & ...

  6. jre配置了1.6,但是eclipse仍然提示需要1.6以上的java

    问题: eclipse创建web项目时,提示错误:Dynamic Web Module 3.0 requires Java 1.6 or newer. 原因: 这是因为当前的编译器java版本太低,请 ...

  7. IOS-沙盒机制(一 简述)

    一 IOS沙盒机制       出于安全的目的,应用程序仅仅能将自己的数据和偏好设置写入到几个特定的位置上.当应用程序被安装到设备上时,系统会为其创建一个家文件夹,这个家文件夹就是应用程序的沙盒.所以 ...

  8. 每天一点Swift(五)控制器的生命周期和SizeClass

    字数358 阅读19 评论0 喜欢0 初始化init-->awakeFromNib--> prepare a segue --> SB去设置outlets --> viewDi ...

  9. iOS在MRC工程环境下下使用ARC的方法

  10. C#中子窗体获取父窗体中控件的内容

    今天在做一个联系人管理的C#设计时,遇到了这个问题,我需要将父窗体中的textBox中的值传到子窗体并进行数据库查询操作,我用了new 父窗体().textBox.text;来进行值传递,然而并无卵用 ...