1. 程式人生 > >BZOJ 1001 狼抓兔子 (最小割轉化成最短路)

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

names assert urn tdi == space bool ems set

題意:中文題。

析:很容易看出是裸板的最小割,然後可能會超時,邊實在是太多了,有一種特殊的方法,可以把平面圖轉成最短路來求,也就是利用對偶圖,把原圖的而看成新圖的點,原圖的邊與兩個面相連的,加一條邊,然後再多加一個起點和終點。跑一次最短路即可。

代碼如下:

#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 狼抓兔子 (最小割轉化成最短路)