Bzoj 1003: [ZJOI2006] Logistics transportation Trans DP+SPFA

Source: Internet
Author: User



Dp[i]=min (dp[j]+dur[j+1][i]* (i-j) +k) dur (i,j) represents the shortest distance from I-day to J-day no-change path

1003: [ZJOI2006] Logistics transport trans Time limit: ten Sec Memory Limit: 162 MB
Submit: 4001 Solved: 1671
[Submit] [Status] [Discuss] 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

The first line is four integers n (1<=n<=100), M (1<=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

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.

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 Output +HINT

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

Source



/* ***********************************************author:ckbosscreated time:2015 April 30 Thursday 10:58 56 seconds file Name : bzoj1003.cpp************************************************ * * #include <iostream> #include <cstdio># Include <cstring> #include <algorithm> #include <string> #include <cmath> #include <cstdlib > #include <vector> #include <queue> #include <set> #include <map>using namespace Std;typedef Long long int ll;const int maxn=1000;const LL inf=0x3f3f3f3f3f3f3f3f;int n,m,k,e,d;struct edge{int To,next,len;} Edge[maxn];int adj[50],size;void init () {memset (adj,-1,sizeof (ADJ)); size=0;} void Add_edge (int u,int v,int len) {edge[size].to=v; edge[size].next=adj[u];edge[size].len=len; adj[u]=size++;} void Add_edge (int u,int v,int len) {Add_edge (U,v,len); Add_edge (V,u,len);} BOOL go[50][200]; LL dur[200][200];bool cango[50];/*************spfa********************/int dist[50],cq[50];bool inq[50];bool SPFA () {memset (dist,63,sizeof (diST); memset (Cq,0,sizeof (CQ)); memset (Inq,false,sizeof (INQ));d ist[1]=0; Queue<int> Q;q.push (1); Inq[1]=true; Cq[1]=1;while (!q.empty ()) {int U=q.front (); Q.pop (); for (int i=adj[u];~i;i=edge[i].next) {int V=edge[i].to;int len= Edge[i].len;if (Cango[v]==false) continue;if (Dist[v]>dist[u]+len) {dist[v]=dist[u]+len;if (!inq[v]) {inq[v]=true ; Cq[v]++;if (cq[v]>=m) return False;q.push (v);}} Inq[u]=false;} return true;}    LL Dp[200];int Main () {//freopen ("In.txt", "R", stdin); Freopen ("OUT.txt", "w", stdout); init (); memset (go,true,sizeof (Go)); scanf ("%d%d%d%d", &n,&m,&k,&e ); for (int i=0;i<e;i++) {int u,v,l;scanf ("%d%d%d", &u,&v,&l); Add_edge (u,v,l);} scanf ("%d", &d), for (int i=0;i<d;i++) {int p,a,b;scanf ("%d%d%d", &p,&a,&b), and for (int j=a;j<=b;j++ ) Go[p][j]=false;} for (int i=1;i<=n;i++) {to (int j=i;j<=n;j++) {memset (cango,true,sizeof (Cango)); for (int k=1;k<=m;k++) {bool fg=true;for (int l=i;l<=j&&fg;l++) if (go[k][l]==false) fg=false;if (fg==false) Cango[k]=false;} SPFA ();d ur[i][j]=1ll*dist[m];}} Memset (Dp,63,sizeof (DP));DP [0]=0;dp[1]=dur[1][1];for (int i=2;i<=n;i++) {if (dur[1][i]!=inf) dp[i]=dur[1][i]*i; for (int j=1;j+1<=i;j++) {if (Dur[j+1][i]==inf) continue;dp[i]=min (dp[i],dp[j]+dur[j+1][i]* (i-j) +K);}}    printf ("%lld\n", Dp[n]); return 0;}


Bzoj 1003: [ZJOI2006] Logistics transportation Trans DP+SPFA

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.