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

USACO 2003 February Green

题意:农夫想从 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\n",a);
    }
}
时间: 2024-10-07 11:04:13

poj 2135(最小费用最大流)的相关文章

poj 2135 最小费用最大流初步

这个题的意思是农夫约翰要呆人参观他的农场, 刚开始从1开始走, 走到N后又返回1点, 两次不能走相同的路, 问农夫约翰走的最短的路是多少?? 我们可以用最小MCMF来解决这个问题, 对于图中的每一条边, 我们建立了两条流量为1, 费用为边权的边, 再增加一个源点和一个汇点, 源点指向1, 流量为2,费用为0, N指向汇点, 流量为2费用为0, 然后求出最小费用即可, 代码如下: #include <cstdio> #include <algorithm> #include <

poj 2195 最小费用最大流模板

/*Source Code Problem: 2195 User: HEU_daoguang Memory: 1172K Time: 94MS Language: G++ Result: Accepted Source Code */ #include <iostream> #include <stdio.h> #include <queue> #include <math.h> #include <string.h> using namespa

poj 2516 最小费用最大流

Language: Default Minimum Cost Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 14334   Accepted: 4908 Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area there are N shopkeepers (m

POJ - 2195 最小费用最大流

题意:每个人到每个房子一一对应,费用为曼哈顿距离,求最小的费用 题解:单源点汇点最小费用最大流,每个人和房子对于建边 #include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstdio> #include<cassert> #include<iomanip&

POJ 3680 最小费用最大流

这题来源:<算法竞赛经典入门-训练指南>中的367页:区间k覆盖问题. 思路:这题建图比较机智,我刚开始想到能建的图也就是离散化后两个端点连边,流量为1,费用为负的权值(因为求的是最大费用最大流),然后再加上源点和汇点,也就如此而已:但是这样建图样例第二和第四个不正确,因为中间没有联系的没连边,然后k就没用了. 原来最重要的连边是i和i+1之间的连边,流量为k,费用为0:为什么要连这些边呢,刚开始我也没想明白,后面才知道,因为有的端点之间你要让它们产生联系并且受制与k次,那么就得把这些点都连边

POJ 3422 最小费用最大流

链接: http://poj.org/problem?id=3422 题解: 关键是如何处理"只能获取一次"的问题,为此可以为每个点创建伪点,由两条有向边相连.原始点到伪点连一条容量为1,权值为负分数的边:原始点到伪点连一条容量为无穷,权值为0的边.前者表示分数只能拿一次,后者表示第二次第三次--可以继续走这个点,但是不拿分数.负权是因为题目要求的是"最大费用".又因为最多走同一个点K次,所以此处的无穷大取K就行了. 代码: 1 #include <map&g

poj 2135 Farm Tour (最小费用最大流模板)

网络流的费用: 在实际应用中,与网络流有关的问题,不仅涉及流量,而且还有费用的因素.网络的每一条边(v,w)除了给定容量cap(v,w)外,还定义了一个单位流量费用cost(v,w) 最小费用最大流问题 给定网络G,要求G的一个最大用流flow,使流的总费用最小. 求解MCMF问题的算法: 最小费用最大流最常用和基本的算法我们可以称它为最小费用路算法,其思想与求最大流的增广路算法类似,不断在残流网络中寻找从源s到汇t的最小费用路,即残流网络中从s到t的以费用为权的最短路,然后沿最小费用路增流,直

网络流(最小费用最大流):POJ 2135 Farm Tour

Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 2135 64-bit integer IO format: %lld      Java class name: Main When FJ's friends visit him on the farm, he likes to show them around. His farm compris

POJ 3686.The Windy&#39;s 最小费用最大流

The Windy's Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5477   Accepted: 2285 Description The Windy's is a world famous toy factory that owns M top-class workshop to make toys. This year the manager receives N orders for toys. The ma