BZOJ 1003: [ZJOI2006]物流运输

BZOJ 1003: [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

HINT

Source

Solution

splay暴力算出(f_{i~j})天的最短路,dp转移即可

Code

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
#define rep(i,x) for(int i=head[x];i;i=e[i].next)
#define mem(a,x) memset(a,x,sizeof(a))
typedef long long LL;
typedef double DB;
using namespace std;
inline int read() {
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9') f=(ch=='-')?-1:f,ch=getchar();
	while(ch>='0'&&ch<='9') x=x*10+(ch-'0'),ch=getchar();return f*x;
}
const int inf=0x3f3f3f3f;
struct data {int next,to,w;}e[801];
int ne,head[21],n,m,k,q,x,y,z;
bool flag[101][21];
long long t[101][101],f[101];
void insert(int u,int v,int w) {e[++ne].to=v,e[ne].w=w,e[ne].next=head[u];head[u]=ne;}
int spfa(int a,int b) {
	bool block[21];
	int dis[21],q[500],inq[21];
	memset(block,0,sizeof(block));
	memset(dis,inf,sizeof(dis));
	memset(inq,0,sizeof(inq));
	fo(i,a,b) fo(j,1,m) if(flag[i][j]) block[j]=1;
	q[0]=1,inq[1]=1,dis[1]=0;
	int t=0,w=1;
	while(t<w) {
		rep(p,q[t]) {
			if(!block[e[p].to]&&dis[e[p].to]>dis[q[t]]+e[p].w) {
				dis[e[p].to]=dis[q[t]]+e[p].w;
				if(!inq[e[p].to]) {
					q[w++]=e[p].to,inq[e[p].to]=1;
				}
			}
		}
		inq[q[t]]=0,t++;
	}
	return dis[m];
}
int main() {
	n=read(),m=read(),k=read(),q=read();
	fo(i,1,q) x=read(),y=read(),z=read(),insert(x,y,z),insert(y,x,z);
	int d=read();
	fo(i,1,d) {
		x=read(),y=read(),z=read();
		fo(j,y,z) flag[j][x]=1;
	}
	fo(i,1,n) fo(j,1,n) t[i][j]=spfa(i,j);
	fo(i,1,n) {
		f[i]=(LL)t[1][i]*i;
		fo(j,0,i-1) f[i]=min(f[i],f[j]+k+t[j+1][i]*(i-j));
	}
	printf("%d",f[n]);
	return 0;
}
原文地址:https://www.cnblogs.com/patricksu/p/7987917.html