关闭

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

标签: 费用流
409人阅读 评论(0) 收藏 举报
分类:

传送门:poj 2135 Farm Tour

描述:

Farm Tour
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15031   Accepted: 5756

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


题意:

FJ带朋友参观自己的农场,从自己的房子出发到农场,再从农场返回自己的房子,要求去回不走同一条路。房子的点数为1,农场为n,在1到n之间有很多点,给出n个顶点,m条边,然后m行每行有三个数,a,b,c代表a到c的路径长度为c,并且a到b是无向边,现在要求从1点到n点在从n点返回1点的最短路

思路:

因为每条边只能经过一次,可以设置这条边的容量是1,费用就是长度。然后增加一个源点s和一个汇点t,从s到1结点连一条容量是2费用是0的弧,对于汇点t也是一样,这样就构建好了一个最小费用最大流的图了。但是需要注意的是,题目给出的是无向图,所以建边的时候要正反两个方向各键一次。

代码:
#include <iostream>
#include<queue>
#include<cstring>
using namespace std;

typedef long long ll;
typedef int Type;
const int N=1005;
const Type inf=0x3f3f3f3f;

struct edge{
  int u,v;
  Type cap, flow, cost;
  edge() {}
  edge(int from,int to,int c,int f,int co):u(from),v(to),cap(c),flow(f),cost(co){}
};

struct MCMF{
  int n,m;
  vector<edge>es;
  vector<int>G[N];
  int inq[N];//是否在队列中
  Type d[N];//SPFA时总费用
  int p[N];//上一条弧
  Type a[N];//可改进量

  void init(int n){
    this->n=n;
    for(int i=0; i<n; i++)G[i].clear();
    es.clear();
  }

  void add_edge(int u,int v,Type cap,Type cost){
    es.push_back(edge(u, v, cap, 0, cost));
    es.push_back(edge(v, u, 0, 0, -cost));
    m=es.size();
    G[u].push_back(m-2);
    G[v].push_back(m-1);
  }

  bool spfa(int s,int t,Type& flow,Type& cost){
    memset(inq, 0, sizeof(inq));
    for(int i=0; i<n; i++)d[i]=inf;
    d[s]=0;inq[s]=1;p[s]=0;a[s]=inf;

    queue<int>q;
    q.push(s);
    while(!q.empty()){
      int u=q.front();q.pop();inq[u]=0;
      for(int i=0; i<G[u].size(); i++){
        edge& e=es[G[u][i]];
        int v=e.v;
        if(e.cap>e.flow && d[v]>d[u]+e.cost){ //在新流下考虑最小费用
          d[v]=d[u]+e.cost;
          a[v]=min(a[u], e.cap-e.flow);
          p[v]=G[u][i];
          if(!inq[v]){ inq[v]=1; q.push(v);}
        }
      }
    }
    if(d[t]==inf)return false;
    flow+=a[t],cost+=a[t]*d[t];
    int u=t;
    while(u!=s){
      es[p[u]].flow+=a[t];
      es[p[u]^1].flow-=a[t];
      u=es[p[u]].u;
    }
    return true;
  }

  Type Mincost(int s,int t){
    Type flow=0, cost=0;
    while(spfa(s,t,flow,cost));
    return cost;
  }
}H;

int main(){
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  int n,m;
  cin>>n>>m;
  H.init(n+2);
  int u,v,c;
  while(m--){
    cin>>u>>v>>c;
    H.add_edge(u,v,1,c);
    H.add_edge(v,u,1,c);
  }
  H.add_edge(0, 1, 2, 0);
  H.add_edge(n, n+1, 2, 0);

  cout<<H.Mincost(0, n+1)<<endl;
  return 0;
}




1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:63813次
    • 积分:3181
    • 等级:
    • 排名:第10770名
    • 原创:260篇
    • 转载:8篇
    • 译文:0篇
    • 评论:9条
    博客专栏
    文章分类