[ZJOI2006]物流运输

Description
物流公司要把一批货物从码头A运到码头B。由于货物量比较大,需要n天才能运完。货物运输过程中一般要转停好几个码头。物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格的管理和跟踪。由于各种因素的存在,有的时候某个码头会无法装卸货物。这时候就必须修改运输路线,让货物能够按时到达目的地。但是修改路线是一件十分麻烦的事情,会带来额外的成本。因此物流公司希望能够订一个n天的运输计划,使得总成本尽可能地小。

Input
第一行是四个整数n(1<=n<=100)、m(1<=m<=20)、K和e。n表示货物运输所需天数,m表示码头总数,K表示每次修改运输路线所需成本。接下来e行每行是一条航线描述,包括了三个整数,依次表示航线连接的两个码头编号以及航线长度(>0)。其中码头A编号为1,码头B编号为m。单位长度的运输费用为1。航线是双向的。再接下来一行是一个整数d,后面的d行每行是三个整数P( 1 < P < m)、a、b(1< = a < = b < = n)。表示编号为P的码头从第a天到第b天无法装卸货物(含头尾)。同一个码头有可能在多个时间段内不可用。但任何时间都存在至少一条从码头A到码头B的运输路线。

Output
包括了一个整数表示最小的总成本。总成本=n天运输路线长度之和+K*改变运输路线的次数。

Sample Input
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
//前三天走1-4-5,后两天走1-3-5,这样总成本为(2+2)3+(3+2)2+10=32


首先用spfa处理出任意两天之间的花费,然后设f[i]为第i天的花费,那么dp方程就非常明显
(f[i]=min{f[j]+cost[j+1][i]+K}(1leqslant j<i))

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x3f3f3f3f
#define sqr(x) ((x)*(x))
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline int read(){
	int x=0,f=1;char ch=getchar();
	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')    f=-1;
	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<1)+(x<<3)+ch-'0';
	return x*f;
}
inline void print(int x){
	if (x>=10)     print(x/10);
	putchar(x%10+'0');
}
const int N=1e2;
int pre[(sqr(N)<<1)+10],now[N+10],child[(sqr(N)<<1)+10],val[(sqr(N)<<1)+10];
int n,m,K,e,q,tot;
int cost[N+10][N+10],dis[N+10],h[N+10],f[N+10];
bool fix[N+10][N+10],vis[N+10],tag[N+10];
void join(int x,int y,int z){pre[++tot]=now[x],now[x]=tot,child[tot]=y,val[tot]=z;}
int SPFA(){
	memset(dis,63,sizeof(dis));
	int head=0,tail=1;
	h[1]=1,dis[1]=0,vis[1]=1;
	while (head!=tail){
		if (++head>N)	head=1;
		int Now=h[head];
		for (int p=now[Now],son=child[p];p;p=pre[p],son=child[p]){
			if (dis[son]>dis[Now]+val[p]&&!tag[son]){
				dis[son]=dis[Now]+val[p];
				if (!vis[son]){
					if (++tail>N)	tail=1;
					vis[h[tail]=son]=1;
				}
			}
		}
		vis[Now]=0;
	}
	return dis[m];
}
int main(){
	n=read(),m=read(),K=read(),e=read();
	for (int i=1;i<=e;i++){
		int x=read(),y=read(),z=read();
		join(x,y,z),join(y,x,z);
	}
	q=read();
	for (int i=1;i<=q;i++){
		int x=read(),l=read(),r=read();
		for (int j=l;j<=r;j++)	fix[x][j]=1;
	}
	for (int i=1;i<=n;i++){
		for (int j=1;j<=n;j++){
			memset(tag,0,sizeof(tag));
			for (int k=1;k<=m;k++)
				for (int l=i;l<=j;l++)
					tag[k]|=fix[k][l];
			cost[i][j]=SPFA();
		}
	}
	for (int i=1;i<=n;i++)
		for (int j=1;j<=n;j++)
			if (cost[i][j]<inf)
				cost[i][j]*=j-i+1;
	for (int i=1;i<=n;i++)	f[i]=cost[1][i];
	for (int i=2;i<=n;i++)
		for (int j=1;j<i;j++)
			f[i]=min(f[i],f[j]+cost[j+1][i]+K);
	printf("%d
",f[n]);
	return 0;
}
原文地址:https://www.cnblogs.com/Wolfycz/p/9141554.html