BZOJ 1001 狼抓兔子 (最小割转化成最短路)

Posted dwtfukgv

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BZOJ 1001 狼抓兔子 (最小割转化成最短路)相关的知识,希望对你有一定的参考价值。

题意:中文题。

析:很容易看出是裸板的最小割,然后可能会超时,边实在是太多了,有一种特殊的方法,可以把平面图转成最短路来求,也就是利用对偶图,把原图的而看成新图的点,原图的边与两个面相连的,加一条边,然后再多加一个起点和终点。跑一次最短路即可。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
//#define all 1,n,1
#define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 2e6 + 10;
const int maxm = 100 + 10;
const ULL mod = 10007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
  return r >= 0 && r < n && c >= 0 && c < m;
}
struct Edge{
  int to, dist, next;
};

struct HeapNode{
  int d, u;
  bool operator < (const HeapNode &p) const{
    return d > p.d;
  }
};
Edge edge[maxn*3];
int head[maxn], cnt;
int d[maxn];
bool done[maxn];

inline void addEdge(int u, int v, int dist){
  edge[cnt].to = v;
  edge[cnt].dist = dist;
  edge[cnt].next = head[u];
  head[u] = cnt++;
}

void dijkstra(int s){
  priority_queue<HeapNode> pq;
  ms(d, INF);  d[s] = 0;
  ms(done, 0);
  pq.push((HeapNode){0, s});
  while(!pq.empty()){
    HeapNode x = pq.top();  pq.pop();
    int u = x.u;
    if(done[u])  continue;
    done[u] = 1;
    for(int i = head[u]; ~i; i = edge[i].next){
      int v = edge[i].to, dist = edge[i].dist;
      if(d[v] > d[u] + dist){
        d[v] = d[u] + dist;
        pq.push((HeapNode){d[v], v});
      }
    }
  }
}


int main(){
  scanf("%d %d", &n, &m);
  if(n == 1 || m == 1){
    n = max(n, m);
    int ans = INF;
    for(int i = 0; i < n; ++i){
      int x;
      scanf("%d", &x);
      ans = min(ans, x);
    }
    printf("%d\n", ans);
    return 0;
  }
  ms(head, -1);  cnt = 0;
  int s = 0, t = 2 * n * m + 1;
  FOR(i, 0, n)  for(int j = 1; j < (m-1<<1); j += 2){
    int dist;
    scanf("%d", &dist);
    int from = i == 0 ? s : (i-1)*(m-1<<1)+j+1;
    int to = i + 1 == n ? t : i*(m-1<<1)+j;
    addEdge(from, to, dist);
    addEdge(to, from, dist);
  }
  FOR(i, 0, n-1)  for(int j = 1; j <= m; ++j){
    int dist;
    scanf("%d", &dist);
    int from = j == 1 ? t : i*(m-1<<1)+(j<<1)-3;
    int to = j == m ? s : i*(m-1<<1)+(j<<1);
    addEdge(from, to, dist);
    addEdge(to, from, dist);
  }
  FOR(i, 0, n-1)  for(int j = 1; j < m; ++j){
    int dist;
    scanf("%d", &dist);
    int from = i*(m-1<<1)+(j<<1);
    int to = from - 1;
    addEdge(from, to, dist);
    addEdge(to, from, dist);
  }
  dijkstra(s);
  printf("%d\n", d[t]);
  return 0;
}

  

以上是关于BZOJ 1001 狼抓兔子 (最小割转化成最短路)的主要内容,如果未能解决你的问题,请参考以下文章

[bzoj1001][BeiJing2006]狼抓兔子-题解[平面图最小割转最短路]/[Dinic求最小割]

BZOJ1001: [BeiJing2006]狼抓兔子 (最小割转最短路)

[BJOI2006][bzoj1001] 狼抓兔子 [最小割]

BZOJ 1001:[BeiJing2006]狼抓兔子(最小割)

BZOJ 1001 狼抓兔子

BZOJ 1001 [BJOI 2006] 狼抓兔子