[USACO 03FEB]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

题目大意

约翰家有 N 间牛棚,M 条双向道路连接了这些牛棚,第 i 条道路连接了第 A i 间牛棚和第 B i 间牛棚,长度为 L i 。所有牛棚中最好的是第一间和最后一间,所以当有朋友来访时,他会带着朋友从第一间牛棚走到第 N 间牛棚,然后再回到第一间牛棚。约翰想让朋友多看看乡村不同的景色,所以希望来回的路上不重复经过任何一条道路,不过重复经过一间牛棚是允许的。请帮助约翰选择一条路线,使得往返路径的总长度最短。输入数据保证路线总是存在的。

题解

就是流量为$2$的最小费用流。

每条边流量为$1$,建立原点连向1号节点,流量为$2$。

 1 #include<set>
 2 #include<map>
 3 #include<ctime>
 4 #include<cmath>
 5 #include<queue>
 6 #include<stack>
 7 #include<vector>
 8 #include<cstdio>
 9 #include<string>
10 #include<cstring>
11 #include<cstdlib>
12 #include<iostream>
13 #include<algorithm>
14 #define LL long long
15 using namespace std;
16 const int N=1000;
17 const int M=10000;
18 
19 int n,m,u,v,c;
20 struct tt
21 {
22     int to,next,cost,cap;
23 }edge[M*4+5];
24 int path[N+5],top=-1;
25 int pre[N+5];
26 
27 void Add(int u,int v,int cost,int cap)
28 {
29     edge[++top].to=v;
30     edge[top].next=path[u];
31     edge[top].cost=cost;
32     edge[top].cap=cap;
33     path[u]=top;
34 }
35 
36 int SPFA()
37 {
38     int dist[N+5];
39     memset(dist,127/3,sizeof(dist));dist[0]=0;
40     int INF=dist[1];
41     bool vis[N+5]={0};vis[0]=1;
42     queue<int>Q;
43     while (!Q.empty()) Q.pop();
44     Q.push(0);
45     while (!Q.empty())
46     {
47       int u=Q.front();Q.pop();vis[u]=0;
48       for (int i=path[u];i!=-1;i=edge[i].next)
49       {
50           int v=edge[i].to;
51           if (dist[v]>dist[u]+edge[i].cost&&edge[i].cap>0)
52           {
53             dist[v]=dist[u]+edge[i].cost;
54             pre[v]=i;
55             if (!vis[v])
56             {
57                 vis[v]=1;
58                 Q.push(v);
59             }
60           }
61       }
62     }
63     return dist[n]==INF ? 0:dist[n];
64 }
65 
66 void change(int r)
67 {
68     if (!r) return;
69     edge[pre[r]].cap--;
70     edge[pre[r]^1].cap++;
71     change(edge[pre[r]^1].to);
72 }
73 
74 int min_cost_flow()
75 {
76     int tolcost=0;
77     int tmp;
78     while (tmp=SPFA()) tolcost+=tmp,change(n);
79     return tolcost;
80 }
81 
82 int main()
83 {
84     memset(path,-1,sizeof(path));
85     scanf("%d%d",&n,&m);
86     Add(0,1,0,2);
87     Add(1,0,0,0);
88     for (int i=1;i<=m;i++)
89     {
90       scanf("%d%d%d",&u,&v,&c);
91       Add(u,v,c,1);
92       Add(v,u,-c,0);
93       Add(v,u,c,1);
94       Add(u,v,-c,0);
95     }
96     printf("%d
",min_cost_flow());
97     return 0;
98 }
原文地址:https://www.cnblogs.com/NaVi-Awson/p/7445409.html