zoukankan      html  css  js  c++  java
  • bzoj1001 [BeiJing2006]狼抓兔子

    (color{#0066ff}{ 题目描述 })

    现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的,
    而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一个网格的地形:

    左上角点为(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只狼,
    才能完全封锁这条道路,你需要帮助狼王安排一个伏击方案,使得在将兔子一网打尽的前提下,参与的
    狼的数量要最小。因为狼还要去找喜羊羊麻烦.

    $color{#0066ff}{ 输入格式 } $

    第一行为N,M.表示网格的大小,N,M均小于等于1000.
    接下来分三部分
    第一部分共N行,每行M-1个数,表示横向道路的权值.
    第二部分共N-1行,每行M个数,表示纵向道路的权值.
    第三部分共N-1行,每行M-1个数,表示斜向道路的权值.
    输入文件保证不超过10M

    (color{#0066ff}{输出格式})

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

    (color{#0066ff}{输入样例})

    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
    

    (color{#0066ff}{输出样例})

    14
    

    (color{#0066ff}{数据范围与提示})

    none

    (color{#0066ff}{ 题解 })

    显然要求一个最小割。。。

    于是连上边跑一边最大流最小割即可

    连边发现,是双向边,直接连,不用建反向边,因为连的就是双向的

    #include<bits/stdc++.h>
    #define LL long long
    LL in() {
    	char ch; LL x = 0, f = 1;
    	while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
    	for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
    	return x * f;
    }
    const int maxn = 1001;
    struct node {
    	int to, dis;
    	node *nxt, *rev;
    	node(int to = 0, int dis = 0, node *nxt  = NULL): to(to), dis(dis), nxt(nxt) {}
    	void *operator new(size_t) {
    		static node *S = NULL, *T = NULL;
    		return (S == T) && (T = (S = new node[1024]) + 1024), S++;
    	}
    }*head[maxn * maxn], *cur[maxn * maxn];
    int dep[maxn * maxn];
    int id[maxn][maxn];
    void add(int from, int to, int dis) {
    	head[from] = new node(to, dis, head[from]);
    }
    void link(int from, int to, int dis) {
    	add(from, to, dis);
    	add(to, from, dis);
    	head[from]->rev = head[to];
    	head[to]->rev = head[from];
    }
    int s, t;
    bool bfs() {
    	for(int i = s; i <= t; i++) cur[i] = head[i], dep[i] = 0;
    	std::queue<int> q;
    	q.push(s);
    	dep[s] = 1;
    	while(!q.empty()) {
    		int tp = q.front(); q.pop();
    		for(node *i = head[tp]; i; i = i->nxt) 
    			if(!dep[i->to] && i->dis)
    				dep[i->to] = dep[tp] + 1, q.push(i->to);
    	}
    	return dep[t];
    }
    int dfs(int x, int change) {
    	if(x == t || !change) return change;
    	int flow = 0, ls;
    	for(node *i = cur[x]; i; i = i->nxt) {
    		cur[x] = i;
    		if(dep[i->to] == dep[x] + 1 && (ls = dfs(i->to, std::min(change, i->dis)))) {
    			flow += ls;
    			change -= ls;
    			i->dis -= ls;
    			i->rev->dis += ls;
    			if(!change) break;
    		}
    	}
    	if(!flow) dep[x] = 0;
    	return flow;
    }
    int n, m;
    int dinic() {
    	int flow = 0;
    	while(bfs()) flow += dfs(s, 0x7fffffff);
    	return flow;
    }
    int main() {
    	n = in(), m = in();
    	for(int i = 1; i <= n; i++)
    		for(int j = 1; j <= m; j++) 
    			id[i][j] = (i - 1) * m + j;
    	s = 1, t = id[n][m];
    	int v;
    	for(int i = 1; i <= n; i++)
    		for(int j = 1; j < m; j++) {
    			v = in();
    			link(id[i][j], id[i][j + 1], v);
    		}
    	for(int i = 1; i < n; i++)
    		for(int j = 1; j <= m; j++) {
    			v = in();
    			link(id[i][j], id[i + 1][j], v);
    		}
    	for(int i = 1; i < n; i++)
    		for(int j = 1; j < m; j++) {
    			v = in();
    			link(id[i][j], id[i + 1][j + 1], v);
    		}
    	printf("%d
    ", dinic());
    	return 0;
    }
    
  • 相关阅读:
    webug第四关:告诉你了flang是5位数
    webug第三关:你看到了什么?
    webug第二关:从图片中你能找到什么?
    webug第一关:很简单的一个注入
    redhat-DHCP服务的配置与应用
    python-基础入门-7基础
    bWAPP----SQL Injection (GET/Search)
    ctf-web-sql
    光棍节程序员闯关秀writeup
    ajax回调函数Status问题
  • 原文地址:https://www.cnblogs.com/olinr/p/10349581.html
Copyright © 2011-2022 走看看