zoukankan      html  css  js  c++  java
  • 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 ",SPFA());
        }
        return 0;
    }
     

  • 相关阅读:
    C#开源框架
    8 种 NoSQL 数据库系统对比
    安装补丁“此更新不适用于你的计算机”解决办法
    .net开源资料
    winform程序退出
    jquery.chained与jquery.chained.remote使用以及区别
    存储过程使用回滚
    C# Panel中绘图如何出现滚动条
    C#结构体的特点浅析
    如何用堆栈和循环结构代替递归调用--递归转换为非递归的10条军规
  • 原文地址:https://www.cnblogs.com/csushl/p/9386770.html
Copyright © 2011-2022 走看看