# AHU-835 FJ的旅行 【最小费用最大流】

Description

Input

Output

Sample Input
4 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2

Sample Output
6

题解：一开始当成最短路做，发现有很多bug根本不能做，学了费用流发现可以做，设一个源点到1的费用为0容量为2，设一个汇点，从n到汇点的费用为0容量为2，其他弧费用为路径长度，容量为1，跑最小费用最大流算法可以过。代码：
 1 #include<bits/stdc++.h>
2 using namespace std;
3 #define INF 0x3f3f3f3f
4 #define M(a, b) memset(a, b, sizeof(a))
5 const int N = 1100;
6 struct Edge {
7     int from, to, cap, flow, cost;
8 };
9
10 struct MCMF {
11     int n, m;
12     vector<Edge> edges;
13     vector<int> G[N];
14     int d[N], inq[N], p[N], a[N];
15
16     void init(int n) {
17         this->n = n;
18         for (int i = 0; i <= n+1; ++i) G[i].clear();
19         edges.clear();
20     }
21
22     void AddEdge(int from, int to, int cap, int cost) {
23         edges.push_back((Edge){from, to, cap, 0, cost});
24         edges.push_back((Edge){to, from, 0, 0, -cost});
25         m = edges.size();
26         G[from].push_back(m-2); G[to].push_back(m-1);
27     }
28
29     bool spfa(int s, int t, int &flow, int &cost) {
30         M(inq, 0); M(d, INF);
31         d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = INF;
32         queue<int> q;
33         q.push(s);
34         while (!q.empty()) {
35             int x = q.front(); q.pop();
36             inq[x] = 0;
37             for (int i = 0; i < G[x].size(); ++i) {
38                 Edge &e = edges[G[x][i]];
39                 if (d[e.to] > d[x] + e.cost && e.cap > e.flow) {
40                     d[e.to] = d[x] + e.cost;
41                     p[e.to] = G[x][i];
42                     a[e.to] = min(a[x], e.cap-e.flow);
43                     if (inq[e.to]) continue;
44                     q.push(e.to); inq[e.to] = 1;
45                 }
46             }
47         }
48         if (d[t] == INF) return false;
49         flow += a[t];
50         cost += d[t] * a[t];
51         int u = t;
52         while (u != s) {
53             edges[p[u]].flow += a[t];
54             edges[p[u]^1].flow -= a[t];
55             u = edges[p[u]].from;
56         }
57         return true;
58     }
59
60     int Mincost(int s, int t) {
61         int flow = 0, cost = 0;
62         while (spfa(s, t, flow, cost));
63         return cost;
64     }
65
66 }solver;
67
68 int main() {
69     int n, m;
70     while (~scanf("%d%d", &n, &m)) {
71         solver.init(n);
74         int from, to, cost;
75         for (int i = 0; i < m; ++i) {
76             scanf("%d%d%d", &from, &to, &cost);
84 }