HDU 3870Catch the Theves

题目: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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值