【题目描述】
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
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
//前三天走1-4-5,后两天走1-3-5,这样总成本为(2+2)*3+(3+2)*2+10=32
HINT
Source
【题解】
预处理出g[i][j]表示第i天到第j天都走同一条路的最小代价。
然后就可以dp了。
/* --------------
user Vanisher
problem bzoj-1003
----------------*/
# include <bits/stdc++.h>
# define ll long long
# define M 100010
# define N 21
# define D 110
# define inf 1e9
using namespace std;
int read(){
int tmp=0, fh=1; char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') fh=-1; ch=getchar();}
while (ch>='0'&&ch<='9'){tmp=tmp*10+ch-'0'; ch=getchar();}
return tmp*fh;
}
struct node{
int data,next,vote;
}e[M];
int dis[N],d,x[N],co[N][D],tag[N],f[D],g[D][D],place,head[N],n,use[N],q[N],k,m;
void build(int u, int v, int w){
e[++place].data=v; e[place].next=head[u]; head[u]=place; e[place].vote=w;
e[++place].data=u; e[place].next=head[v]; head[v]=place; e[place].vote=w;
}
int spfa(){
for (int i=1; i<=n; i++)
dis[i]=inf,use[i]=false;
dis[1]=0; use[1]=true;
int pl=1, pr=1; q[1]=1;
while (pl<=pr){
int x=q[(pl++)%n];
for (int ed=head[x]; ed!=0; ed=e[ed].next){
if (tag[e[ed].data]==true) continue;
if (dis[e[ed].data]>e[ed].vote+dis[x]){
dis[e[ed].data]=e[ed].vote+dis[x];
if (use[e[ed].data]==false){
use[e[ed].data]=true;
q[(++pr)%n]=e[ed].data;
}
}
}
use[x]=false;
}
return dis[n];
}
int main(){
d=read(), n=read(), k=read(), m=read();
for (int i=1; i<=m; i++) {
int u=read(), v=read(), w=read();
build(u,v,w);
}
int c=read();
for (int i=1; i<=c; i++){
int p=read(), l=read(), r=read();
for (int j=l; j<=r; j++)
co[p][j]=true;
}
for (int i=1; i<=d; i++){
memset(tag,0,sizeof(tag));
for (int j=i; j<=d; j++){
for (int t=1; t<=n; t++)
tag[t]=tag[t]|co[t][j];
g[i][j]=spfa();
}
}
for (int i=1; i<=d; i++){
f[i]=inf;
for (int j=1; j<=i; j++)
if (g[j][i]!=inf)
f[i]=min(f[i],f[j-1]+k+g[j][i]*(i-j+1));
}
printf("%d\n",f[d]-k);
return 0;
}