[Luogu] P1772 [ZJOI2006] Logistics transportation (dynamic planning, shortest way)

Source: Internet
Author: User
Tags cmath

P1772 [ZJOI2006] Logistics Transportation Topic 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 format:

The first line is a four integer\ (n (l≤n≤100), M (l≤m≤20), k\)And\ (e\)。\ (n\)Indicates the number of days required to transport the goods,\ (m\)Indicates the total number of docks,\ (k\)Represents the cost per modification of the shipping route,\ (e\)Indicates the number of route lines. Next\ (e\)Each row 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) \)。 One of the docks\ (a\)Number is\ (1\)Terminal\ (b\)Number is\ (m\)。 The cost of transportation per unit length is\ (1\)。 The route is bidirectional. And then the next line is an integer\ (d\), Behind the\ (d\)Rows are three integers per line\ (P (1<p<m), A, B (1≤a≤b≤n) \)。 Indicates that the number is\ (p\)The pier from the first\ (a\)Days to the first\ (b\)The cargo can not be loaded (including tail). The same dock may not be available for multiple time periods. But at any time there was at least one from the pier\ (a\)to the Pier\ (b\)Transport routes.

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/Output sample

Input Sample # #: copy

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

Output Example # #: replication

32

Description

"Sample Input description"

In turn, the 1th to the 5th day of the situation, the shadow indicates the pier is not available.

"Sample Output description"

The first three days walk 1-4-5, after two days walk 1-3-5, so the total cost is (3+2)2+10=32.


Exercises

A very good topic. I can't do it because I'm too vegetable.
This is a very obvious question, and it can be seen as a DP.
Because our goal is the lowest charge, is to consider changing the route this operation.
Because \ (m\) value is small. So we can run the shortest way while DP.
The shortest circuit should not be said to know it.
This way we enumerate the circumstances of the day.
\ (dp[i]\) represents the minimum consumption for the first day.
So the whole question we have to consider is how long it takes a short circuit to be valid.
Because the impact of the best answer is the cost of each change of road.
Then we will enumerate the first \ (i\) to \ (j\) days with the currently valid shortest path on it.
Equation is
\ (dp[i]=min (dp[i],dp[j-1]+ Shortest Path * (i-j+1) +k) \)

Code

Version Floyd
It was blown up.

#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include < iostream>using namespace Std;const int n=21;int n,m,k,l,q,vis[n];int u[n*10],v[n*10],z[n*10];int ch[N][101],f[101]    , Dis[n][n];int read () {int X=0,w=1;char ch=getchar (); while (ch> ' 9 ' | |    ch< ' 0 ') {if (ch== '-') W=-1;ch=getchar ();}    while (ch>= ' 0 ' &&ch<= ' 9 ') x=x*10+ch-' 0 ', Ch=getchar (); return x*w;}    void Floyd () {memset (dis,63,sizeof (dis));    for (int i=1;i<=l;i++) dis[u[i]][v[i]]=dis[v[i]][u[i]]=z[i]; for (int p=1;p<=m;p++) for (int i=1;i<=m;i++) for (int j=1;j<=m;j++) {if (i!=j&&j!=p            &&p!=i&&!vis[i]&&!vis[j]&&!vis[p]) {if (dis[i][j]>dis[p][j]+dis[i][p])        DIS[I][J]=DIS[I][P]+DIS[P][J];    }}}int Main () {n=read (); M=read (); K=read (); L=read ();    for (int i=1;i<=l;i++) {u[i]=read (), V[i]=read (), Z[i]=read ();    } q=read (); for (int i=1;i<=q;i+ +) {int x=read (), Lx=read (), Rx=read ();    for (int j=lx;j<=rx;j++) ch[x][j]=1;    } memset (F,63,sizeof (f)); f[0]=-k;        for (int i=1;i<=n;i++) {for (int j=1;j<=m;j++) vis[j]=0;            for (int. j=i;j>=1;j--) {for (int p=1;p<=m;p++) VIS[P]|=CH[P][J];            Floyd (); if (dis[1][m]==dis[0][0]) continue;        F[i]=min (f[i],f[j-1]+ (i-j+1) *dis[1][m]+k);    }} printf ("%d\n", F[n]); return 0;}

Version SPFA

#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue > #include <iostream>using namespace std;const int n=21;int n,m,k,l,q,vis[n],inf;int u[n*10],v[n*10],z[n*10];    int Ch[n][101],f[101],dis[n][n];int read () {int X=0,w=1;char ch=getchar (); while (ch> ' 9 ' | |    ch< ' 0 ') {if (ch== '-') W=-1;ch=getchar ();}    while (ch>= ' 0 ' &&ch<= ' 9 ') x=x*10+ch-' 0 ', Ch=getchar (); return x*w;}    int SPFA () {Queue<int>q;q.push (1);    int dist[21],ff[21];    memset (dist,63,sizeof (Dist)); inf=dist[0];    memset (ff,0,sizeof (FF));d ist[1]=0;ff[1]=1;        while (!q.empty ()) {int U=q.front (); Q.pop ();        ff[u]=0;            for (int i=1;i<=m;i++) {if (vis[i]) continue;                if (Dis[u][i]&&dist[i]>dist[u]+dis[u][i]) {dist[i]=dist[u]+dis[u][i];                if (!ff[i]) {Q.push (i); ff[i]=1; }}}}return dist[m];} int main () {n=read (); m=read(); K=read (); L=read ();        for (int i=1;i<=l;i++) {//u[i]=read (), V[i]=read (), Z[i]=read ();        int X=read (), Y=read (), Vi=read ();    Dis[x][y]=dis[y][x]=vi;    } q=read ();        for (int i=1;i<=q;i++) {int x=read (), Lx=read (), Rx=read ();    for (int j=lx;j<=rx;j++) ch[x][j]=1;    } memset (F,63,sizeof (f)); f[0]=-k;        for (int i=1;i<=n;i++) {for (int j=1;j<=m;j++) vis[j]=0;        for (int. j=i;j>=1;j--) {for (int p=1;p<=m;p++) VIS[P]|=CH[P][J];        int VI=SPFA ();            if (Vi==inf) continue;        F[i]=min (f[i],f[j-1]+ (i-j+1) *vi+k);    }} printf ("%d\n", F[n]); return 0;}

Dijkstra the goo Off.

[Luogu] P1772 [ZJOI2006] Logistics transportation (dynamic planning, shortest way)

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.