poj_2315 Minimum cost maximum flow

Source: Internet
Author: User

Main Topic

There are n vertices on a graph, numbered from 1 to N, there are some non-forward edges between vertices, edges have length, require to go from vertex 1 to vertex N, and then from Vertex n back to Vertex 1, where it is not necessary to go through each vertex, but the edge of the path required to walk only once. Find the minimum length of the path from 1--->n-->1.

Problem Analysis

Each non-forward edge can only walk at most once, which could be considered as a capacity of only 1 of these edges. The topic requires that you go from vertex 1 to N and then back to Vertex 1, where the passing edge can only walk once, in fact, as the two different paths from Vertex 1 (the edges of the path cannot have coincident) reach the vertex n. Then it can be considered that the total flow from vertex 1 to vertex n is 2. The title requires the minimum total path length, which can be considered as the network flow cost, then the problem is converted to the solution of the minimum cost maximum flow.
The introduction of the source point SS and the sink point Tt,ss introduces an edge to Vertex 1, a capacity of 2, a cost of 0, an edge to TT from Vertex N, a capacity of 2, and a cost of 0. The problem is the maximum flow of the minimum cost for arriving at TT from SS.
Because the edges are non-aligned, the U-->V and V-->u are added when the edges are added, and the corresponding reverse edges are added (that is, the actual figure (U,V) edge corresponds to 4 edges in the network flow graph).

Implementation (c + +)
#include <stdio.h> #include <string.h> #include <queue> #include <algorithm>using namespace std ; #define INFINITE 1 << 26#define max_node 1005#define max_edge_num 40005struct edge{int to;int vol;int cost;int Next ;}; Edge gedges[max_edge_num];int ghead[max_node];int gpre[max_node];int gpath[max_node];int gDist[MAX_NODE];int gedgecount;void Insertedge (int u, int v, int vol, int cost) {gedges[gedgecount].to = V;gedges[gedgecount].vol = vol;gedges[ Gedgecount].cost = Cost;gedges[gedgecount].next = Ghead[u];ghead[u] = gedgecount++;gedges[gedgecount].to = u;gEdges[ Gedgecount].vol = 0;//vol is 0, which means that the reverse of the edge is Gedges[gedgecount].cost =-cost;//cost is the opposite of the cost of the positive edge for the start time, this is to Gedges[gedgecount ].next = ghead[v];ghead[v] = gedgecount++;} Assuming that there are no negative weights and loops in the graph, the SPFA algorithm finds the shortest path/cost and the smallest path from the source point S to the end of the boundary T, the sum of the minimum paths bool SPFA (int s, int t) {memset (Gpre,-1, sizeof (GPRE)); Memset ( Gdist, 0x7F, sizeof (gdist)); Gdist[s] = 0;queue<int> Q; Q.push (s); while (! Q.empty ()) {//due to the absence of negative weights and loops, it is bound to end int u = Q.front (); Q.pop (); for (int E= Ghead[u]; E! =-1; E = gedges[e].next) {int v = gedges[e].to;if (Gedges[e].vol > 0 && gdist[u] + gedges[e].cost < Gdist[v]) {GDIs T[V] = Gdist[u] + gedges[e].cost;gpre[v] = u; Previous point Gpath[v] = e;//The previous edge of the point connection Q.push (v);}} if (gpre[t] = = 1)//If the end T does not have a pre set, the path to the end point T is not present return False;return true;} int Mincostflow (int s, int t) {int cost = 0;int flow = 0;while (SPFA (S, t)) {int f = infinite;for (int u = t; u! = s; u = GP) Re[u]) {if (Gedges[gpath[u]].vol < f) f = gedges[gpath[u]].vol;} Flow + = F;cost + gdist[t] * f;for (int u = t; u = s; u = Gpre[u]) {gedges[gpath[u]].vol-= f;//forward Edge capacity reduction gedges[gpath[u]^1] . vol + = f; Reverse edge capacity increased}}return cost;} int main () {int n, m, U, V, d;while (scanf ("%d%d", &n, &m)! = EOF) {Gedgecount = 0;memset (Ghead,-1, sizeof (Ghead)) ; Insertedge (0, 1, 2, 0); for (int i = 0; i < m; i++) {scanf ("%d%d%d", &u, &v, &d); Insertedge (U, V, 1, D); Sertedge (V, U, 1, d);} Insertedge (n, n + 1, 2, 0); int ans = mincostflow (0, n + 1);p rintf ("%d\n", ans);} Return 0;} 

poj_2315 Minimum cost maximum flow

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.