Animal Run——UVALive 3661
来源:互联网 发布:华元宠物用品淘宝真假 编辑:程序博客网 时间:2024/05/22 00:13
题目描述:
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.
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*2001using namespace std;#define INF 0x7f7f7f7fstruct 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;}
- Animal Run——UVALive 3661
- 【UVALive】3661 Animal Run 平面图最小割 最短路
- UVALive 3661 Animal Run(平面图最小割,边为节点+最短路)
- UVALive - 3661 Animal Run (平面图+最小割+对偶图+最短路)
- UVALIVE 3661 Animal Run <最大-最小原理之一 最大流-最小割原理>
- uva1376(LA3661) Animal Run
- C#基础——animal
- uva 1376 - Animal Run(最短路)
- 第十四周项目一——Animal类
- The Star Zoo——8、An animal is made
- The Star Zoo——9、An animal is destroyed
- The Call of the Wild——3、The wild animal
- Uvalive—5462 Coconuts, Revisited
- 1)编写Animal接口,接口中声明run()方法; 2)定义Bird类和Fish类实现Animal接口; 3)编写Bird类和Fish类的测试程序,并调用其中的run()方法
- )编写Animal接口,接口中声明run()方法; 2)定义Bird类和Fish类实现Animal接口; )编写Bird类和Fish类的测试程序,并调用其中的run()方法
- Animal Farm
- Animal.java
- Animal类
- 图像压缩研究
- Hbase 学习笔记(四): Hbase的架构和实现原理
- explicit关键字
- sdfbh烦得很年投放一款
- ExtJs treepanel和TabPanel配合使用
- Animal Run——UVALive 3661
- php.ini配置详解(1)
- X Window Programming/XLib
- goto语句的用法
- ul li 中判断当前点击的li是哪儿个
- Codeforces 458B Distributed Join
- printf函数精讲
- 在JAVA中线程到底起到什么作用
- 最小生成树_hdu_1863