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,相当于找到两条从1到n的路径且二者不能有交集。
对于每条路,费用为长度,容量为1,这样就限制了只能走一次。新建一个顶点连向1,费用为0,容量为2.,作为等价源点。同理再建一个等价汇点。
因为重边的存在,图的存储结构为邻接表而非邻接矩阵。
注意:不能单纯的找两次最短路,反例见下图:
代码:spfa,还不太懂 ,先贴上
no-understand 最小费用最大流-poj-2135,布布扣,bubuko.com
no-understand 最小费用最大流-poj-2135
原文:http://blog.csdn.net/chuchus/article/details/25831271