Problem Solving report POJ2135 Farm Tour
Description
When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= n <=) fields numbered 1..N, the first of which contains he house and the Nth of WHI CH contains the Big barn. A Total M (1 <= m <= 10000) Paths this connect the fields in various ways. Each path connects the different fields and have a nonzero length smaller than 35,000.
To show off his farm in the best-of-the-walks, he-a-tour-that-starts at his house, potentially travels through-some fields, a nd ends at the barn. Later, he returns (potentially through some fields) the back to his house again.
He wants his tour to being as short as possible, however he doesn ' t want-walk on any given path more than once. Calculate the shortest tour possible. FJ is sure this 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 lengt H.
Output
A single line containing the length of the shortest tour.
Sample Input
4 51 2 12 3 13 4 11 3 22 4 2
Sample Output
6
The main topic: give you an 1~n,m graph, there are n nodes numbered the edge of the line. And the starting point is 1 and the end point is N. Give the length of each side, and ask each side only to walk once, ask you to go from the beginning to the end point and then back to the starting point of the shortest path is how much?
Analysis: The first intuitive feeling, if you can repeat the straight on the SPFA can be. But this problem requires every day side can only walk once, then the shortest path algorithm is invalid. So what do we do? It is recommended that you refer to another blog post, which is a short-circuit-less model of the edge, which appears to be the minimum cost stream upgrade for the model.
ZOJ 2760 how many shortest Path
http://blog.csdn.net/maxichu/article/details/45151399
So far I have assumed that you understand that the shortest circuit is not repeated. Through the maximum flow we can find a few days without repeating the shortest, but this question is not required to be two short circuit, but the total length of the minimum. So we have to introduce the minimum cost flow to solve the problem. The theory suggestion of the minimum cost flow see the challenge, but feel the code implementation of the challenge is not very suitable for my small white, so I understand the self-created a template (praise up. Embarrassed). So the problem is actually the most basic of a minimum cost flow.
First, the Super Source connection node 1, the load is 2, the cost is 0. Then, based on the input, the node n is connected to the super sink, the load is 2 and the cost is 0. Then run a minimum charge flow with a maximum flow of 2 is the answer. I use the "challenge" in my template H-labeling method, but this problem does not exist in the negative edge, I just to save a template ...
On the code:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include < Queue> #include <deque>using namespace std;const int maxn = 90010;const int MAXM = 910000;const int INF = 0x3f3f3f3 F;struct Edge{int from, to, caps, next, cost;}; Edge edge[maxm];int prevv[maxn];int preve[maxn];int dist[maxn];int head[maxn];int H[MAXN]; Label array int src, des, cnt;void addedge (int from, int to, int caps, int cost) {Edge[cnt].from = From;edge[cnt].to = To;edge[c Nt].cap = Cap;edge[cnt].cost = Cost;edge[cnt].next = Head[from];head[from] = Cnt++;swap (from, to); edge[cnt].from = from; edge[cnt].to = To;edge[cnt].cap = 0;edge[cnt].cost =-cost;edge[cnt].next = Head[from];head[from] = cnt++;} int SPFA () {deque<int> dq;bool inqueue[maxn];memset (Dist, INF, sizeof Dist), memset (inqueue, 0, sizeof Inqueue);d Q.push_back (SRC); inqueue[src] = 1;dist[src] = 0;while (!dq.empty ()) {int u = dq.front ();d q.pop_front (); Inqueue[u] = 0;for (int i = head[u]; I! =-1; i = Edge[i].Next) {int v = edge[i].to;if (Edge[i].cap > 0 && dist[u] + edge[i].cost +h[u]-h[v]< dist[v]) {Dist[v] = Dist[u] + Edge[i].cost + h[u]-h[v];p revv[v] = u;preve[v] = i;if (!inqueue[v]) {if (!dq.empty () && dist[v] <= dist[dq.fr Ont ()]) {Dq.push_front (v);} Elsedq.push_back (v);}}} return 0;} int min_cost_flow (int f,int n) {memset (h, 0, sizeof h); int cost = 0;while (F > 0) {SPFA (); if (dist[des] = = INF) {return-1 ;} for (int u = 1; u <= n; u++) H[u] + dist[u];h[des] + = dist[des];int D = f;for (int i = des; I! = src; i = prevv[i]) {d = m In (d, Edge[preve[i]].cap);} Here the cycle compares the chicken because the load is 1. F-= D;cost + = d*h[des];for (int i = des; i = src; i = prevv[i]) {edge[preve[i]].cap-= D;edge[preve[i] ^ 1].cap + = D;}} return cost;} int main () {int n, m;src = 0;des = 90005;while (cin >> n >> m) {memset (head,-1, sizeof head); cnt = 0;int A, b , c;for (int i = 1; I <= m; i++) {cin >> a >> b >> C;addedge (A, B, 1, c); Addedge (b, A, 1, c);} Addedge (SRC, 1, 2, 0); AddeDge (N, DES, 2, 0); cout << Min_cost_flow (2,n) << Endl;} return 0;}
Yesterday night to finish webserver, it seems today to review the network and embedded and communication principles. By, I absolutely did not expose my professional background. You'll never guess what I'm majoring in!
Problem solving report POJ2135 Farm Tour