POJ: Farm Tour

dume0011
3 min readDec 6, 2021

--

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

Problem Analysis:

We can see that it need to have 2 paths, and no common edge, that make the cost is minimum. So we can convert this problem to a min cost flow problem.

We set each edge’s capacity is 1, as the 2 paths’s start and end vertex is reverse, we can set one as start vertex, and another one a end vertex, and we query for minimum cost of capacity 2 flows from start to end vertex.

#include <cstdio>
#include <vector>
using namespace std;struct SEdge {
int to;
int cap;
int cost;
int rev;
SEdge(int t, int c, int co, int r): to(t), cap(c), cost(co), rev(r) {}
};
#define MAX_N 1000
#define MAX_M 10000
#define INF 0x3f3f3f3f
int n, m;
vector<SEdge> G[MAX_N];
int dist[MAX_N];
int prevv[MAX_N];
int preve[MAX_N];
void add_edge(int from, int to, int cap, int cost) {
SEdge fromEdge(to, cap, cost, (int)G[to].size());
G[from].push_back(fromEdge);
SEdge toEdge(from, 0, -cost, (int)G[from].size() - 1);
G[to].push_back(toEdge);
}
int min_cost_flow(int s, int t, int f) {
int res = 0;
while (f > 0) {
fill(dist, dist+n, INF);
dist[s] = 0;
bool update = true;
while (update) {
update = false;
for (int v = 0; v < n; ++v) {
if (dist[v] == INF) continue;
for (int i = 0; i < G[v].size(); ++i) {
SEdge& e = G[v][i];
if (e.cap > 0 && dist[e.to] > dist[v] + e.cost) {
dist[e.to] = dist[v] + e.cost;
prevv[e.to] = v;
preve[e.to] = i;
update = true;
}
}
}
}
if (dist[t] == INF) return -1; int d = f;
for (int v = t; v != s; v = prevv[v])
d = min(d, G[prevv[v]][preve[v]].cap);
f -= d;
res += d * dist[t];
for (int v = t; v != s; v = prevv[v]) {
SEdge& e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
int main(int argc, char *argv[]) {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; ++i) {
getchar();
int from, to, cost;
scanf("%d%d%d", &from, &to, &cost);
add_edge(from - 1, to - 1, 1, cost);
add_edge(to - 1, from - 1, 1, cost);
}
printf("%d\n", min_cost_flow(0, n-1, 2)); return 0;
}

time complexity is O(mn)

space complexity is O(n)

--

--

No responses yet