bzoj1003 [ZJOI2006]物流运输

[ZJOI2006]物流运输

Time Limit: 10 Sec Memory Limit: 162 MB

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



首先每天最短路然后贪心是错的。。。 dp + 最短路。。。 dp[i] 表示前 i 天的最小值。。。 考虑枚举最后这次最短路的区间转移即可。。。 注意如果这个时间区间里的最短路不存在要特判一下。。。

```c++

include<bits/stdc++.h>

using namespace std;
struct lpl{
int to, dis;
inline bool operator < (const lpl &A)const{
return dis > A.dis;
}
};
int s, t, n, m, k, e, dp[105], cost[105][105];
int d, dis[25], lim[25][105];
bool vis[25], f[25];
vector point[25];
priority_queue q;

inline void putit(){
scanf("%d%d%d%d", &n, &m, &k, &e);
lpl lin; s = 1; t = m;
for(int a, b, i = 1; i <= e; ++i){
scanf("%d%d%d", &a, &b, &lin.dis);
lin.to = b; point[a].push_back(lin);
lin.to = a; point[b].push_back(lin);
}
scanf("%d", &d);
for(int a, l, r, i = 1; i <= d; ++i){
scanf("%d%d%d", &a, &l, &r);
for(int j = l; j <= r; ++j) lim[a][j] = true;
}
}

inline bool check(int t, int l, int r){
for(int i = l; i <= r; ++i)
if(lim[t][i]) return false;
return true;
}

inline int dijkstra(){
memset(f, false, sizeof(f));
memset(dis, 0x3f, sizeof(dis));
while(!q.empty()) q.pop();
q.push((lpl){s, 0}); dis[s] = 0;
while(!q.empty()){
lpl now = q.top(); q.pop();
if(f[now.to]) continue;
if(now.to == t) return dis[t];
f[now.to] = true;
for(int i = point[now.to].size() - 1; i >= 0; --i){
lpl qwe = point[now.to][i]; if(!vis[qwe.to]) continue;
if(dis[qwe.to] > now.dis + qwe.dis){
dis[qwe.to] = now.dis + qwe.dis;
q.push((lpl){qwe.to, dis[qwe.to]});
}
}
}
return dis[t];
}

inline int calc(int l, int r){
if(cost[l][r]) return cost[l][r];
memset(vis, false, sizeof(vis));
memset(dis, 0x3f, sizeof(dis));
for(int i = 1; i <= m; ++i)
if(check(i, l, r)) vis[i] = true;
return cost[l][r] = dijkstra();
}

inline void workk(){
memset(dp, 0x3f, sizeof(dp));
dp[0] = -k;
for(int i = 1; i <= n; ++i)
for(int j = 0; j < i; ++j)
if(calc(j + 1, i) < 1e9)
dp[i] = min(dp[i], dp[j] + calc(j + 1, i) * (i - j) + k);
cout << dp[n];
}

int main()
{
putit();
workk();
return 0;
}

posted @ 2018-10-30 22:01  沛霖  阅读(120)  评论(0编辑  收藏  举报