• POJ2135 Farm Tour


    嘟嘟嘟


    费用流入门题。
    其实我也不知道为啥是费用流,不过因为学费用流的时候推这题了我才能想到。


    因为每一条路只能走一次,所以容量设为1,路径长度作为费用。
    然后从源点向1号节点连一条容量为2,费用为0的边;从(n)号节点向汇点连一条容量为2,费用为0的边。
    跑最小费用流即可。

    #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 rg register
    typedef long long ll;
    typedef double db;
    const int INF = 0x3f3f3f3f;
    const db eps = 1e-8;
    const int maxn = 1e3 + 5;
    const int maxm = 1e4 + 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 n, m, s, t;
    struct Edge
    {
      int nxt, from, to, cap, c;
    }e[maxm << 2];
    int head[maxn], ecnt = -1;
    void addEdge(int x, int y, int w, int f)
    {
      e[++ecnt] = (Edge){head[x], x, y, w, f};
      head[x] = ecnt;
      e[++ecnt] = (Edge){head[y], y, x, 0, -f};
      head[y] = ecnt;
    }
    
    queue<int> q;
    int dis[maxn], flow[maxn], pre[maxn];
    bool in[maxn];
    bool spfa(int s, int t)
    {
      Mem(dis, 0x3f); Mem(in, 0);
      dis[s] = 0; in[s] = 1; flow[s] = INF;
      q.push(s);
      while(!q.empty())
        {
          int now = q.front(); q.pop(); in[now] = 0;
          for(int i = head[now], v; i != -1; i = e[i].nxt)
    	{
    	  v = e[i].to;
    	  if(e[i].cap > 0 && dis[now] + e[i].c < dis[v])
    	    {
    	      dis[v] = dis[now] + e[i].c;
    	      flow[v] = min(flow[now], e[i].cap);
    	      pre[v] = i;
    	      if(!in[v]) in[v] = 1, q.push(v);
    	    }
    	}
        }
      return dis[t] != INF;
    }
    ll maxFlow = 0, minCost = 0;
    void update(int s, int t)
    {
      int x = t;
      while(x != s)
        {
          int i = pre[x];
          e[i].cap -= flow[t];
          e[i ^ 1].cap += flow[t];
          x = e[i].from;
        }
      maxFlow += flow[t];
      minCost += (ll)flow[t] * dis[t];
    }
    
    void MCMF(int s, int t)
    {
      while(spfa(s, t)) update(s, t);
    }
    
    int main()
    {
      Mem(head, -1);
      n = read(); m = read(); s = 0; t = n + 1;
      for(int i = 1; i <= m; ++i)
        {
          int x = read(), y = read(), f = read();
          addEdge(x, y, 1, f); addEdge(y, x, 1, f);
        }
      addEdge(s, 1, 2, 0); addEdge(n, t, 2, 0);
      MCMF(s, t);
      write(minCost), enter;
      return 0;
    }
    
  • 相关阅读:
    【js】replace()
    【js】indexOf()
    【js】sort()
    【js】typeof与instanceof
    【js】with 语句
    跳出框架iframe的操作语句
    Mongodb启动命令mongod参数说明
    ERROR: child process failed, exited with error number 100
    SELECT控件add方法 ie 类型不匹配
    Red hat linux ping: unknown host www.baidu.com
  • 原文地址:https://www.cnblogs.com/mrclr/p/10009612.html
Copyright © 2020-2023  润新知