Farm Tour
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 13778 | Accepted: 5228 |
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.
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.
* 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个点,编号从1到n,然后从1到n,再从n返回1,两次路径不能有重复边,但点可以重复,求两次路径的最小和 //最小费用流第一题。可以看成从1到n走两次,不能有重复边,然后根据题目给的数据建图,无向图建两条边,建超级源点0,和1连边,容量为2,费用为0,超级汇点n+1和n连边,容量2,费用0,其他边容量皆为1,这样确保只能被走一次,费用即为边的长度,然后求出最小费用流即可 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <queue> using namespace std; const int N = 1100; const int INF = 0x3f3f3f3f; struct edge { int to, cap, cost, rev; }; vector <edge> G[N]; int dist[N]; int prevv[N], preve[N]; bool used[N]; int n, m; void add_edge(int from, int to, int cap, int cost) { edge e; e.to = to, e.cap = cap, e.cost = cost, e.rev = G[to].size(); G[from].push_back(e); e.to = from, e.cap = 0, e.cost = -cost, e.rev = G[from].size() - 1; G[to].push_back(e); } int min_cost_flow(int s, int t, int f) { int res = 0; while(f > 0) { memset(dist, 0x3f, sizeof dist); memset(used, 0, sizeof used); queue <int> que; que.push(s); dist[s] = 0; used[s] = true; while(! que.empty()) { int v = que.front(); que.pop(); for(int i = 0; i < G[v].size(); i++) { edge &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; if(!used[e.to]) { que.push(e.to); used[e.to] = true; } } } used[v] = false; } if(dist[t] == INF) return -1; int d = f; for(int i = t; i != s; i = prevv[i]) d = min(d, G[prevv[i]][preve[i]].cap); f -= d; res += d * dist[t]; for(int i = t; i != s; i = prevv[i]) { edge &e = G[prevv[i]][preve[i]]; e.cap -= d; G[i][e.rev].cap += d; } } return res; } int main() { int a, b, c; while(~ scanf("%d%d", &n, &m)) { add_edge(0, 1, 2, 0); add_edge(n, n + 1, 2, 0); for(int i = 0; i < m; i++) { scanf("%d%d%d", &a, &b, &c); add_edge(a, b, 1, c); add_edge(b, a, 1, c); } printf("%d\n", min_cost_flow(0, n + 1, 2)); for(int i = 0; i <= n + 1; i++) G[i].clear(); } return 0; }