zoukankan      html  css  js  c++  java
  • zoj 2404 最小费用流

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2404

    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <queue>
    #include <vector>
    
    #define maxn 550
    #define maxe 100000 
    #define INF  0x3f3f3f
    using namespace std;
    
    struct Edge{
        int from,to,cap,flow,cost;
        int next;
        void assign(int a,int b,int c,int d,int e,int f){
            from = a;  to = b;  cap = c; flow = d;
            cost = e;  next = f;
        }
    };
    
    struct MCMF{
        int n,cnt;
        int head[maxn];
        int d[maxn];
        Edge edges[maxe];
        int inq[maxn];
        int p[maxn];
        int res[maxn];
        
        void init(int n){
            this->n = n;
            cnt = 0;
            memset(head,-1,sizeof(head));
        }
        
        void addedge(int a,int b,int c,int d,int e){
            edges[cnt].assign(a,b,c,d,e,head[a]);
            head[a] = cnt++;
            edges[cnt].assign(b,a,0,0,-e,head[b]);
            head[b] = cnt++;
        }
        bool SPFA(int s,int t,int &flow,int& cost){
            memset(d,0x3f,sizeof(d));
            memset(inq,0,sizeof(inq));
            d[s] = 0;  inq[s] = 1; p[s] = s; res[s] = INF; res[t] = 0;
            
            queue<int> Q;
            Q.push(s);
            while(!Q.empty()){
                int u = Q.front(); Q.pop();
                inq[u] = 0;  
                
                for(int i=head[u];i!=-1;i=edges[i].next){
                    Edge& e = edges[i];
                    if(e.cap > e.flow && d[e.to] > d[e.from] + e.cost){
                        d[e.to] = d[e.from] + e.cost;
                        p[e.to] = i;  
                        res[e.to] = min(res[u],e.cap - e.flow);
                        if(!inq[e.to]){
                            Q.push(e.to);  inq[e.to] = 1;
                        }
                    }
                }
            }
            if(res[t] == 0) return false;
            flow += res[t];   
            cost += d[t]*res[t];  
            for(int i=t;i!=s;i=edges[p[i]].from){
                edges[p[i]].flow += res[t];
                edges[p[i]^1].flow -= res[t];
            } 
            return true;
        }
    }solver;
    int Mincost(int s,int t){
        int flow = 0, cost = 0;
        while(solver.SPFA(s,t,flow,cost)){}
        return cost;
    }
    
    int main()
    {
        //freopen("input.txt","r",stdin);
        int N,M;
        
        while(cin>>N>>M && N+M){  
            int ltail = 0;
            int rtail = 0;
            struct node{  
              int x, y;
            }l[maxn],r[maxn];
        
            char ch[105]; 
            for(int i=1;i<=N;i++){
                scanf("%s",ch);
                for(int j=0;j<M;j++){
                     if(ch[j] == 'H'){
                         r[rtail].x = i;  r[rtail++].y = j;
                    }
                    else if(ch[j] == 'm'){
                        l[ltail].x = i;  l[ltail++].y = j;
                    }
                   }
            }
               int n = ltail + rtail;
               solver.init(n);  
               int s = 0, t = n+1;
               for(int i=0;i<ltail;i++) solver.addedge(s,i+1,1,0,0);
               for(int i=0;i<rtail;i++) solver.addedge(i+1+ltail,t,1,0,0);
               for(int i=0;i<ltail;i++)
                  for(int j=0;j<rtail;j++){
                        int cost = (int)abs(1.0*l[i].x-1.0*r[j].x) + (int)abs(1.0*l[i].y-1.0*r[j].y);
                        solver.addedge(i+1,ltail+1+j,1,0,cost);
                  }  
               printf("%d
    ",Mincost(s,t));   
        }
    }
    View Code
  • 相关阅读:
    MQTT学习笔记——Yeelink MQTT维修 采用mqtt.js和paho-mqtt
    ubuntu 14.04设备OVS虚拟OpenFlow交换机配置汇总
    于iOS跳转到应用程序AppStore
    C++学习笔记33 转换操作符
    Win32 Windows规划 三
    [2014 Regional]牡丹江 H Hierarchical Notation 做题记录
    序言树,为了,序后,(递归)
    Visual Studio Code尝试体验
    2015第40周二Node学习
    2015第40周一Node学习
  • 原文地址:https://www.cnblogs.com/acmdeweilai/p/3236640.html
Copyright © 2011-2022 走看看