zoukankan      html  css  js  c++  java
  • hdu3870-Catch the Theves(平面图最小割)

    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=1000000000000007;
    const int maxn=2*402*402;
    int N,eid,head[maxn];
    struct edge{ int v,w,next; }E[4*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-1)+y; }
    struct node
    {
        int u;
        LL d;
        node(int u=0,LL d=0):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=0;i<maxn;i++) D[i]=INF;
        D[S]=0;
        memset(vis,false,sizeof(vis));
        que.push(node(S,0));
        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!=-1;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=0,t=(N-1)*(N-1)+1;
            memset(head,-1,sizeof(head));
            eid=0;
            int u,v,w;
            for(int i=1;i<=N;i++)
                for(int j=1;j<=N;j++)
            {
                scanf("%d",&w);
                if(i==1&&j<N)
                {
                    u=s; v=j;
                    AddEdge(u,v,w);
                }
                if(i==N&&j<N)
                {
                    u=f(i-2,j); v=t;
                    AddEdge(u,v,w);
                }
                if(1<i&&i<N&&j<N)
                {
                    u=f(i-2,j);
                    v=f(i-1,j);
                    AddEdge(u,v,w);
                }
                if(j==1&&i<N)
                {
                    u=f(i-1,j); v=t;
                    AddEdge(u,v,w);
                }
                if(j==N&&i<N)
                {
                    u=f(i-1,j-1); v=s;
                    AddEdge(u,v,w);
                }
                if(1<j&&j<N&&i<N)
                {
                    u=f(i-1,j-1);
                    v=f(i-1,j);
                    AddEdge(u,v,w);
                }
            }
            printf("%d
    ",Dij(s,t));
        }
        return 0;
    }
    View Code
  • 相关阅读:
    机器学习入门-相关性分析
    R语言-记号体系
    R语言基础
    职位画像分析(pandas/ matplotlib)
    python 可视化工具-matplotlib
    pandas-缺失值处理
    k-means实战-RFM客户价值分群
    药店商品销量分析(python)
    Jike_Time-决策树
    3.7 嵌入式SQL
  • 原文地址:https://www.cnblogs.com/wust-ouyangli/p/5794452.html
Copyright © 2011-2022 走看看