最小费用最大流-poj-2135

简介: Farm Tour   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 

Farm Tour

 

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

微笑大意:小明喜欢带他的朋友们逛自己的农场。农场有n块地,屋舍位于1号,谷仓位于n号。有m条路连接这些地,路是无向的,每条路长度已知。他想设计一条线路,从1出发,到n,再回到1,且同一条路不走两遍。问最短的行程是多少。

分析:可建模为最小费用最大流。从1出发,到n,再回到1,相当于找到两条从1n的路径且二者不能有交集。

对于每条路,费用为长度,容量为1,这样就限制了只能走一次。新建一个顶点连向1,费用为0,容量为2.,作为等价源点。同理再建一个等价汇点。

因为重边的存在,图的存储结构为邻接表而非邻接矩阵。

注意:不能单纯的找两次最短路,反例见下图:

 

代码:spfa,还不太懂 ,先贴上

目录
相关文章
|
4月前
【每日一题Day243】LC1595连通两组点的最小成本 | 状态压缩dp
【每日一题Day243】LC1595连通两组点的最小成本 | 状态压缩dp
50 0
|
24天前
|
C++
G : 最大流问题
这篇文章介绍了最大流问题的基本概念和求解方法,并通过C++代码实现了使用广度优先搜索(BFS)和残余网络来计算有向图中从源点到汇点的最大流。
|
4月前
|
算法
DAY-6 | 牛客-NC107 寻找峰值:二分法巧得峰值
这是一个关于寻找数组峰值的编程问题,题目来源于牛客网的 NC107 题目。文章介绍了两种方法,其中重点讲解了使用二分法寻找峰值的思路:通过比较数组中点与相邻元素的大小,不断缩小搜索范围,直到找到峰值或范围只剩一个元素。二分法的时间复杂度为 O(logN),空间复杂度为 O(1)。文章还配有多张图解和代码示例来辅助理解。
49 4
|
4月前
|
Java 索引
leetcode-746:使用最小花费爬楼梯
leetcode-746:使用最小花费爬楼梯
36 0
|
算法 C++
数据结构与算法之最小爬楼梯费用&&动态规划
数据结构与算法之最小爬楼梯费用&&动态规划
72 0
数据结构与算法之最小爬楼梯费用&&动态规划
leetcode 746 使用最小花费爬楼梯
leetcode 746 使用最小花费爬楼梯
72 0
leetcode 746 使用最小花费爬楼梯
|
算法 数据建模
最小费用最大流问题详解
最小费用最大流问题详解
858 0
最小费用最大流问题详解
|
Python
LeetCode 746. 使用最小花费爬楼梯
数组的每个下标作为一个阶梯,第 i 个阶梯对应着一个非负数的体力花费值 cost[i](下标从 0 开始)。
78 0
使用最小花费爬楼梯(LeetCode-746)
使用最小花费爬楼梯(LeetCode-746)
74 0
|
算法
背包问题求方案数(一)
AcWing算法提高课内容,本文讲解 动态规划
140 0
背包问题求方案数(一)