• [SHOI2010]最小生成树


    嘟嘟嘟


    这道题的切入点在于模型的转化。


    令第(Lab)条边连接的两个点分别为(x, y)。根据kruskal算法,我们排完序加边的时候,在执行第(Lab)条边之前,都要保证(x, y)不连通。这就很像最小割了。
    所以我们把边权小于(w_{Lab})的都拿来建图,那边权是啥咧?题中说的除了一条边其余的都减1,那不就相当于这条边自己加1了嘛。那么为了让图不连通,只需让每一条边的容量为(w_{lab} - w_i + 1)即可。

    #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 = 505;
    const int maxm = 805;
    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 n, m, t, id;
    struct Node
    {
      int x, y, w;
    }q[maxm];
    struct Edge
    {
      int nxt, from, to, cap, flow;
    }e[maxm << 2];
    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;
    }
    
    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;
    }
    
    int main()
    {
      Mem(head, -1);
      n = read(), m = read(), id = read(); t = n + 1;
      for(int i = 1; i <= m; ++i) q[i].x = read(), q[i].y = read(), q[i].w = read();
      for(int i = 1; i <= m; ++i)
        if(q[i].w <= q[id].w && i != id)
          {
    	addEdge(q[i].x, q[i].y, q[id].w - q[i].w + 1);
    	addEdge(q[i].y, q[i].x, q[id].w - q[i].w + 1);
          }
      addEdge(0, q[id].x, INF), addEdge(q[id].y, t, INF);
      write(minCut()), enter;
      return 0;
    }
    
  • 相关阅读:
    php l练习(写着玩)
    位、字节、字
    ueditor上传图片时目录创建失败的问题解决方法
    tp5插入百度富文本编辑器UEditor
    PHP实现用户异地登录提醒功能的方法
    tp5知识点
    TP5语法
    微擎url
    微擎多图片上传
    微擎函数Iserializer和Iunserializer序列化函数
  • 原文地址:https://www.cnblogs.com/mrclr/p/10805401.html
Copyright © 2020-2023  润新知