BZOJ1003

1003: [ZJOI2006]物流运输trans

Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 4160  Solved: 1735 [Submit][Status][Discuss]

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

HINT

前三天走1-4-5,后两天走1-3-5,这样总成本为(2+2)*3+(3+2)*2+10=32

--------------------->_<----------------------

„按时间DP
„F[i]表示到时间i最少成本是多少
„由于每段时间道路信息都不相同(节点是否开放)
„而点数又特别少
„所以预处理spfa[i][j]表示从时间i到时间j从起点到终点的最小代价
„(用spfa预处理)
„Dp方程
„F[i]=min(f[j-1]+spfa[i][j]*(i-j+1)+k)
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <cstdlib>
 5 #include <algorithm> 
 6 #define N 200
 7 #define M 100000 
 8 using namespace std;
 9 const int INF=2147483647; 
10 int head[N],next[M],to[M],len[M];
11 int can[N][N],pt[M],st[M],ed[M];
12 int dis[N],q[M],dp[N];
13 bool vis[N],bh[N];
14 int n,m,K,e,d,cnt; 
15 inline void add(int u,int v,int w)
16 {
17     to[cnt]=v; len[cnt]=w; next[cnt]=head[u]; head[u]=cnt++;
18 }
19 inline void build(int l,int r)
20 {
21      memset(bh,true,sizeof bh);
22      for(int i=1;i<=d;i++)
23      {
24          if(st[i]>r||ed[i]<l) continue;
25          bh[pt[i]]=false;
26      }
27 }
28 inline int spfa()
29 {
30      memset(dis,0,sizeof dis);
31      int h=1,t=2,sta;
32      q[1]=1; vis[1]=true; dis[1]=0;
33      while(h<t)
34      {
35          sta=q[h++]; vis[sta]=false;
36          for(int i=head[sta];~i;i=next[i])
37              if(bh[to[i]]&&dis[to[i]]>dis[sta]+len[i])
38              {
39                  dis[to[i]]=dis[sta]+len[i];
40                  if(!vis[to[i]]) vis[to[i]]=true,q[t++]=to[i];
41              }
42      }
43      return dis[m];
44 } 
45 inline void init()
46 {
47      memset(head,-1,sizeof head); cnt=0;
48      scanf("%d%d%d%d",&n,&m,&K,&e);
49      for(int i=1,a,b,c;i<=e;i++)
50      {
51          scanf("%d%d%d",&a,&b,&c);
52          add(a,b,c); add(b,a,c);
53      }
54      scanf("%d",&d);
55      for(int i=1;i<=d;i++) scanf("%d%d%d",&pt[i],&st[i],&ed[i]);
56      for(int i=1;i<=n;i++)
57          for(int j=i;j<=n;j++)
58          {
59              build(i,j);
60              can[i][j]=spfa();
61          }
62      memset(dp,0,sizeof dp);
63      dp[0]=0;
64      for(int i=1;i<=n;i++)
65          for(int j=1;j<=i;j++)
66          {
67              if(can[j][i]>=INF||dp[j-1]>=INF) continue;
68              dp[i]=min(dp[i],dp[j-1]+can[j][i]*(i-j+1)+K);
69          }
70      printf("%d\n",dp[n]-K);
71 }
72 int main()
73 {
74      init();
75      return 0;
76 }

 

 

posted on 2015-06-15 14:50  diamonddd  阅读(159)  评论(0编辑  收藏  举报

导航