1001 狼抓兔子

刚刚看到题,感觉是最小割最大流;

但是数据量很大,看看网上的题解,发现要把它变成最短路来做;

每个孔作为一个点,孔之间的边作为点之间的边;

然后利用spfa就可以了!

不过这个题runtime error了我15发;

经大师提醒才知道,在main函数里面不能够开大数组;

因为main函数中的变量保存在栈中;

代码:

#include<cstdio>
#include<cstring>
#include<queue>
#define inf 1e8
using namespace std;
const int maxn=2*1005*1005;
const int maxm=3*maxn;
struct edge
{
    int from,to,dist;
    edge(){}
    edge(int f,int t,int d):from(f),to(t),dist(d) {}
};
queue<int>q;
struct Spfa
{
    int n,m;
    edge edges[maxm];
    int next[maxm];
    int head[maxn];
    int inq[maxn];
    int d[maxn];
    void init(int n)
    {
        this->n=n;
        m=0;
        memset(head,-1,sizeof(head[0])*(n+1));
    }
    void add(int from,int to,int dis)
    {
        next[m]=head[from];
        edges[m]=edge(from,to,dis);
        head[from]=m++;
    }
    int spfa(int s,int t)
    {
        for(int i=0; i<=n; i++)
            d[i]=inf,inq[i]=0;
        inq[s]=1;
        d[s]=0;
        q.push(s);
        while(!q.empty())
        {
            int u=q.front();
            q.pop();
            inq[u]=0;
            for(int i=head[u]; i!=-1; i=next[i])
            {
                edge& e=edges[i];
                int v=e.to;
                if(d[v]>d[u]+e.dist)
                {
                    d[v]=d[u]+e.dist;
                    if(!inq[v])
                    {
                        inq[v]=1;
                        q.push(v);
                    }
                }
            }
        }
        if(d[t]==inf)return -1;
        return d[t];
    }
};
Spfa solve;
int main()
{
    int n,m,s=0,t,x;
    scanf("%d%d",&n,&m);
    t=(n-1)*(m-1)*2+1;
    solve.init(t+3);
    for(int i=1; i<=n; i++)
        for(int j=1; j<m; j++)
        {
            scanf("%d",&x);
            if(i==1)
            {
                solve.add(2*j,t,x);
                solve.add(t,2*j,x);
            }
            else if(i==n)
            {
                solve.add((n-2)*(m-1)*2+2*j-1,s,x);
                solve.add(s,(n-2)*(m-1)*2+2*j-1,x);
            }
            else
            {
                solve.add((i-2)*(m-1)*2+2*j-1,(i-1)*(m-1)*2+2*j,x);
                solve.add((i-1)*(m-1)*2+2*j,(i-2)*(m-1)*2+2*j-1,x);
            }
        }

    for (int i=1; i<=n-1; i++)
    {
        scanf("%d",&x);
        solve.add(s,(i-1)*(m-1)*2+2*1-1,x);
        solve.add((i-1)*(m-1)*2+2*1-1,s,x);
        for (int j=2; j<=m-1; j++)
        {
            scanf("%d",&x);
            solve.add((i-1)*(m-1)*2+2*j-1-1,(i-1)*(m-1)*2+2*j-1,x);
            solve.add((i-1)*(m-1)*2+2*j-1,(i-1)*(m-1)*2+2*j-1-1,x);
        }
        scanf("%d",&x);
        solve.add((i-1)*(m-1)*2+2*(m-1),t,x);
        solve.add(t,(i-1)*(m-1)*2+2*(m-1),x);
    }

    for(int i=1; i<n; i++)
    {
        for(int j=1; j<m; j++)
        {
            scanf("%d",&x);
            solve.add((i-1)*(m-1)*2+2*j-1,(i-1)*(m-1)*2+2*j,x);
            solve.add((i-1)*(m-1)*2+2*j,(i-1)*(m-1)*2+2*j-1,x);
        }
    }
    printf("%d\n",solve.spfa(s,t));
    return 0;
}

 

posted @ 2013-12-01 20:57  Yours1103  阅读(146)  评论(0编辑  收藏  举报