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. JFrame编程

    最基础的窗口 public test1() { setTitle("test1"); setSize(300,300); //设置窗口大小 setLocation(300,300) ...

  2. 第21/22讲 UI_布局 之 线性布局

    第21/22讲 UI_布局 之 线性布局 布局管理就是组件在activity中呈现方式,包括组件的大小,间距和对齐方式等. Android提供了两种布局的实现方式: 1.在xml配置文件中声明:这种方 ...

  3. iOS 语音识别使用讯飞报错

    You must rebuild it with bitcode enabled(Xcode setting ENABLE_BITCODE), obtain an updated library fr ...

  4. 编写高质量代码改善python程序91个建议学习01

    编写高质量代码改善python程序91个建议学习 第一章 建议1:理解pythonic的相关概念 狭隘的理解:它是高级动态的脚本编程语言,拥有很多强大的库,是解释从上往下执行的 特点: 美胜丑,显胜隐 ...

  5. css中的列表样式

    在网页设计中,我们经常将某些具有相似功能的标签放在同一组中,这时我们经常会用到列表标签(无序列表ul,有序列表ol),在列表标签中对列表样式的设计可以使我们的页面得到一定程度的美化. 在css中对列表 ...

  6. Android Studio编译好的apk放在哪里?

    Eclipse中编译好的apk文件时在bin文件中面的,可是在Android Studio有一个比較大的修改了,编译好的apk在android studio里面是直接看不到了,并且apk文件所在文件夹 ...

  7. LNMP、LAMP、LANMP一键安装脚本(定期更新)[转]

    这个脚本是使用shell编写,为了快速在生产环境上部署LNMP/LAMP/LANMP(Linux.Nginx/Tengine.MySQL/MariaDB/Percona.PHP),适用于CentOS/ ...

  8. OpenMp 基本

      OpenMp是由OpenMP Architecture Review Board牵头提出的,并已被广泛接受的,用于共享内存并行系统的多线程程序设计的一套指导性的编译处理方案(Compiler Di ...

  9. 一张图解析如何让img垂直居中对齐

    测试代码: <!DOCTYPE html> <html> <head> <style> .dd { background-color: gray; po ...

  10. 解决 innerHTML 在 IE6-IE9中不能赋值的bug

    在MSDN可以了解跟多,关于innerHTML的介绍,但是在这里只要是解决表格部分问题 MSDN上有这样的记录: When using innerHTML to insert script, you ...