[Bzoj 1003] [zjoi2006] Logistics and Transportation Trans

Source: Internet
Author: User
Description

The logistics company wants to ship a batch of goods from Pier A to Terminal B. Because the cargo volume is large, it takes n days to complete the operation. In the process of cargo transportation, it is generally necessary to stop several docks. Logistics companies usually design a fixed transportation route to strictly manage and track the entire transportation process. Due to various factors, sometimes the cargo cannot be loaded or unloaded at a certain pier. At this time, you must modify the transportation route so that the goods can arrive at the destination on time. However, modifying the route is a very troublesome task and will incur additional costs. Therefore, the logistics company wants to set up a transportation plan for N days to minimize the total cost.

Input

The first row contains four integers, n (1 <= n <= 100), m (1 <= m <= 20), K, and E. N indicates the number of days required for cargo transportation, M indicates the total number of terminals, and K indicates the cost required for each modification of the transportation route. Next, each line of line E is a route description, which contains three integers, indicating the number of two terminals connected to the route and the length of the route (> 0 ). Pier A is 1 and pier B is M. The transportation fee per unit is 1. The route is bidirectional. The next row is an integer d, and the next row of D is three integers p (1 <p <m), A, B (1 <= A <= B <= N ). It indicates that goods (including head and tail) cannot be loaded and loaded from day A to day B of the pier numbered p ). The same pier may be unavailable for multiple time periods. However, there is at least one transportation route from Pier A to pier B at any time.

Output

An integer is included to indicate the minimum total cost. Total cost = the sum of N-day transportation route lengths + K * The number of transportation routes changed.

Sample input5 5 10 8
1 2 1
1 3 3
1 4 2
2 3 2
2 4 4
3 4 1
3 5 2
4 5 2
4
2 2 3
3 1 1
3 3 3
4 4 5

Sample outputsample output
32
Hint

Take 1-4-5 for the first three days, and 1-3-5 for the next two days. The total cost is (2 + 2) x 3 + (3 + 2) * 2 + 10 = 32

Source

 

It is obvious that the shortest path is the most short-circuited, but in another form, the time-based DP is used to run the shortest path during decision-making, points that cannot be used in each period of time are disabled in the spfa process (the point in the extended queue can be determined by adding conditions). When making a decision, you can find the shortest path for a period of time and update the DP array, although the idea seems very simple, I still got stuck for 2 days to make it out (the water problem in the big bull seems very difficult to AC in the eyes of Yan Miao). It seems that I am not very well-founded, you have to lay a solid foundation to improve your strength.

<P> // DP + spfa Shortest Path # include <stdio. h> # include <string. h> # include <stdlib. h >#include <algorithm> </P> <p> # define maxn 250 # define maxm 100000 </P> <p> using namespace STD; </P> <p> int f [maxm], n, m, K, E, D, CNT = 0; // F [I] = minimum cost to day I, f [I] = min {f [J] + cost [J + 1] [I] * (I-j) + k }, CNT = Total number of edges struct line {int from, to, W; // start point, end point, Edge Weight int next;} edges [maxm]; // edge set int start [maxm], end [maxm], head [maxm]; // start [I] = start time used by vertex I, end [I] = when the consumption of vertex I ends Int dis [maxm]; // dis [I] = distance from the start point to the I point, spfa uses int Q [maxm]; // simulate the queue, spfa uses int visit [maxm]; // visit [I] to indicate that vertex I has been accessed by INT mindis [maxn] [maxn]; // mindis [I] [J] = int day [maxm], PT [maxm]; void addline (INT U, int V, int W) // Add U-> v. If the edge weight is W, {existing} directed edge {edges [CNT]. from = u; edges [CNT]. to = V; edges [CNT]. W = W; edges [CNT]. next = head [u]; head [u] = CNT ++;} void Init () // input data {int I, j, L, U, V, W, p, A, B; memset (Head,-1, sizeof (head); scanf ("% D % d ", & N, & M, & K, & E); for (I = 1; I <= E; I ++) {scanf ("% d", & U, & V, & W); // input edge addline (U, V, W ); // addline (v, U, W);} scanf ("% d", & D); for (I = 1; I <= D; I ++) scanf ("% d", & PT [I], & start [I], & End [I]);} void prework (INT day1, int day2) {int I, j; memset (day, 1, sizeof (day); for (I = 1; I <= D; I ++) {If (start [I]> day2 | end [I] <day1) continue; Day [PT [I] = 0 ;}} int spfa () // calculate day1 ~ For spfa ~ Day2 always unblocked shortest path {int I, J, K, H = 1, t = 2, now; // H = team lead, t = team end, now = the first node of the team memset (DIS, 0x3f, sizeof (DIS); // At the beginning, the shortest distance between all nodes and the start point is + ∞ // memset (visit, 0, sizeof (visit); // clear the marked dis [1] = 0 in the queue; // The distance from the start point to the start point is 0 Q [1] = 1; // In the queue, set the start point to visit [1] = 1; // mark the start point to access while (H <t) {now = Q [H]; H ++; // obtain the first team and then start the team. Visit [now] = 0; for (I = head [now]; I! =-1; I = edges [I]. next) {If (day [edges [I]. to] & (DIS [edges [I]. to] = 0 | dis [now] + edges [I]. W <dis [edges [I]. to]) // If the I-th point is unobstructed at [day1, day2], the new path from now to I is closer than the old one, extend it to {dis [edges [I]. to] = dis [now] + edges [I]. w; // better if (! Visit [edges [I]. to]) {q [t ++] = edges [I]. to; // click I to join the team. Visit [edges [I]. to] = 1 ;}}} return dis [m]; // returns the distance from start 1 to end m} void solve () // DP solves {int I, j, l, t; for (I = 1; I <= N; I ++) for (j = 1; j <= N; j ++) {prework (I, j); mindis [I] [J] = spfa ();} memset (F, 0x3f, sizeof (f )); // set all the dynamic indicator arrays to infinity f [0] = 0; // DP initialization for (I = 1; I <= N; I ++) for (j = 1; j <= I; j ++) {If (mindis [J] [I]> = 0x3f3f3f3f | f [J-1]> = 0x3f3f3f3f) continue; f [I] = min (F [I], F [J-1] + mindis [J] [I] * (I-j + 1) + k ); // better update solution} printf ("% d \ n", F [N]-k); // note that a k is subtracted from the output (K is subtracted from the first time it was migrated to the east and west, actually, no reduction is required, because no route change was made for the first time)} int main () {Init (); solve (); Return 0 ;}</P>


 

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.