Logistics and Transportation

Source: Internet
Author: User
Tags min port number
Title Description

Description
The logistics company is going to ship a batch of cargo from Wharf A to Pier B. Due to the large volume of goods, it takes n days to complete the shipment. In the course of cargo transport, several terminals are generally diverted. Logistics companies typically design a fixed transport route to carry out strict management and tracking of the entire transport process. Due to the existence of various factors, sometimes a pier will not be able to load and unload goods. At this time, the transport route must be modified to allow the goods to arrive at their destination. But modifying the route is a very troublesome thing, and will bring additional costs. So logistics companies want to be able to order an N-day shipping plan, making the total cost as small as possible.
Input/output format Input/output
Input format:
The first line is four integers n (l≤n≤100), M (l≤m≤20), K, and E. n indicates the number of days to transport the goods, m represents the total number of docks, and K indicates the cost of each modification of the shipping route. The next line of e lines is a description of the route, including three integers, which in turn represent the two port number of the route connection and the route length (>0). Pier A is numbered 1 and Pier B is M. The transportation cost per unit length is 1. The route is bidirectional. Then the next line is an integer d, followed by a row of D for each row is three integers P (1< p< m), A, B (1≤a≤b≤n). Indicates that the pier numbered P is unable to load and unload goods from day A to day B (including tail). The same dock may not be available for multiple time periods. But at any time there is at least one transport route from Pier A to Pier B.
Output format:
Includes an integer representing the minimum total cost. Total cost =n days the sum of the length of the transportation route +k* Change the number of shipping routes.
Input and Output Sample sample Input/output
Sample test point # # Input Example:
5 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 output:
32
Description description
"Sample Input description"

The figure above shows the 1th to 5th days in turn, and the shadows indicate the docks that are not available.
"Sample Output description"
The first three days go 1-4-5, after two days walk 1-3-5, so the total cost is (*3+) (3+2) *2+10=32.Analysis

Single source shortest way +DP.
DP equation: F[i]=min{ans (1,i) * I,f[j]+ans (j+1,i) * (I-J) +k} (1=< j<=i-1) F[i] represents the minimum freight for the first I day,
Ans (x, y) indicates the shortest path from the beginning to the end of the X-day to the Y-day, and can be used Bellman FORD,DIJKSTRA,SPFA ... The equation is well understood.
The first case: 1 to I days all go one way.
The second situation: j+1 to I day walk a road (this road is recorded as Path 1). This time, regardless of the first J-day Path 2 and path 1 is the same, it is considered as a path change, so add K. This is true, because if the same adds K there must be no solution for the first case or a J-better solution. Specifically, this more forward J, is the real change of the path of the dividing line. So at first it might be considered how to record the status to determine whether or not to add K, so that can not be done. The value of this problem is actually in the processing of K.

The above DP equation can be changed to:
Because ans (1,i) * I is equivalent, the first day and the No. 0 day are compared to change the path, that is
F[i]=min{f[j-1]+ans (j,i) * (i-j+1) +k}-K (1=< j<=i)
Because at the beginning of the Min:=maxlongint, the first day of comparison will add a K, so in the end should subtract another K. Code

var ans:array[1..100,1..100]of longint;
    F:array[0..100]of Longint;
    Use,v:array[1..100]of Boolean;
    Can:array[1..20,1..100]of Boolean;
    Dist:array[1..20]of Longint;
    Map:array[1..20,1..20]of Longint;

Min,a,b,c,n,m,k,e,i,d,j:longint;
Procedure Dijkstra;
var i,j,min,minj:longint;
  Begin for I:=1 to M do begin dist[i]:=maxint;v[i]:=false;end;
  dist[1]:=0;
      For i:=1 to M-1 do begin min:=maxint;
            For J:=1 to M does if (not V[j]) and (not Use[j]) and (Min>dist[j]) then begin MIN:=DIST[J];
          Minj:=j;
     End
      If Minj=m then exit;
      V[minj]:=true;
            For J:=1 to M does if (not V[j]) and (map[minj,j]>0) then if (Dist[j]>dist[minj]+map[minj,j]) then
    DIST[J]:=DIST[MINJ]+MAP[MINJ,J];
End

End
Procedure Get_ans;
var i,j,p,k:longint;
       Begin for I:=1 to N does for J:=1 to N does begin Fillchar (use,sizeof (use), false); For K:=i to J do for P:=1To M does if can[p,k] then use[p]:=true;
       Dijkstra
     ANS[I,J]:=DIST[M];
End

End
  Begin READLN (n,m,k,e);
      For I:=1 to E do begin READLN (A,B,C);
      Map[a,b]:=c;
    Map[b,a]:=c;
  End
  READLN (d);
      For i:=1 to D do begin READLN (C,A,B);
    For J:=a and b do can[c,j]:=true;

  End

  Get_ans;
  f[0]:=0;
      For I:=1 to n do begin min:=maxlongint;
        For j:=1 to I does begin if min>f[j-1]+ans[j,i]* (i-j+1) +k then min:=f[j-1]+ans[j,i]* (i-j+1) +k;
      End
    F[i]:=min;

  End
Write (f[n]-k);


End.

 Evaluation result: Accepted Score: 100 submitted on: 2015-10-17 17:50 Time: 137ms Memory: 3182kb Click to enter the list of records. Compile information compiling compile successful evaluation result Test point #1: Pass the test point.
Score 10, time consuming 0ms, memory 3149kB. Test point #2: Pass the test point.
Score 10, time consuming 0ms, memory 3149kB. Test point #3: Pass the test point.
Score 10, time consuming 0ms, memory 3129kB. Test point #4: Pass the test point.
Score 10, time consuming 0ms, memory 3158kB. Test point #5: Pass the test point.
Score 10, time consuming 15ms, memory 3162kB. Test point #6: Pass the test point.
Score 10, time consuming 15ms, memory 3149kB. Test point #7: Pass the test point.
Score 10, time consuming 15ms, memory 3174kB. Test point #8: Pass the test point. Score 10, time consuming 15ms, Memory 3174kB. Test point #9: Pass the test point.
Score 10, time consuming 31ms, memory 3166kB. Test point #10: Pass the test point. Score 10, time consuming 46ms, memory 3182kB.

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.