题目链接: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
USACO 2003 February Green
题解:
把问题转化为:最小费用最大流。
1.每一条边其容量为1, 其费用为距离。
2.可知题目要求两条不同的路径,那么对于这个网络流图,就是要求:在流量为2的状态下,1到n的最小费用。
3.那么怎么转化为最小费用最大流呢?设一个超级源点和一个超级汇点。且超级源点到1的容量为2,费用为0, n到超级汇点的容量为2,费用为0;且题目说明了必定有解,那么在这个条件下求超级源点到超级汇点的最小费用最大流,最大流就只能为2了。所以最小费用即为题目所求。
代码如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include