"DP+SPFA" "ZJOI2006" "bzoj1003" "cogs1824" logistics transport Trans

Source: Internet
Author: User
Tags port number

1824. [ZJOI2006] Logistics transport trans ★★☆ input file: bzoj_1003.in output file: bzoj_1003.out simple comparison
Time limit: 1 s memory limit: 162 MB


Now it's official data, you can submit it.


"Title 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 Format"

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 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. "Sample Input"

5 5 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
"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

Because each change of the route will not affect the previous, so think of DP (written like a backpack.) )。 Because it is time will lead to changes in the path, so according to the time to divide the stage, with SPFA run every period of time I to J of the shortest, and then update the cost, because the beginning to do SPFA when the distance is set to infinity, two points between the last return or infinity, so to judge. The DP equation is as follows:

F (i) =min (f[i],f[j]+cost[j+1][i]+k) (0<=j<i)

Code:

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath
> #include <algorithm> using namespace std; struct edge{int v,len,next;}
EDGE[10001];
int map[31][31],cost[101][101];
int head[10001],f[1001],v[1001],s[1001],q[100001];
BOOL Use[21][101],vis[21];
int n,m,k,e,d,num=0,l=0,r=0;
	inline int in () {int x=0; char Ch=getchar ();
	while (ch< ' 0 ' | | ch> ' 9 ') Ch=getchar ();
	while (ch>= ' 0 ' && ch<= ' 9 ') x=x*10+ch-' 0 ', Ch=getchar ();
return x;
	} inline void Add (int u,int v,int l) {edge[++num].v=v; edge[num].len=l; Edge[num].next=head[u];
Head[u]=num;
	} inline void init () {n=in (), M=in (), K=in (), E=in ();
	memset (cost,0,sizeof (cost));
		for (int i=1; i<=e; i++) {int u=in (), V=in (), L=in (); Add (u,v,l);
	Add (v,u,l);
	} d=in ();
	memset (use,0,sizeof (use));
		for (int i=1; i<=d; i++) {int p=in (), A=in (), B=in ();
	for (int j=a; j<=b; j + +) Use[p][j]=1;
	}} inline void Spfa () {memset (f,0x3f,sizeof (f)); Q[++l]=1;
	f[1]=0;
		while (l!=r) {int u=q[++r]; v[u]=0; for (int i=head[u]; i; i=edge[i].next) if (!VIS[EDGE[I].V] && f[u]+edge[i].len<f[edge[i].v]) {F[edge[i]
					. V]=f[u]+edge[i].len;
				if (!V[EDGE[I].V]) v[edge[i].v]=1,q[++l]=edge[i].v;
			}}}: inline void dp () {for (Int. I=1; i<=n; i++) for (int j=i; j<=n; J + +) {memset (vis,0,sizeof (VIS));
						for (int k=2, k<m; k++) for (int l=i; l<=j; l++) if (Use[k][l]) {vis[k]=1;
					Break
			} SPFA ();
			if (F[M]&GT;=0X3FFFF) cost[i][j]=f[m];
	else cost[i][j]=f[m]* (j-i+1);
	} memset (S,0x3f,sizeof (s));
	s[0]=0;
	for (int i=1, i<=n; i++) for (int j=0; j<i; j + +) S[i]=min (s[i],s[j]+cost[j+1][i]+k);
printf ("%d\n", s[n]-k);
	} int main () {init ();
	DP ();
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.