BZOJ 1001 构造对偶图 + dijkstra_heap

来源:互联网 发布:淘宝好的第三方活动 编辑:程序博客网 时间:2024/06/05 08:05

1001: [BeiJing2006]狼抓兔子
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
3 4
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
14
HINT
2015.4.16新加数据一组,可能会卡掉从前可以过的程序。
Source

从start到end 在一条路上可以放该边权值只狼拦截 求最需要最少的狼
最初想到的应该是最小割,那么求最大流数据太大 显然会T掉…
或许可以这么想 从拦截的角度思考 不论如何 拉一条曲线 从要把与其相交的所有边都拦截 那么要求的就转化成了求这相交的边中和最小 那么可以构造成这么一幅图
这里写图片描述//这幅图是偷来的

图引用自(http://www.th7.cn/Program/cp/201412/326834.shtml)万分感谢

就转化成了 从节点s到节点t的最短路 最后 存好图之后 一边 dijkstra_heap 或 spfa 就好了

最后附代码

#include <cstdio>#include <algorithm>#include <cstring>#include <queue>#include <cstdlib>#define N 2000100#define M 6000100#define INF 1000000000using namespace std;struct edge {    int to, w, next;}e[M];struct node {    int now, dist;    node(void) {}    node(int x, int y):now(x), dist(y){}    bool operator < (const node x) const {        return dist > x.dist;    }};int n, m, num, sum, p[N], d[N], flag[N];void read(int &x) {    x = 0; char c = getchar();    for(; c < '0' || c > '9'; c = getchar());    for(; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = getchar());}void add(int x, int y, int z) {    e[++num].to = y; e[num].w = z;    e[num].next = p[x]; p[x] = num;}void dij_heap(void) {    priority_queue<node>q;    for (int i = 1; i <= sum; ++i)    d[i] = INF;    d[sum+1] = INF;    q.push(node(0, 0));    while(!q.empty()) {        int x = q.top().now;        q.pop();        if (flag[x]) continue;        for (int i = p[x]; i; i = e[i].next) {            int k = e[i].to;            if (d[k] > d[x] + e[i].w) {                d[k] = d[x] + e[i].w;                q.push(node(k, d[k]));            }        }    }}int main() {    int x, y, z, l, r, po;    read(n), read(m);    if (n == 1 || m == 1) {        if (n > m) swap(n, m);        int ans = INF;        for (int i = 1; i < m; i++) {            read(x); if (x < ans) ans = x;        }        printf("%d\n", ans);        return 0;    }    l = n - 1, r = m - 1;    sum = 2 * l * r;    for (int i = 1; i <= n; i++)        for (int j = 1; j < m; j++) {            read(z);            po = (i-1)*r + j;            y = po << 1;            x = y - 2*r - 1;            if (i == 1) x = 0;            else if (i == n) y = sum + 1;            add(x, y, z);            add(y, x, z);        }    for (int i = 1; i < n; i++)        for (int j = 1; j <= m; j++) {            read(z);            po = (i-1)*r + j - 1;            x = po << 1;            y = x | 1;            if (j == 1) x = sum + 1;            else if (j == m) y = 0;            add(x, y, z);            add(y, x, z);        }    for (int i = 1; i < n; i++)        for (int j = 1; j < m; j++) {            read(z);            po = (i-1)*r + j;            y = po << 1;            x = y - 1;            add(x, y, z);            add(y, x, z);        }    dij_heap();    printf("%d\n", d[sum+1]);    return 0;}
1 0
原创粉丝点击