Code 1035 train stop [maximum fee flow]

Source: Internet
Author: User

Question: Code 1035 train stop


Question: Chinese Question. Read it directly.


Analysis: after reading the question, it is found that the maximum cost is not easy to take into account the cost flow. In fact, the maximum and minimum costs are the same. If the maximum cost is, the minimum cost can be changed to a negative value.

Graph creation method:

Super source point SS connection s, capacity is N, cost is 0

Split each train into two nodes I and II. The Edge building capacity is 1, and the cost is the opposite of the money paid to the station.

S connection I, capacity INF, cost 0

II connection T, capacity INF, cost 0

Enumeration edge creation before the last point of all time points. The capacity is 1 and the cost is 0.


And then calculate a billing flow. The opposite number is ans.

AC code:

# Include <cstdio> # include <iostream> # include <algorithm> # include <vector> # include <cstring> # include <queue> # include <string> # include <map> using namespace STD; # define del (a, B) memset (a, B, sizeof (A) const int INF = 0x3f3f3f3f; const int n = 220; struct node {int from,, cap, flow; double cost ;}; vector <node> E; vector <int> V [N]; int vis [N]; double dis [N]; int P [N], a [n]; // P save father, a save capvoid clear (int x) {fo R (INT I = 0; I <= x; I ++) V [I]. clear (); E. clear ();} void add_node (int from, int to, int cap, double cost) // Shab {e. push_back (node) {from, to, Cap, 0, cost}); E. push_back (node) {to, from, 0, 0,-cost}); int Len = E. size ()-1; V [to]. push_back (LEN); V [from]. push_back (len-1);} bool bellmanford (int s, int T, Int & flow, double & cost) {for (INT I = 0; I <= N; I ++) dis [I] = 1000000000; del (VIS, 0); DIS [s] = 0; vis [s] = 1; p [s] = 0; [S] = inf; queue <int> q; q. Push (s); While (! Q. empty () {int u = Q. front (); q. pop (); vis [u] = 0; For (INT I = 0; I <V [u]. size (); I ++) {node & G = E [V [u] [I]; If (G. cap> G. flow & dis [G. to]> dis [u] + G. cost) {dis [G. to] = dis [u] + G. cost; // printf ("%. 5lf \ n ", G. cost); P [G. to] = V [u] [I]; // Save the precursor A [G. to] = min (A [u], G. cap-g.flow); If (! Vis [G. to]) {q. push (G. to); vis [G. to] = 1 ;}}}if (DIS [T] = 1000000000) return false; flow + = A [T]; cost + = dis [T] * A [T]; int u = T; while (u! = S) {e [p [u]. flow + = A [T]; E [p [u] ^ 1]. flow-= A [T]; u = E [p [u]. from;} return true;} double min_cost (int s, int t) {int flow = 0; double cost = 0; while (bellmanford (S, T, flow, cost); Return cost;} struct tree {int fir, SEC; double val;}; tree KF [200]; int main () {int n, m; while (~ Scanf ("% d", & N, & M) {int Ss = 0, S = 1; int t = 2 * m + 3, tt = t + 1; for (INT I = 1; I <= m; I ++) {scanf ("% d % lf % d", & KF [I]. FIR, & KF [I]. val, & KF [I]. sec); KF [I]. SEC + = KF [I]. FIR; add_node (S, 2 * I, INF, 0); double cs = KF [I]. val * 0.01; // printf ("%. 5lf \ n ", CS); add_node (2 * I, 2 * I + 1,1,-CS); add_node (2 * I + 1, t, INF, 0 );} for (INT I = 1; I <= m; I ++) {for (Int J = 1; j <= m; j ++) // note {if (I = J) continue; If (kf [J]. fir> KF [I]. sec) add_node (I * 2 + 1, J * 0.0, 0) ;}} add_node (SS, S, N,); double ans = min_cost (SS, t ); printf ("%. 2lf \ n ",-ans); clear (TT);} return 0 ;}


Code 1035 train stop [maximum fee 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.