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
  • 相关阅读:
    antdesign vue 官网地址
    WMS智能仓储管理系统-干货-产品介绍
    C# 判断文件夹与文件是否存在
    C# 获取文件大小,创建时间,文件信息,FileInfo类的属性表
    问题2:采购发票、材料出库单、到货单,打开时提示某某插件是否安装! 问题
    问题一:u813.0销售出库单-EAI接口报-在对应所需名称或序数的集合中,未找到项目
    SqlServer数据库sa登录失败,错误18456
    解决<compilation debug="true" targetFramework="4.0"> 问题
    SQL Server 2005无法远程连接的解决方法
    Windows Server 2008 IIS 7.5 HTTP 错误 404.3
  • 原文地址:https://www.cnblogs.com/wust-ouyangli/p/5794452.html
Copyright © 2011-2022 走看看