zoukankan      html  css  js  c++  java
  • [HNOI2013]切糕

    嘟嘟嘟


    这题我看了半天才懂,一直以为这一刀必须横平竖直的,谁知道这题这一道怎么拐都行,只要满足相邻的高度差不大于(d)就行。


    看数据范围,猜是最小割。
    先不考虑限制。对于每一个((x, y)),我们只能从(r)(f(x, y))选一个,问最小的(sum v)。如果每一个点只有两个选择,那自然往源汇点连边;现在变成了多个选择,那就把这些点穿成一串,再向源汇点连边就好啦(这个技巧得记住啊)
    现在有了限制。如果选了这个点的其中一个(f(z)),那么相邻点只能选([z - d, z + d])。也就是割了(z)那条边后,只有割([z - d, z + d])这个区间的边才能不让图连通。那我们就从((x, y, z))((x', y', z - d))((x', y', z + d))连边。这样如果想断掉((x, y, z))的话,((x', y'))只能从([z - d, z + d])选。

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<vector>
    #include<stack>
    #include<queue>
    using namespace std;
    #define enter puts("") 
    #define space putchar(' ')
    #define Mem(a, x) memset(a, x, sizeof(a))
    #define In inline
    typedef long long ll;
    typedef double db;
    const int INF = 0x3f3f3f3f;
    const db eps = 1e-8;
    const int maxn = 45;
    const int maxN = 6e5 + 5;
    const int maxe = 2e7 + 5;
    inline ll read()
    {
      ll ans = 0;
      char ch = getchar(), last = ' ';
      while(!isdigit(ch)) last = ch, ch = getchar();
      while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
      if(last == '-') ans = -ans;
      return ans;
    }
    inline void write(ll x)
    {
      if(x < 0) x = -x, putchar('-');
      if(x >= 10) write(x / 10);
      putchar(x % 10 + '0');
    }
    
    int p, q, r, d, t;
    int a[maxn][maxn][maxn], id[maxn][maxn][maxn], cnt = 0;
    struct Edge
    {
      int nxt, from, to, cap, flow;
    }e[maxe];
    int head[maxN], ecnt = -1;
    In void addEdge(int x, int y, int w)
    {
      e[++ecnt] = (Edge){head[x], x, y, w, 0};
      head[x] = ecnt;
      e[++ecnt] = (Edge){head[y], y, x, 0, 0};
      head[y] = ecnt;
      //printf("%d %d %d
    ", x, y, w);
    }
    
    int dis[maxN];
    In bool bfs()
    {
      Mem(dis, 0); dis[0] = 1;
      queue<int> q; q.push(0);
      while(!q.empty())
        {
          int now = q.front(); q.pop();
          for(int i = head[now], v; i != -1; i = e[i].nxt)
    	{
    	  if(!dis[v = e[i].to] && e[i].cap > e[i].flow)
    	    dis[v] = dis[now] + 1, q.push(v);
    	}
        }
      return dis[t];
    }
    int cur[maxN];
    In int dfs(int now, int res)
    {
      if(now == t || res == 0) return res;
      int flow = 0, f;
      for(int& i = cur[now], v; i != -1; i = e[i].nxt)
        {
          v = e[i].to;
          if(dis[v] == dis[now] + 1 && (f = dfs(v, min(res, e[i].cap - e[i].flow))) > 0)
    	{
    	  e[i].flow += f; e[i ^ 1].flow -= f;
    	  flow += f; res -= f;
    	  if(res == 0) break;
    	}
        }
      return flow;
    }
    In int minCut()
    {
      int flow = 0;
      while(bfs())
        {
          memcpy(cur, head, sizeof(head));
          flow += dfs(0, INF);
        }
      return flow;
    }
    
    const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
    In void build(int x, int y)
    {
      addEdge(0, id[x][y][1], INF);
      addEdge(id[x][y][r + 1], t, INF);
      for(int k = 1; k <= r; ++k)
        {
          addEdge(id[x][y][k], id[x][y][k + 1], a[x][y][k]);
          for(int i = 0 ; i < 4; ++i)
    	{
    	  int nx = x + dx[i], ny = y + dy[i];
    	  if(!nx || nx > p || !ny || ny > q) continue;
    	  addEdge(id[x][y][k], id[nx][ny][max(1, k - d)], INF);
    	  addEdge(id[nx][ny][min(r + 1, k + d + 1)], id[x][y][k + 1], INF);
    	}
        }
    }
    
    int main()
    {
      //freopen("ac.out", "w", stdout);
      Mem(head, -1);
      p = read(), q = read(), r = read(), d = read();
      for(int k = 1; k <= r; ++k)
        for(int i = 1; i <= p; ++i)
          for(int j = 1; j <= q; ++j) a[i][j][k] = read();
      for(int i = 1; i <= p; ++i)
        for(int j = 1; j <= q; ++j)
          for(int k = 1; k <= r + 1; ++k) id[i][j][k] = ++cnt;
      t = ++cnt;
      for(int i = 1; i <= p; ++i)
        for(int j = 1; j <= q; ++j) build(i, j);
      write(minCut()), enter;
      return 0;
    }
    
  • 相关阅读:
    [stm32] Systick
    [stm32] GPIO及最小框架
    51单片机-PC数据传输 温度 距离 监控系统设计
    [游戏学习29] Win32 图像处理1
    [51单片机] 串口通讯 简单通信
    [汇编] 闰年计算
    Java常用工具类之ArrayUtil
    常用工具类系列之DateUtil
    SpringBoot 获取当前登录用户IP
    Spring data jpa Specification查询关于日期的范围搜索
  • 原文地址:https://www.cnblogs.com/mrclr/p/10804661.html
Copyright © 2011-2022 走看看