閱讀702 返回首頁    go 阿裏雲 go 技術社區[雲棲]


最小費用最大流-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 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,還不太懂 ,先貼上

最後更新:2017-04-03 12:56:39

  上一篇:go ubuntu 14.04中安裝Jenkins
  下一篇:go 線段樹-區間延遲修改-zoj-1610