1001: [BeiJing2006]狼抓兔子
Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 12989 Solved: 3076
[ Submit][ Status][ Discuss]
Description
现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的,而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一个网格的地形:
左上角点为(1,1),右下角点为(N,M)(上图中N=4,M=5).有以下三种类型的道路 1:(x,y)<==>(x+1,y) 2:(x,y)<==>(x,y+1) 3:(x,y)<==>(x+1,y+1) 道路上的权值表示这条路上最多能够通过的兔子数,道路是无向的. 左上角和右下角为兔子的两个窝,开始时所有的兔子都聚集在左上角(1,1)的窝里,现在它们要跑到右下解(N,M)的窝中去,狼王开始伏击这些兔子.当然为了保险起见,如果一条道路上最多通过的兔子数为K,狼王需要安排同样数量的K只狼,才能完全封锁这条道路,你需要帮助狼王安排一个伏击方案,使得在将兔子一网打尽的前提下,参与的狼的数量要最小。因为狼还要去找喜羊羊麻烦.
Input
第一行为N,M.表示网格的大小,N,M均小于等于1000.接下来分三部分第一部分共N行,每行M-1个数,表示横向道路的权值. 第二部分共N-1行,每行M个数,表示纵向道路的权值. 第三部分共N-1行,每行M-1个数,表示斜向道路的权值. 输入文件保证不超过10M
Output
输出一个整数,表示参与伏击的狼的最小数量.
Sample Input
5 6 4
4 3 1
7 5 3
5 6 7 8
8 7 6 5
5 5 5
6 6 6
Sample Output
感觉像是一道最大流的题,但是n,m≤1000,dinic算法可能会超时。但是据说数据比较水,dinic也能过。
正确解法:在起点和终点之间连一条边之后平面图转对偶图,dijkstra求最短路径+优先队列优化。时间复杂度O(nlogn)。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<cmath>
#include<algorithm>
#define F(i,j,n) for(int i=j; i<=n; i++)
#define INF 1000000000
#define MAXN 2000001
using namespace std;
int n,m,x,t,dis[MAXN];
bool vst[MAXN];
vector<int>edge[MAXN],weight[MAXN];
struct Heapnode
{
int dist,num;
bool operator < (const Heapnode & x)const
{
return dist>x.dist;
}
};
void Solve()
{
int x=max(n,m),Min=INF,p;
F(i,1,x-1)
{
scanf("%d",&p);
Min=min(Min,p);
}
if (Min==INF) Min=0;
printf("%d\n",Min);
}
priority_queue<Heapnode> Q;
int main()
{
scanf("%d%d",&n,&m);
if (n==1||m==1)
{
Solve();
return 0;
}
t=(n-1)*(m-1)*2+1;
F(i,1,n)
F(j,1,m-1)
{
scanf("%d",&x);
if (i==1)
{
edge[t].push_back(2*j);
weight[t].push_back(x);
edge[2*j].push_back(t);
weight[2*j].push_back(x);
}
if (i==n)
{
int p=2*(n-2)*(m-1)+2*j-1;
edge[0].push_back(p);
weight[0].push_back(x);
edge[p].push_back(0);
weight[p].push_back(x);
}
if (i>1&&i<n)
{
int p=2*(m-1)*(i-1)+2*j,q=2*(m-1)*(i-2)+2*j-1;
edge[p].push_back(q);
weight[p].push_back(x);
edge[q].push_back(p);
weight[q].push_back(x);
}
}
F(i,1,n-1)
F(j,1,m)
{
scanf("%d",&x);
if (j==1)
{
int p=2*(m-1)*(i-1)+1;
edge[0].push_back(p);
weight[0].push_back(x);
edge[p].push_back(0);
weight[p].push_back(x);
}
if (j==m)
{
int p=2*(m-1)*i;
edge[t].push_back(p);
weight[t].push_back(x);
edge[p].push_back(t);
weight[p].push_back(x);
}
if (j>1&&j<m)
{
int p=2*(m-1)*(i-1)+2*(j-1);
edge[p].push_back(p+1);
weight[p].push_back(x);
edge[p+1].push_back(p);
weight[p+1].push_back(x);
}
}
F(i,1,n-1)
F(j,1,m-1)
{
scanf("%d",&x);
int p=2*(m-1)*(i-1)+2*j-1;
edge[p].push_back(p+1);
weight[p].push_back(x);
edge[p+1].push_back(p);
weight[p+1].push_back(x);
}
F(i,1,t) dis[i]=INF;
dis[0]=0;
memset(vst,false,sizeof(vst));
Q.push((Heapnode){0,0});
while (!Q.empty())
{
Heapnode x=Q.top();
Q.pop();
if (vst[x.num]) continue;
vst[x.num]=true;
for (int i=0; i<edge[x.num].size(); i++)
if (dis[edge[x.num][i]]>dis[x.num]+weight[x.num][i])
{
dis[edge[x.num][i]]=dis[x.num]+weight[x.num][i];
Q.push((Heapnode){dis[edge[x.num][i]],edge[x.num][i]});
}
}
printf("%d\n",dis[t]);
return 0;
}