POJ 2135.Farm Tour 最小费用流

Farm Tour

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17307   Accepted: 6687

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

题目链接:http://poj.org/problem?id=2135题意:有n块地,m条道路,问从1到n在返回1的最短距离,并且同一条道路不能经过2次。思路:感觉像是先求出1-n的最短路,然后删除路径再次求最短路,但是这种方法,明显是错误的。将问题当作1-n的两条没有公共边的路径,将路径本身作为一条流量为1的路径,花费为路径长度,这样就不过是求流量为2的最小费用流
代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define PI acos(-1.0)
const int maxn=3e5+100,maxm=1e5+100,inf=0x3f3f3f3f,mod=1e9+7;
const ll INF=1e18+7;
struct edge
{
    int from,to;
    int cap,cost;
    int rev;
};
int n;
vector<edge>G[maxn];
int h[maxn];
///顶点的势,取h(u)=(s到u的最短距离),边e=(u,v)的长度变成d`(e)=d(e)+h(u)-h(v)>=0
int dist[maxn];
int prevv[maxn],preve[maxn];///前驱结点和对应的边
void addedge(int u,int v,int cap,int cost)
{
    edge e;
    e.from=u,e.to=v,e.cap=cap,e.cost=cost,e.rev=G[v].size();
    G[u].push_back(e);
    e.from=v,e.to=u,e.cap=0,e.cost=-cost,e.rev=G[u].size()-1;
    G[v].push_back(e);
}
int min_cost_flow(int s,int t,int f)
{
    int res=0;
    fill(h+1,h+n+1,0);
    while(f>0)
    {
        priority_queue<P,vector<P>,greater<P> >q;
        fill(dist+1,dist+n+1,inf);
        dist[s]=0;
        q.push(P(dist[s],s));
        while(!q.empty())
        {
            P p=q.top();
            q.pop();
            int u=p.second;
            if(dist[u]<p.first) continue;
            for(int i=0; i<G[u].size(); i++)
            {
                edge e=G[u][i];
                if(e.cap>0&&dist[e.to]>dist[u]+e.cost+h[u]-h[e.to])
                {
                    dist[e.to]=dist[u]+e.cost+h[u]-h[e.to];
                    prevv[e.to]=u;
                    preve[e.to]=i;
                    q.push(P(dist[e.to],e.to));
                }
            }
        }
        if(dist[t]==inf) return -1;
        for(int i=1; i<=n; i++) h[i]+=dist[i];
        int d=f;
        for(int i=t; i!=s; i=prevv[i])
            d=min(d,G[prevv[i]][preve[i]].cap);
        f-=d;
        res+=d*h[t];
        for(int i=t; i!=s; i=prevv[i])
        {
            edge &e=G[prevv[i]][preve[i]];
            e.cap-=d;
            G[i][e.rev].cap+=d;
        }
    }
    return res;
}
int main()
{
    int m;
    scanf("%d%d",&n,&m);
    for(int i=1; i<=m; i++)
    {
        int u,v,cost;
        scanf("%d%d%d",&u,&v,&cost);
        addedge(u,v,1,cost);
        addedge(v,u,1,cost);
    }
    int ans=min_cost_flow(1,n,2);
    cout<<ans<<endl;
    return 0;
}

最小费用流

				
时间: 2024-08-23 19:26:28

POJ 2135.Farm Tour 最小费用流的相关文章

poj 2135 Farm Tour 最小费用流入门模板

题意: 求点1到点n再从点n回点1不经过同一条路的最短路. 分析: 建图容易,给一组针对求两次最短路的数据: 4 5 1 2 1 1 3 100 2 4 100 2 3 1 3 4 1 接下来上最小费用流的模板就好. 代码: //poj 2135 //sep9 #include <iostream> #include <queue> using namespace std; const int maxN=2048; const int maxM=20024; struct Edge

【网络流#9】POJ 2135 Farm Tour 最小费用流 - 《挑战程序设计竞赛》例题

[题意]给出一张无向图,从1开始到n,求两条没有公共边的最短路,使得路程总和最小 每条边的权值设为费用,最大流量设为1,然后就是从源点到汇点流量为2的最小费用流. 因为是规定了流量,新建一个源点和一个汇点,源点到结点1连一条最大流量为2,费用为0的边,结点N到汇点连一条最大流量为2,费用为0的边,这样就规定好流量了. #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #

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

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

POJ 2135 Farm Tour &amp;&amp; HDU 2686 Matrix &amp;&amp; HDU 3376 Matrix Again 费用流求来回最短路

累了就要写题解,最近总是被虐到没脾气. 来回最短路问题貌似也可以用DP来搞,不过拿费用流还是很方便的. 可以转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1,然后连一条流量为1(花费根据题意来定) 的边来控制每个点只能通过一次. 额外添加source和sink来控制满流为2. 代码都雷同,以HDU3376为例. #include <algorithm> #include <iostream> #include <cstring> #in

POJ 2135 Farm Tour(费用流)

POJ 2135 Farm Tour 题目链接 题意:给定一个无向图,边有权值,求从1到n再从n到1的最短路 思路:费用流,连边容量为1(注意是无向图),然后源点和1连容量2,n和汇点连容量是2 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> #include <algorithm> using namespace std; const int

poj 2135 Farm Tour 【无向图最小费用最大流】

题目:poj 2135 Farm Tour 题意:给出一个无向图,问从 1 点到 n 点然后又回到一点总共的最短路. 分析:这个题目不读仔细的话可能会当做最短路来做,最短路求出来的不一定是最优的,他是两条分别最短,但不一定是和最短. 我们可以用费用流来很轻易的解决,建边容量为1,费用为边权,然后源点s连 1 ,费用0 ,容量 2 ,n点连接汇点,容量2,费用0,,就可以了. 注意这个题目是无向图,所以要建双向边. AC代码: #include <iostream> #include <a

POJ 2135 Farm Tour (dinic算法,网络流)

构图方法: 注意题目中的边为无向边.新建源点s 和 汇点t 每两条道路连一条容量为1,费用为w的边.s到1连一条容量为1,费用为0 的边,n到 t 连一条容量为1,费用为0 的边,求最大流. #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <queue> #include

POJ 2135 Farm Tour

Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 213564-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 comprise

网络流(最小费用最大流):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