Animal Run——UVALive 3661

Animal Run(最短路)

题目描述:

Animals are living their painful lives in the zoo. Their activities are limited in a small area without any fun of snacks, alcohol, love or games. They are so upset that they decide to escape in a night.

As shown in Figure 1, the paths in the zoo can be expressed by a grid with n x m nodes. All the paths in the grid are two-way, horizontal or vertical or diagonal. Animals start from the upper left corner, and they are free if they can reach the lower right through paths.



Figure 1: This is a  x 4 nodes grid, the number beside the path indicating how many staff shall be sent to block this path

To protect public safety, the police are sent to block some paths to catch all the escaping animals. As it needs certain police staff to block a path, you are required to write a program for the police officer, and tell him how many staff at least shall be sent in order to defeat this Animal Escape.


题目链接


思路:

如果按照题目所给的条件直接建立最小割模型。是可以得到正确答案的。

但是由于点数过多。有1,000,000个点。网络流做法是一定会超时的。所以一定要选择一个更优的算法。

由于这个图是平面图,那么它的最小割一定要连接左/下,右/上的边,于是这道题变为了最短路。


具体建图:

以每条边作为点,点权为该边的边权。那么所有和该点相连的点其实就是原图中相连的边。

答案即为从 左/下 到 右/上 的最短路。

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cctype>
#include <queue>
#include <map>
#include <algorithm>
#define LL long long
#define maxm 2001*2001*12+2002
#define maxn 2001
#define maxp 2001*2001
using namespace std;
#define INF 0x7f7f7f7f
struct Node
{
    int id,dis;
    bool operator < (const Node b ) const
    {
        return dis > b.dis;
    }
};
struct Edge
{
    int from,to,next;
    int len;
}es[maxm];
int cnt,n,m;
int p[maxp],inq[maxp],in[maxp];
int dis[maxp];
void add(int x,int y,int len)
{
    es[cnt].from = x;
    es[cnt].to = y;
    es[cnt].len = len;
    es[cnt].next = p[x];
    p[x] = cnt++;
}
void init()
{
    memset(p,-1,sizeof p);
    cnt= 0 ;
}
void dij(int s)
{
    priority_queue<Node> q;
    memset(inq,0,sizeof inq);
    memset(dis,0x7f,sizeof dis);
    Node x;
    x = (Node){s,0};
    dis[s] = 0;
    q.push(x);
    while(!q.empty())
    {
        x = q.top();
        q.pop();
        int u = x.id;
        if(inq[u]) continue;
        inq[u] = true;

        for(int i = p[u];i+1;i = es[i].next)
        {
            int v = es[i].to;
            if(!inq[v] && dis[v] > dis[u] + es[i].len)
            {
                dis[v] = dis[u] + es[i].len;

                Node tmp = {v,dis[v]};
                q.push(tmp);
            }
        }
    }
}

int a[maxn][maxn],b[maxn][maxn],c[maxn][maxn];

int ida(int x,int y)
{
    return (x-1)*m+y;
}

int idb(int x,int y)
{
    return (x-1)*m+y+n*m;
}

int idc(int x,int y)
{
    return (x-1)*m+y+2*m*n;
}

int main()
{
    int ks = 1;
    while(scanf("%d %d",&n,&m) ,n||m)
    {
        init();

        for(int i = 1;i <= n;i++)
            for(int j = 1;j < m;j++)
                scanf("%d",&a[i][j]);
        for(int i = 1;i < n;i++)
            for(int j = 1;j <= m;j++)
                scanf("%d",&b[i][j]);
        for(int i = 1;i < n;i++)
            for(int j = 1;j < m;j++)
                scanf("%d",&c[i][j]);

        int s = 0,t = 3*m*n+1;

        for(int i = 1;i < n;i++)
            for(int j = 1;j < m ;j++)
            {
                /*下三角*/
                add(ida(i+1,j),idc(i,j),a[i+1][j]);
                add(idc(i,j),ida(i+1,j),c[i][j]);

                add(ida(i+1,j),idb(i,j),a[i+1][j]);
                add(idb(i,j),ida(i+1,j),b[i][j]);

                add(idb(i,j),idc(i,j),b[i][j]);
                add(idc(i,j),idb(i,j),c[i][j]);
                /*上三角*/
                add(ida(i,j),idc(i,j),a[i][j]);
                add(idc(i,j),ida(i,j),c[i][j]);

                add(ida(i,j),idb(i,j+1),a[i][j]);
                add(idb(i,j+1),ida(i,j),b[i][j+1]);

                add(idc(i,j),idb(i,j+1),c[i][j]);
                add(idb(i,j+1),idc(i,j),b[i][j+1]);
            }

        for(int i = 1;i < m;i++)
        {
            add(s,ida(n,i),0);
            add(ida(1,i),t,a[1][i]);
        }

        for(int i = 1;i < n;i++)
        {
            add(s,idb(i,1),0);
            add(idb(i,m),t,b[i][m]);
        }
        dij(s);
        int ans = INF;

        printf("Case %d: Minimum = %d\n",ks++,dis[t]);

    }

    return 0;
}











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值