10806-Dijkstra, Dijkstra.

Source: Internet
Author: User

// The Problem of the minimum cost and the maximum flow (it refers to the problem that the total cost is the minimum when the total traffic is the maximum)

// Because you do not need to find the total stream for this question, it is more suitable to use Edmonds-Karp than pre-stream propulsion.

// Note that cost [u] [PA [u] =-cost [PA [u] [u]; in this way, we can ensure that when the stream is resized again, we can "TOP" the stream that was not suitable before.

// Do not use dijisktra twice, for example, the following data

6. <br/> 8 <br/> 1 2 1 <br/> 2 3 1 <br/> 3 6 1 <br/> 1 4 100 <br/> 4 5 100 <br/> 5 6 100 <br/> 1 3 10 <br/> 2 6 10 </P> <p>

# Include <iostream> <br/> # include <cstdio> <br/> # include <queue> <br/> # include <memory. h> </P> <p> using namespace STD; <br/> const int max = 105, INF = 1 <30; <br/> bool INQ [Max]; <br/> int m, n, Cap [Max] [Max], flow [Max] [Max], cost [Max] [Max], <br/> d [Max], a [Max], pa [Max]; <br/> queue <int> q; <br/> int BFS () {<br/> int f = 0, c = 0, U, V; <br/> memset (flow, 0, sizeof (flow )); <br/> while (1) {<br/> memset (INQ, 0, sizeof (INQ); <B R/> for (INT I = 2; I <= N; I ++) <br/> d [I] = inf; <br/> d [1] = 0; <br/> INQ [1] = true; <br/> q. push (1); <br/> while (! Q. empty () {<br/> U = Q. front (); q. pop (); <br/> INQ [u] = false; <br/> for (V = 1; v <= N; V ++) {<br/> If (u = 5 & V = 2) {<br/>/* cout <Endl <CAP [u] [v] <"<br/> <flow [u] [v] <Endl; <br/> cout <D [v] <"" <D [u] <Endl; <br/> cout <cost [u] [v] <Endl; */<br/>}< br/> If (Cap [u] [v]> flow [u] [v] & D [v]> d [u] + cost [u] [v]) {<br/> d [v] = d [u] + cost [u] [v]; <br/> pa [v] = u; <br/> If (! INQ [v]) {<br/> INQ [v] = true; <br/> q. push (V); <br/>}< br/> If (d [N] = inf) <br/> break; <br/> int A = inf; <br/> for (u = N; u! = 1; u = pa [u]) A = A <(Cap [PA [u] [u]-flow [PA [u] [u])? <Br/> A: CAP [PA [u] [u]-flow [PA [u] [u]); <br/> for (u = N; u! = 1; u = pa [u]) {<br/> flow [PA [u] [u] + =; <br/> flow [u] [PA [u]-=; <br/> cost [u] [PA [u] =-cost [PA [u] [u]; <br/>}< br/> // cout <"A:" <A <"D:" <D [N] <Endl; <br/> U = N; <br/>/* While (u! = 1) {<br/> cout <u <"<-- (" <D [PA [u] <")" <"--"; <br/> U = pa [u]; <br/>}< br/> cout <1 <Endl; */<br/> C + = d [N] * A; <br/> F + = A; <br/> If (F = 2) {<br/> return C; <br/>}< br/> return 0; <br/>}< br/> int main () <br/>{< br/> freopen ("I .txt", "r", stdin); <br/> int U, V, C, ans; <br/> while (CIN> N, N) {<br/> CIN> m; <br/> memset (Cap, 0, sizeof (CAP )); <br/> memset (cost, 0, sizeof (cost); <br/> for (INT I = 0; I <m; I ++) {<br/> CIN> U> V> C; <br/> CAP [u] [v] = CAP [v] [u] = 1; <br/> cost [u] [v] = cost [v] [u] = C; <br/> // CAP [u] [v] = 1; <br/> // cost [u] [v] = C; <br/>}< br/> ans = BFS (); <br/> If (ANS) <br/> cout <ans <Endl; <br/> else <br/> cout <"back to jail" <Endl; <br/>}< br/> return 0; <br/>}< br/>

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.