10594 data flow (minimum cost flow)

Source: Internet
Author: User

Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 116 & page = show_problem & problem = 1535


Problem F

Data Flow

Time Limit

5 seconds

 

In the latest lab of iiuc, it requires to send huge amount of data from the local server to the terminal server. the lab setup is not yet ready. it requires to write a router program for the best path of data. the problem is all links of the network has a fixed capacity and cannot flow more than that amount of data. also it takes certain amount of time to send one unit data through the link. to avoid the collision at a time only one data unit can travel I. e. at any instant more than one unit of data cannot travel parallel through the network. this may be time consuming but it certainly gives no collision. each node has sufficient buffering capability so that data can be temporarily stored there. iiuc Management wants the shortest possible time to send all the data from the local server to the final one.

 

 

For example, in the above network if anyone wants to send 20 unit data from A to D, he will send 10 unit data through ad link and then 10 unit data through AB-BD link which will take 10 + 70 = 80 unit time.

 

Input

Each input starts with two positive integersN (2N(100), M (1)M5000). In next few lines the link and corresponding propagation time will be given. the links are bidirectional and there will be at most one link between two network nodes. in next line there will be two positive integersD, KWhereDIs the amount of data to be transferred from 1stN'Th node andKIs the link capacity. input is terminated by EOF.

Output

For each dataset, print the minimum possible time in a line to send all the data. if it is not possible to send all the data, print "impossible. ". the time can be as large as 1015.

 

Sample Input

Output for sample input

4 5
1 4 1
1 3 3
3 4 4
1 2 2
2 4 5
20 10
4 4
1 3 3
3 4 4
1 2 2
2 4 5
20 100
4 4
1 3 3
3 4 4
1 2 2
2 4 5
20 1
80
140
Impossible.

 

Problemsetter: Md. kamruzzaman

Member of elite problemsetters 'panel


Question:

In an undirected graph G, data in Unit D is transmitted from Point 1 to point N. The maximum capacity of data transmitted on each side is K, and the cost of unit data transmitted on each side is given, calculates the minimum fee for data transmission in units D.

Analysis:

Note that an undirected graph must use an adjacent table to divide an undirected edge into directed edges in two directions. 1 is the source point, n is the sink point, and the minimum cost of traffic is D. Use spfa to find the augmented path.


# Include <cstdio> # include <iostream> # include <cstdlib> # include <algorithm> # include <ctime> # include <cctype> # include <cmath> # include <string> # include <cstring> # include <stack> # include <queue> # include <list> # include <vector> # include <map> # include <set> # define sqr (x) (x) * (x) # define ll long # define ITN int # define INF 0x3f3f3f3f # define inf_ll 0x3f3f3f3f3f3f3fll # define PI 3.1415926535897932384626 # define EPS 1E- 10 # define maxm 10007 <2 # define maxn 107 using namespace STD; int FIR [maxn], p [maxn]; long flow [maxm], cost [maxm], cap [maxm]; int U [maxm], V [maxm], rev [maxm], NEX [maxm]; int Q [maxm]; bool INQ [maxm]; long long d [maxn]; int e_max; long d, K; void spfa (INT s) {memset (D, 0x3f, sizeof D); memset (INQ, 0, sizeof INQ); D [s] = 0; int F, R; Q [f = r = 0] = s; while (F <= r) {int x = Q [f ++]; INQ [x] = false; For (int e = FIR [X]; ~ E; E = NEX [e]) {If (Cap [e]> flow [e] & D [V [e]> d [U [e] + cost [e]) {d [V [e] = d [U [e] + cost [E]; P [V [e] = E; If (! INQ [V [e]) {INQ [V [e] = true; Q [++ R] = V [e] ;}}} int main () {# ifndef online_judge freopen ("/home/fcbruce/documentation/code/T", "r", stdin); # endif // online_judge int n, m; while (~ Scanf ("% d", & N, & M) {e_max = 0; memset (FIR,-1, sizeof fir); For (INT I = 0; I <m; I ++) {int e = e_max ++; scanf ("% d % LLD", & U [e], & V [e], & cost [e]); NEX [e] = FIR [U [e]; FIR [U [e] = E; rev [e] = e + 1; E = e_max ++; U [e] = V [E-1]; V [e] = U [E-1]; cost [e] =-cost [E-1]; nex [e] = FIR [U [e]; FIR [U [e] = E; rev [e] = E-1; E = e_max ++; U [e] = U [E-1]; V [e] = V [E-1]; cost [e] =-cost [E-1]; nex [e] = FIR [U [e]; FIR [U [e] = E; rev [e] = e + 1; E = e_max ++; U [e] = V [E-1]; V [e] = U [E-1]; cost [e] =-cost [E-1]; nex [e] = FIR [U [e]; FIR [U [e] = E; rev [e] = E-1;} scanf ("% LLD ", & D, & K); For (INT I = 0; I <e_max; I ++) {if (I & 1) CAP [I] = 0; else CAP [I] = K;} memset (flow, 0, sizeof flow); LL total_flow = 0, total_cost = 0; int S = 1, t = N; for (;) {spfa (s); If (d [T] = inf_ll) break; long _ f = inf_ll; for (int e = P [T]; E = P [U [e]) {_ f = min (_ F, cap [e]-flow [e]); If (U [e] = s) break;} For (int e = P [T]; E = P [U [e]) {flow [e] + = _ F; flow [Rev [e]-= _ F; if (U [e] = s) break;} If (total_flow + _ F <D) {total_flow + = _ F; total_cost + = d [T] * _ F;} else {total_cost + = d [T] * (D-total_flow); total_flow = D; break ;}} if (total_flow = d) printf ("% LLD \ n", total_cost); else puts ("impossible. ");} return 0 ;}



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.