HDU 3870Catch the Theves

来源:互联网 发布:linux jdk版本降级 编辑:程序博客网 时间:2024/05/23 11:45

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3870

题意:平面图求最小割,转化成求最短路

#include<cstdio>#include<iostream>#include<cstring>#include<queue>#include<map>#include<string>using namespace std;const int N=410*410;const int M=410*410*4+10;const int INF=INT_MAX;struct Edge{    int to,next,dis;    Edge(){}    Edge(int To,int Next,int Dis)    {        to=To,next=Next ,dis=Dis;    }}e[M];int head[N];struct Spfa{    int total;    int low[N];    int vis[N];    void init()    {        total=0;        memset(head,-1,sizeof(head));    }    void add_edges(int from,int to,int dis)    {        e[total]=Edge(to,head[from],dis);        head[from]=total++;    }    int _spfa(int s,int t)    {        memset(low,-1,sizeof(low));        low[s]=0;        memset(vis,0,sizeof(vis));        vis[s]=1;        queue<int> qq;        qq.push(s);        while(!qq.empty())        {            int u=qq.front();qq.pop();            vis[u]=0;            for(int i=head[u];i!=-1;i=e[i].next)            {                int v=e[i].to;                if(low[v]==-1||(low[v]>low[u]+e[i].dis))                {                    low[v]=low[u]+e[i].dis;                    if(!vis[v])                    {                        vis[v]=1;                        qq.push(v);                    }                }            }        }        return low[t];    }}spfa;int aa[410][410];int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};int main(){    int T;scanf("%d",&T);    while(T--)    {        int n;scanf("%d",&n);        for(int i=0;i<n;i++)        {            for(int j=0;j<n;j++)            {                scanf("%d",&aa[i][j]);            }        }        int s=0,t=(n-1)*(n-1)+1;        spfa.init();        for(int i=0;i<n-1;i++)        {            for(int j=0;j<n-1;j++)            {                int ans=i*(n-1)+j+1;                for(int k=0;k<4;k++)                {                    int x=i+dir[k][0];                    int y=j+dir[k][1];                    if(x>=0&&x<n-1&&y>=0&&y<n-1)                    {                        int bns=x*(n-1)+y+1;                        if(x<i)x=i;                        if(y<j)y=j;                        spfa.add_edges(ans,bns,aa[x][y]);                        //printf("ans=%d,bns=%d,aa=%d\n",ans,bns,aa[x][y]);                    }                    else if(x<0||y>=n-1)                    {                        if(x<i)x=i;                        if(y<j)y=j;                        spfa.add_edges(s,ans,aa[x][y]);                        //printf("s=%d,ans=%d,aa=%d\n",s,ans,aa[x][y]);                    }                    else                    {                        if(x<i)x=i;                        if(y<j)y=j;                        spfa.add_edges(ans,t,aa[x][y]);                        //printf("ans=%d,t=%d,aa=%d\n",ans,t,aa[x][y]);                    }                }            }        }        printf("%d\n",spfa._spfa(s,t));    }    return 0;}



0 0