zoukankan      html  css  js  c++  java
  • poj 2135(最小费用最大流)

    Farm Tour
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 14730   Accepted: 5614

    Description

    When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000) fields numbered 1..N, the first of which contains his house and the Nth of which contains the big barn. A total M (1 <= M <= 10000) paths that connect the fields in various ways. Each path connects two different fields and has a nonzero length smaller than 35,000.

    To show off his farm in the best way, he walks a tour that starts at his house, potentially travels through some fields, and ends at the barn. Later, he returns (potentially through some fields) back to his house again.

    He wants his tour to be as short as possible, however he doesn't want to walk on any given path more than once. Calculate the shortest tour possible. FJ is sure that some tour exists for any given farm.

    Input

    * Line 1: Two space-separated integers: N and M.

    * Lines 2..M+1: Three space-separated integers that define a path: The starting field, the end field, and the path's length.

    Output

    A single line containing the length of the shortest tour.

    Sample Input

    4 5
    1 2 1
    2 3 1
    3 4 1
    1 3 2
    2 4 2

    Sample Output

    6
    

    Source

     
    题意:农夫想从 1 走到 n,然后又从 n 走到 1,要求两次所走的路径不一样并且和最小,问最小路径和?
    题解:从题目中我们可以知道这个题的最大流为2,所以添加容量为2的源点和汇点进行限流,然后再将边的权值变成费用,跑一遍最小费用最大流即可。
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <algorithm>
    using namespace std;
    const int INF = 999999999;
    const int N = 2005;
    const int M = 80005;
    struct Edge{
        int u,v,cap,cost,next;
    }edge[M];
    int head[N],tot,low[N],pre[N];
    int total ;
    bool vis[N];
    void addEdge(int u,int v,int cap,int cost,int &k){
        edge[k].u=u,edge[k].v=v,edge[k].cap = cap,edge[k].cost = cost,edge[k].next = head[u],head[u] = k++;
        edge[k].u=v,edge[k].v=u,edge[k].cap = 0,edge[k].cost = -cost,edge[k].next = head[v],head[v] = k++;
    }
    void init(){
        memset(head,-1,sizeof(head));
        tot = 0;
    }
    bool spfa(int s,int t,int n){
        memset(vis,false,sizeof(vis));
        for(int i=0;i<=n;i++){
            low[i] = (i==s)?0:INF;
            pre[i] = -1;
        }
        queue<int> q;
        q.push(s);
        while(!q.empty()){
            int u = q.front();
            q.pop();
            vis[u] = false;
            for(int k=head[u];k!=-1;k=edge[k].next){
                int v = edge[k].v;
                if(edge[k].cap>0&&low[v]>low[u]+edge[k].cost){
                    low[v] = low[u] + edge[k].cost;
                    pre[v] = k; ///v为终点对应的边
                    if(!vis[v]){
                        vis[v] = true;
                        q.push(v);
                    }
                }
            }
        }
        if(pre[t]==-1) return false;
        return true;
    }
    int MCMF(int s,int t,int n){
        int mincost = 0,minflow,flow=0;
         while(spfa(s,t,n))
        {
            minflow=INF+1;
            for(int i=pre[t];i!=-1;i=pre[edge[i].u])
                minflow=min(minflow,edge[i].cap);
            flow+=minflow;
            for(int i=pre[t];i!=-1;i=pre[edge[i].u])
            {
                edge[i].cap-=minflow;
                edge[i^1].cap+=minflow;
            }
            mincost+=low[t]*minflow;
        }
        total=flow;
        return mincost;
    }
    int n,m;
    
    int main(){
        while(scanf("%d%d",&n,&m)!=EOF){
            init();
            int src = 0,des = n+1;
            for(int i=1;i<=m;i++){
                int u,v,w;
                scanf("%d%d%d",&u,&v,&w);
                if(u==v) continue;
                addEdge(u,v,1,w,tot);
                addEdge(v,u,1,w,tot);
            }
            addEdge(src,1,2,0,tot);
            addEdge(n,des,2,0,tot);
            int a = MCMF(src,des,n+2);
            printf("%d
    ",a);
        }
    }
  • 相关阅读:
    css实现图像边框的样式
    css3 实现div靠右对齐
    将div水平,垂直居中的方式
    使用vue-cli可视化的方式创建项目后如何关闭ESLint代码检测
    清楚html和css标签自带默认样式
    vue动态请求到的多重数组循环遍历,取值问题,如果某个值存在则显示,不存在则不显示。
    python 编程
    python 错题集
    python+selenium页面自动化 元素定位实际遇到的各种问题(持续更新)
    使用Fiddle抓取IOS手机
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5728135.html
Copyright © 2011-2022 走看看