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;
    }
     

  • 相关阅读:
    【转载】 卷积神经网络(Convolutional Neural Network,CNN)
    【转载】 深度学习之卷积神经网络(CNN)详解与代码实现(一)
    【边缘计算】 Edge Computing: Vision and Challenges
    【转载】 一种替代性的基于模拟的搜索方法,即策略梯度搜索
    【转载】 共享相关任务表征,一文读懂深度神经网络多任务学习
    【转载】 另一种(深度)学习:自我监督学习会是下一个重点导向吗?
    【转载】 谷歌做了个机器人,扔东西比人准多了 | 极客酷玩
    【节选 转载】 当前的迁移学习算法进行分类
    【转载】 第四范式首席科学家杨强:AlphaGo的弱点及迁移学习的应对(附视频)
    Flash打开新窗口 被浏览器拦截问题 navigateToURL被拦截 真正试验结果
  • 原文地址:https://www.cnblogs.com/csushl/p/9386770.html
Copyright © 2011-2022 走看看