zoukankan      html  css  js  c++  java
  • UVaLive 3353 Optimal Bus Route Design (最小费用流)

    题意:给定一个 n 个点的有向带权图,让你找若干个圈,使得每个结点恰好属于一个圈,并且总长度尽量小。

    析:一开始想的是先缩点,先用DP,来求。。。

    题解给的是最小费用流或者是最佳完全匹配,其实都是一样的,因为每个点都只属于一个圈,那么对于每个点的入度和出度都应该是一样的,然后就是把每个点都拆成两个点,然后如果有边相连,就加一条费用该权值,容量为1的边,然后跑一个最小费用流即可,如果满流就是有解,否则就是无解。如果用最佳完全匹配的话,也差不多,每条都有一个后继边,连一条边,然后不存在的用无限大,因为求是最小值,所以取反每个权值,求最佳完全匹配即可。

    代码如下:

    #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>
    #include <numeric>
    #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 LL LNF = 1e17;
    const double inf = 1e20;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 200 + 10;
    const int maxm = 3e5 + 10;
    const ULL mod = 3;
    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 from, to, cap, flow, cost;
    };
    
    struct MinCostMaxFlow{
      int n, m, s, t;
      vector<Edge> edges;
      vector<int> G[maxn];
      int d[maxn];
      int p[maxn];
      int a[maxn];
      bool inq[maxn];
    
      void init(int n){
        this-> n = n;
        for(int i = 0; i < n; ++i)  G[i].cl;
        edges.cl;
      }
    
      void addEdge(int from, int to, int cap, int cost){
        edges.pb((Edge){from, to, cap, 0, cost});
        edges.pb((Edge){to, from, 0, 0, -cost});
        m = edges.sz;
        G[from].pb(m - 2);
        G[to].pb(m - 1);
      }
    
      bool bellman(int &flow, int &cost){
        ms(d, INF);  d[s] = 0;  ms(inq, 0);  inq[s] = 1;
        p[s] = 0;  a[s] = INF;
        queue<int> q;  q.push(s);
        while(!q.empty()){
          int u = q.front();  q.pop();
          inq[u] = 0;
          for(int i = 0; i < G[u].sz; ++i){
            Edge &e = edges[G[u][i]];
            if(e.cap > e.flow && d[e.to] > d[u] + e.cost){
              p[e.to] = G[u][i];
              d[e.to] = d[u] + e.cost;
              a[e.to] = min(a[u], e.cap - e.flow);
              if(!inq[e.to]){ q.push(e.to);  inq[e.to] = 1; }
            }
          }
        }
        if(d[t] == INF)  return false;
        flow += a[t];
        cost += a[t] * d[t];
        int u = t;
        while(u != s){
          edges[p[u]].flow += a[t];
          edges[p[u]^1].flow -= a[t];
          u = edges[p[u]].from;
        }
        return true;
      }
    
      int mincostmaxflow(int s, int t, int &flow){
        this-> s = s;
        this-> t = t;
        int cost = 0;
        while(bellman(flow, cost));
        return cost;
      }
    };
    MinCostMaxFlow mcmf;
    
    int main(){
      while(scanf("%d", &n) == 1 && n){
        int s = 0, t = n * 2 + 2;
        mcmf.init(t + 2);
        for(int i = 1; i <= n; ++i){
          int u, cost;
          mcmf.addEdge(s, i<<1, 1, 0);
          mcmf.addEdge(i<<1|1, t, 1, 0);
          while(scanf("%d", &u) == 1 && u){
            scanf("%d", &cost);
            mcmf.addEdge(i<<1, u<<1|1, 1, cost);
          }
        }
        int flow = 0;
        int ans = mcmf.mincostmaxflow(s, t, flow);
        if(flow != n)  puts("N");
        else printf("%d
    ", ans);
      }
      return 0;
    }
    

      

  • 相关阅读:
    Pivotal tc Server
    深入剖析jsonp跨域原理
    IE11 el-menu鼠标滑过报错:Error in v-on handler: "TypeError: 对象不支持此操作"
    细嚼JS闭包知识点及案例分析
    关于解决Chrome新版本中cookie跨域携带和samesite的问题处理 ——Ngnix篇
    解决vue本地环境跨域请求正常,版本打包后跨域代理不起作用,请求不到数据的方法——针对vue2.0
    element-ui 的 el-table 上使用无限滚动加载(与自带的 infinite-scroll 结合)——vue2.0
    vue+element-ui table实现滚动加载 ——vue2.0版本
    websocket(一)封装使用
    JS和jQuery将类数组对象转化成数组对象的几种方法
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7818663.html
Copyright © 2011-2022 走看看