BZOJ1001 [Beijing2006] 狼抓兔子

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1001

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

输出一个整数,表示参与伏击的狼的最小数量.

 

第一次看到的时候认为是最小割,但是数据范围太大肯定TLE。

从周冬的《浅析最大最小定理在信息学竞赛中的应用》中学到建对偶图的姿势,最大流=最小割=对偶图最短路

建出对偶图然后跑Dijkstra就行了。

注意数组大小,不要RE

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <queue>
 6 #define rep(i,l,r) for(int i=l; i<=r; i++)
 7 #define clr(x,y) memset(x,y,sizeof(x))
 8 #define travel(x) for(Edge *p=last[x]; p; p=p->pre)
 9 using namespace std;
10 const int INF = 0x3f3f3f3f;
11 const int maxn = 2000010;
12 int n,m,x,y,s,t,d[maxn];
13 struct Edge{
14     Edge *pre;
15     int to,cost;
16 }edge[6000010];
17 Edge *last[maxn],*pt;
18 struct node{
19     int x,d;
20     node(int _x,int _d) : x(_x), d(_d){}
21     inline bool operator < (const node &_Tp) const {
22         return d > _Tp.d;
23     }
24 };
25 priority_queue <node> q;
26 inline int read(){
27     int ans = 0, f = 1; char c = getchar();
28     while (!isdigit(c)){
29         if (c == '-') f = -1; c = getchar();
30     }
31     while (isdigit(c)){
32         ans = ans * 10 + c - '0'; c = getchar();
33     }
34     return ans * f;
35 }
36 inline void addedge(int x,int y,int z){
37     pt->pre = last[x]; pt->to = y; pt->cost = z; last[x] = pt++;
38 }
39 inline void add(int x,int y,int z){
40     addedge(x,y,z); addedge(y,x,z);
41 }
42 int dijkstra(){
43     clr(d,INF); d[s] = 0; q.push(node(s,0));
44     while (!q.empty()){
45         node now = q.top(); q.pop();
46         if (d[now.x] != now.d) continue;
47         travel(now.x){
48             if (d[p->to] > d[now.x] + p->cost){
49                 d[p->to] = d[now.x] + p->cost;
50                 q.push(node(p->to,d[p->to]));
51             }
52         }
53     }
54     return d[t];
55 }
56 int main(){
57     n = read(); m = read(); clr(last,0); pt = edge;
58     s = 0; t = (n-1) * (m-1) * 2 + 1;
59     rep(i,0,n-1){
60         rep(j,1,m-1){
61             x = read(); y = ((i*(m-1))<<1)+(j<<1);
62             if (!i) add(j<<1,t,x);
63             else if (i == n-1) add(s,y-((m-1)<<1)-1,x);
64             else add(y,y-((m-1)<<1)-1,x);
65         }
66     }
67     rep(i,0,n-2){
68         rep(j,1,m){
69             x = read(); y = ((i*(m-1))<<1)+(j<<1);
70             if (j == 1) add(s,y-1,x);
71             else if (j == m) add(y-2,t,x);
72             else add(y-2,y-1,x);
73         }
74     }
75     rep(i,0,n-2){
76         rep(j,1,m-1){
77             x = read(); y = ((i*(m-1))<<1)+(j<<1);
78             add(y,y-1,x);
79         }
80     }
81     printf("%d\n",dijkstra());
82     return 0;
83 }
View Code

 

posted on 2015-12-11 18:31  ACMICPC  阅读(328)  评论(1编辑  收藏  举报

导航