zoukankan      html  css  js  c++  java
  • POJ2135 Farm Tour —— 最小费用最大流

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


    Farm Tour
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 17672   Accepted: 6851

    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.每一条边其容量为1, 其费用为距离。

    2.可知题目要求两条不同的路径,那么对于这个网络流图,就是要求:在流量为2的状态下,1到n的最小费用。

    3.那么怎么转化为最小费用最大流呢?设一个超级源点和一个超级汇点。且超级源点到1的容量为2,费用为0, n到超级汇点的容量为2,费用为0;且题目说明了必定有解,那么在这个条件下求超级源点到超级汇点的最小费用最大流,最大流就只能为2了。所以最小费用即为题目所求。



    代码如下:

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <algorithm>
      5 #include <vector>
      6 #include <cmath>
      7 #include <queue>
      8 #include <stack>
      9 #include <map>
     10 #include <string>
     11 #include <set>
     12 #define ms(a,b) memset((a),(b),sizeof((a)))
     13 using namespace std;
     14 typedef long long LL;
     15 const int INF = 2e9;
     16 const LL LNF = 9e18;
     17 const int mod = 1e9+7;
     18 const int MAXN = 1e3+10;
     19 
     20 struct Edge
     21 {
     22     int to, next, cap, flow, cost;
     23 }edge[10010<<2];
     24 int tot, head[MAXN];
     25 int pre[MAXN], dis[MAXN];
     26 bool vis[MAXN];
     27 int N;
     28 
     29 void init(int n)
     30 {
     31     N = n;
     32     tot = 0;
     33     memset(head, -1, sizeof(head));
     34 }
     35 
     36 void add(int u, int v, int cap, int cost)
     37 {
     38     edge[tot].to = v; edge[tot].cap = cap; edge[tot].cost = cost;
     39     edge[tot].flow = 0; edge[tot].next = head[u]; head[u] = tot++;
     40     edge[tot].to = u; edge[tot].cap = 0; edge[tot].cost = -cost;
     41     edge[tot].flow = 0; edge[tot].next = head[v]; head[v] = tot++;
     42 }
     43 
     44 bool spfa(int s, int t)
     45 {
     46     queue<int>q;
     47     for(int i = 0; i<=N; i++)
     48     {
     49         dis[i] = INF;
     50         vis[i] = false;
     51         pre[i] = -1;
     52     }
     53 
     54     dis[s] = 0;
     55     vis[s] = true;
     56     q.push(s);
     57     while(!q.empty())
     58     {
     59         int u  = q.front();
     60         q.pop();
     61         vis[u] = false;
     62         for(int i = head[u]; i!=-1; i = edge[i].next)
     63         {
     64             int v = edge[i].to;
     65             if(edge[i].cap>edge[i].flow && dis[v]>dis[u]+edge[i].cost)
     66             {
     67                 dis[v] = dis[u]+edge[i].cost;
     68                 pre[v] = i;
     69                 if(!vis[v])
     70                 {
     71                     vis[v] = true;
     72                     q.push(v);
     73                 }
     74             }
     75         }
     76     }
     77     if(pre[t]==-1) return false;
     78     return true;
     79 }
     80 
     81 int minCostMaxFlow(int s, int t, int &cost)
     82 {
     83     int flow = 0;
     84     cost = 0;
     85     while(spfa(s,t))
     86     {
     87         int Min = INF;
     88         for(int i = pre[t]; i!=-1; i = pre[edge[i^1].to])
     89         {
     90             if(Min>edge[i].cap-edge[i].flow)
     91                 Min = edge[i].cap-edge[i].flow;
     92         }
     93         for(int i = pre[t]; i!=-1; i = pre[edge[i^1].to])
     94         {
     95             edge[i].flow += Min;
     96             edge[i^1].flow -= Min;
     97             cost += edge[i].cost*Min;
     98         }
     99         flow += Min;
    100     }
    101     return flow;
    102 }
    103 
    104 int main()
    105 {
    106     int n, m;
    107     scanf("%d%d",&n,&m);
    108     init(n+2);
    109     for(int i = 1; i<=m; i++)
    110     {
    111         int u, v, c;
    112         scanf("%d%d%d",&u,&v,&c);
    113         add(u,v,1,c);   //双向图,容量为1,花费即为距离c
    114         add(v,u,1,c);
    115     }
    116     int start = 0, end = n+1;
    117     add(start, 1, 2, 0);   //超级源点到1的边,单向。容量为2, 花费为0
    118     add(n, end, 2, 0);  //n到超级汇点的边,单向。容量为2, 花费为0
    119     int ans;
    120     minCostMaxFlow(start, end, ans);
    121     printf("%d
    ", ans);
    122     return 0;
    123 }
    View Code
  • 相关阅读:
    WMS、WCS、PLC、AGV
    SAP消息号修改汇总
    SQL 计算累计和 sum() over( partition by order by )
    DDLS报错数据类型冲突:data type conflict in a selection 解决办法
    SAP销售订单需求类型的确定优先级
    SAP替代,出口U904在RGGBS000中未生成
    订单BOM与销售BOM的区别
    在配置和销售凭证 GET_CONFIG_MODE 间通信时内部出错
    ABAP Write
    php的api接口
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538609.html
Copyright © 2011-2022 走看看