HDU3870-Caught these thieves(最小割->偶图->最短路问题)
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
Hint
The map is like this:
题解:我们根据欧拉函数可以将其转化为偶图,然后加起点和终点,转化为求最短路问题;
AC代码为:
//最小割转化为最短路问题(偶图为稀疏图,故用FIFO队列的SPFA在O(nlogn)时间内稳定)
#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=170000;
struct point
{
int v,w;
}Point;
vector<point> e[maxn];
int n,S,T,q[maxn];
int dis[maxn];
bool inq[maxn];
int SPFA()
{
int i,k,head=0,tail=0;
memset(inq,false,sizeof inq);
for(i=0;i<=(n-1)*(n-1)+1;i++) dis[i]=INF;
q[tail++]=S;
dis[S]=0;
inq[S]=true;
while(head!=tail)
{
k=q[head];
head=(head+1)%maxn;
inq[k]=false;
for(i=0;i<e[k].size();i++)
{
Point=e[k][i];
if(dis[Point.v]>dis[k]+Point.w)
{
dis[Point.v]=dis[k]+Point.w;
if(!inq[Point.v])
{
inq[Point.v]=true;
q[tail]=Point.v;
tail=(tail+1)%maxn;
}
}
}
}
return dis[T];
}
int main()
{
int t,i,j,k;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=0;i<(n-1)*(n-1)+2;i++) e[i].clear();
S=(n-1)*(n-1);
T=(n-1)*(n-1)+1;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&k);
Point.w=k;
if(i==0&&j!=n-1)
{
Point.v=j;
e[S].push_back(Point);
}
if(j==n-1&&i!=n-1)
{
Point.v=i*(n-1)+j-1;
e[S].push_back(Point);
}
if(j==0&&i!=n-1)
{
Point.v=T;
e[i*(n-1)].push_back(Point);
}
if(i==n-1&&j!=n-1)
{
Point.v=T;
e[(n-2)*(n-1)+j].push_back(Point);
}
if(i!=n-1&&j!=n-1)
{
if(i)
{
Point.v=(i-1)*(n-1)+j;
e[i*(n-1)+j].push_back(Point);
Point.v=i*(n-1)+j;
e[(i-1)*(n-1)+j].push_back(Point);
}
if(j)
{
Point.v=i*(n-1)+j-1;
e[i*(n-1)+j].push_back(Point);
Point.v=i*(n-1)+j;
e[i*(n-1)+j-1].push_back(Point);
}
}
}
}
printf("%d\n",SPFA());
}
return 0;
}
HDU3870-Caught these thieves(最小割->偶图->最短路问题)的更多相关文章
- bzoj 1001 狼抓兔子 —— 平面图最小割(最短路)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1001 平面图最小割可以转化成最短路问题: 建图时看清楚题目的 input ... 代码如下: ...
- URAL1277 Cops and Thieves(最小割)
Cops and Thieves Description: The Galaxy Police (Galaxpol) found out that a notorious gang of thieve ...
- HDU3870 Catch the Theves(平面图最小割转最短路)
题目大概说给一个n×n的方格,边有权值,问从求(1,1)到(n,n)的最小割. 点达到了160000个,直接最大流不好.这题的图是平面图,求最小割可以转化成求其对偶图的最短路,来更高效地求解: 首先源 ...
- HDU3491 Thieves(最小割)
题目大概说,一个国家有n个城市,由m条双向路相连,小偷们从城市s出发准备到h城市,警察准备在某些除了s和h外的城市布置警力抓小偷,各个城市各有警力所需的数目.问警察最少要布置多少警力才能万无一失地抓住 ...
- hdu3870 基于最短路的最小割
题意: 给你一个平面图,让你输出(1,1),(n ,n)的最小割.. 思路: 看完题想都没想直接最大流,结果TLE,想想也是 G<400*400,400*400*4> ...
- hdu3870-Catch the Theves(平面图最小割)
Problem Description A group of thieves is approaching a museum in the country of zjsxzy,now they are ...
- hdu 3870(平面图最小割转最短路)
Catch the Theves Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 65768/32768 K (Java/Others) ...
- hdu4289 最小割最大流 (拆点最大流)
最小割最大流定理:(参考刘汝佳p369)增广路算法结束时,令已标号结点(a[u]>0的结点)集合为S,其他结点集合为T=V-S,则(S,T)是图的s-t最小割. Problem Descript ...
- 最小割 总结&&做题记录
模型要点: 1.一般适用于二取一问题或者01规划. 2.利用最小割=最大流,转化为最大流求之. 建议阅读胡伯涛的论文 <<最小割模型在信息学竞赛的应用>>,有精彩有序的证明和各 ...
随机推荐
- JAVA项目打包成可运行的exe程序
前言:本篇文章为原创,转载请注明地址,谢谢. 我们一些时候,可能需要需要把我们完成的java打包,打成jar文件或者exe文件.这时候就请鄙人的这篇文章. 言尽于此,Let‘s go! 一.导出jar ...
- @resource和@autowired的区别是什么-CSDN论坛-CSDN.NET-中国最大的IT技术社区 - Google Chrome
@Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了.@Resource有两个属性是比较重要的,分 ...
- Linux命令实战(一)
1.pwd(printing working directory)打印当前工作目录路径 [root@test sysconfig]# pwd /etc/sysconfig 2.ls(list)列出当前 ...
- .NET Core3.0 EF 连接 MySql
一:创建项目 添加 csproj (或者直接NuGet 引用) <ItemGroup> <PackageReference Include="Microsoft.Entit ...
- Oracle instant client免安装Oracle客户端配置
不想安装几个G的完整版client,可以直接通过安装包安装的时候选择instant client,如果没有安装包,也可以直接去官网下载一个即时客户端,64位的windows包大小只有78MB左右 传送 ...
- Linux下编写-makefile-详细教程(跟我一起写-Makefile-Markdown整理版)
目录 概述 关于程序的编译和链接 Makefile 介绍 Makefile的规则 一个演示例子 make是怎样工作的 makefile中使用变量 让make自己主动推导 另类风格的makefile 清 ...
- Leetcode算法【114. 二叉树展开为链表】
上周通过一位小伙伴,加入了一个氛围很好的小群,人不多,但是大家保持着对知识的渴望,让我很感动. 我自己也有一个群,人数也不多,但是能真正互动起来一起学习,一起进步的,还是太少.所以,现在也在学习如何让 ...
- hdu 1024 Max Sum Plus Plus (动态规划)
Max Sum Plus PlusTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- A Lot of Games(Trie树 + 博弈)
题目链接:http://codeforces.com/contest/455/problem/B 题意:n, k 分别表示 字符串组数 和 比赛次数. 从一个空单词开始, a,b二人分别轮流往单词后 ...
- 注意android辅助服务事件不能用于保存
本来希望把来自辅助服务的事件,像epoll那样暂存在队列进行调度,或者做成事件堆栈,从而将辅助服务事件加入到容器.但是一直不能达到预期的后果.最后才发现一个坑人的事实,辅助服务事件被释放(或者说重置) ...