zoukankan      html  css  js  c++  java
  • POJ 2135 Farm Tour (费用流)

    【题目链接】 http://poj.org/problem?id=2135

    【题目大意】

      有一张无向图,求从1到n然后又回来的最短路
      同一条路只能走一次

    【题解】

      题目等价于求从1到n的两条路,使得两条路的总长最短
      那么就等价于求总流量为2的费用流

    【代码】

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <utility>
    using namespace std;
    const int INF=0x3f3f3f3f;
    typedef pair<int,int> P;
    struct edge{int to,cap,cost,rev;};
    const int MAX_V=1000;
    int V,h[MAX_V],dist[MAX_V],prevv[MAX_V],preve[MAX_V];
    vector<edge> G[MAX_V]; 
    void add_edge(int from,int to,int cap,int cost){
        G[from].push_back((edge){to,cap,cost,G[to].size()});
        G[to].push_back((edge){from,0,-cost,G[from].size()-1});
    }
    int min_cost_flow(int s,int t,int f){
        int res=0;
        fill(h,h+V,0);
        while(f>0){
            priority_queue<P,vector<P>,greater<P> > que;
            fill(dist,dist+V,INF);
            dist[s]=0;
            que.push(P(0,s));
            while(!que.empty()){
                P p=que.top(); que.pop();
                int v=p.second;
                if(dist[v]<p.first)continue;
                for(int i=0;i<G[v].size();i++){
                    edge &e=G[v][i];
                    if(e.cap>0&&dist[e.to]>dist[v]+e.cost+h[v]-h[e.to]){
                        dist[e.to]=dist[v]+e.cost+h[v]-h[e.to];
                        prevv[e.to]=v;
                        preve[e.to]=i;
                        que.push(P(dist[e.to],e.to));
                    }
                }
            }
            if(dist[t]==INF)return -1;
            for(int v=0;v<V;v++)h[v]+=dist[v];
            int d=f;
            for(int v=t;v!=s;v=prevv[v]){
                d=min(d,G[prevv[v]][preve[v]].cap);
            }f-=d;
            res+=d*h[t];
            for(int v=t;v!=s;v=prevv[v]){
                edge &e=G[prevv[v]][preve[v]];
                e.cap-=d;
                G[v][e.rev].cap+=d; 
            }
        }return res;
    }
    const int MAX_M=10000;
    int N,M;
    int a[MAX_M],b[MAX_M],c[MAX_M];
    void init(){
        for(int i=0;i<M;i++)scanf("%d%d%d",&a[i],&b[i],&c[i]);
    }
    void solve(){
        int s=0,t=N-1;
        V=N;
        for(int i=0;i<N;i++)G[i].clear();
        for(int i=0;i<M;i++){
            add_edge(a[i]-1,b[i]-1,1,c[i]);
            add_edge(b[i]-1,a[i]-1,1,c[i]);
        }
        printf("%d
    ",min_cost_flow(s,t,2));
    }
    int main(){
        while(~scanf("%d%d",&N,&M)){
            init();
            solve();
        }return 0;
    }
  • 相关阅读:
    RCNN,Fast RCNN,Faster RCNN 的前生今世:(4) SSD
    RCNN,Fast RCNN,Faster RCNN 的前生今世:(0) DMP
    nachos3.4 threads管理 (c++)
    逻辑回归与多分类逻辑回归
    [LeetCode]String to Integer (atoi)
    [LeetCode]Reverse Integer
    [LeetCode]ZigZag Conversion
    [LeetCode]Longest Palindromic Substring
    [LeetCode]Median of Two Sorted Arrays
    平面点的旋转公式
  • 原文地址:https://www.cnblogs.com/forever97/p/poj2135.html
Copyright © 2011-2022 走看看