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(最小割-&gt;偶图-&gt;最短路问题)的更多相关文章

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

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

  2. URAL1277 Cops and Thieves(最小割)

    Cops and Thieves Description: The Galaxy Police (Galaxpol) found out that a notorious gang of thieve ...

  3. HDU3870 Catch the Theves(平面图最小割转最短路)

    题目大概说给一个n×n的方格,边有权值,问从求(1,1)到(n,n)的最小割. 点达到了160000个,直接最大流不好.这题的图是平面图,求最小割可以转化成求其对偶图的最短路,来更高效地求解: 首先源 ...

  4. HDU3491 Thieves(最小割)

    题目大概说,一个国家有n个城市,由m条双向路相连,小偷们从城市s出发准备到h城市,警察准备在某些除了s和h外的城市布置警力抓小偷,各个城市各有警力所需的数目.问警察最少要布置多少警力才能万无一失地抓住 ...

  5. hdu3870 基于最短路的最小割

    题意:      给你一个平面图,让你输出(1,1),(n ,n)的最小割.. 思路:       看完题想都没想直接最大流,结果TLE,想想也是 G<400*400,400*400*4> ...

  6. hdu3870-Catch the Theves(平面图最小割)

    Problem Description A group of thieves is approaching a museum in the country of zjsxzy,now they are ...

  7. hdu 3870(平面图最小割转最短路)

    Catch the Theves Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65768/32768 K (Java/Others) ...

  8. hdu4289 最小割最大流 (拆点最大流)

    最小割最大流定理:(参考刘汝佳p369)增广路算法结束时,令已标号结点(a[u]>0的结点)集合为S,其他结点集合为T=V-S,则(S,T)是图的s-t最小割. Problem Descript ...

  9. 最小割 总结&&做题记录

    模型要点: 1.一般适用于二取一问题或者01规划. 2.利用最小割=最大流,转化为最大流求之. 建议阅读胡伯涛的论文 <<最小割模型在信息学竞赛的应用>>,有精彩有序的证明和各 ...

随机推荐

  1. java多线程与线程并发二:线程互斥

    本文章内容整理自:张孝祥_Java多线程与并发库高级应用视频教程 当两条线程访问同一个资源时,可能会出现安全隐患.以打印字符串为例,先看下面的代码: // public class Test2 { p ...

  2. 使用火狐浏览器模仿手机浏览器,附浏览器HTTP_USER_AGENT汇总

    HTTP_USER_AGENT用来获取浏览页面的访问者在用什么操作系统(包括版本号)浏览器(包括版本号)和用户个人偏好. 改变浏览器的这个参数就可以伪装成相应的浏览器. User Agent Swit ...

  3. Class文件结构全面解析(上)

    什么是Class文件? 在Java刚刚诞生的时候就提出了一个非常著名的口号:"一次编写,到处运行.(Write Once,Run Anywhere)".为了实现平台无关性,各种不同 ...

  4. 给大家整理了几个开源免费的 Spring Boot + Vue 学习资料

    最近抽空在整理前面的文章案例啥的,顺便把手上的几个 Spring Boot + Vue 的学习资料推荐给各位小伙伴.这些案例有知识点的讲解,也有项目实战,正在做这一块的小伙伴们可以收藏下. 案例学习 ...

  5. 在C\C++中char 、short 、int各占多少个字节

    在C\C++中char .short .int各占多少个字节 : #include <bits/stdc++.h> using namespace std; int main() { co ...

  6. 领扣(LeetCode)交替位二进制数 个人题解

    给定一个正整数,检查他是否为交替位二进制数:换句话说,就是他的二进制数相邻的两个位数永不相等. 示例 1: 输入: 5 输出: True 解释: 5的二进制数是: 101 示例 2: 输入: 7 输出 ...

  7. MySQL基础知识面试与答案

    1.Mysql 的存储引擎,myisam和innodb的区别. 答: 1.MyISAM 是非事务的存储引擎,适合用于频繁查询的应用.表锁,不会出现死锁,适合小数据,小并发. 2.innodb是支持事务 ...

  8. javaScript——label语句

    第一次看见label语句是这样一个场景: function foo() {x: 1} 当时十分疑惑,为什么不报错呢?对象可以这样写? 后来知道这个是label语句,一般配合break和continue ...

  9. Navicat Premium 12连接ubuntu18 ,Mysql 5.7.27-0

    1,搭建好mysql服务器,cd  /etc/mysql/mysql.conf.d,进入mysql配置目录,vim mysqld.cnf 2,注释掉,bind-address =127.0.0.1 , ...

  10. Java File类常用方法及实例

    创建:createNewFile()在指定位置创建一个空文件,成功就返回true,如果已存在就不创建,然后返回false. createTempFile(String prefix, String s ...