zoukankan      html  css  js  c++  java
  • [ZJOI2006]物流运输

    1003: [ZJOI2006]物流运输

    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 8020  Solved: 3353
    [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
    //前三天走1-4-5,后两天走1-3-5,这样总成本为(2+2)*3+(3+2)*2+10=32

    一次路线增加K的费用,n天,那么,可以设状态dp[i]表示前i天的最小费用,那么转移很好想,dp[i]=min(dp[j] + cost[j+1][i]*(i-j) + K),cost[i][j]ij天共同的最短路的长度,从[j+1,i]天内都使用这条路,相对于j天来讲换了一次;可能会想,这个dpdp[j]会不会跟cost[j+1][i]是相同的,没关系,因为总会存在一个点使得路径转化,j是从0枚举的,dpmin,所以答案肯定是正确的;

     1 #include<algorithm>
     2 #include<iostream>
     3 #include<cstdlib>
     4 #include<cstdio>
     5 #include<cmath>
     6 #include<map>
     7 #include<set>
     8 #include<queue>
     9 #include<vector>
    10 #include<iomanip>
    11 #include<cstring>
    12 #define inf 1<<29
    13 #define ll long long
    14 #define db double
    15 #define re register
    16 #define il inline
    17 #define rep(i,a,b) for(register int i=a;i<=b;++i)
    18 #define file(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
    19 using namespace std;
    20 const int N=110;
    21 struct Edge{
    22   int to,net,w;
    23 }e[250*2];
    24 int head[25],num_e,day;
    25 int dp[N],cost[N][N],K,n,m;
    26 bool can[25][N][N];
    27 il void add(int x,int y,int w) {
    28   e[++num_e].to=y,e[num_e].w=w,e[num_e].net=head[x],head[x]=num_e;
    29 }
    30 inline int gi() {
    31   re int res=0,f=1;re char ch=getchar();
    32   while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    33   if(ch=='-') f=-1,ch=getchar();
    34   while(ch>='0'&&ch<='9') res=res*10+ch-'0',ch=getchar();
    35   return res*f;
    36 }
    37 bool inq[25];
    38 int dis[25];
    39 il void spfa(int a,int b) {
    40   for(re int i=1;i<=n;i++) dis[i]=inf;
    41   memset(inq,0,sizeof(inq));
    42   queue<int> q;
    43   q.push(1);
    44   dis[1]=0;inq[1]=1;
    45   re int u;
    46   while(!q.empty()) {
    47     u=q.front();q.pop();
    48     inq[u]=0;
    49     for(int i=head[u];i;i=e[i].net) {
    50       int to=e[i].to;
    51       if(dis[to] > dis[u] + e[i].w && can[to][a][b]) {
    52     dis[to]=dis[u] + e[i].w;
    53     if(!inq[to]) q.push(to),inq[to]=1;
    54       }
    55     }
    56   }
    57   cost[a][b]=dis[n];
    58 }
    59 void DP() {
    60   rep(i,1,day) dp[i]=inf;
    61   dp[0]=-K;
    62   for(re int i=1;i<=day;i++)
    63     for(re int j=0;j<i;j++)
    64       if(dp[j]<inf&&cost[j+1][i]<inf)
    65       dp[i]=min(dp[i],dp[j]+cost[j+1][i] * (i-j)+K);
    66 }
    67 int main(){
    68   file("spfa");
    69   day=gi(),n=gi(),K=gi(),m=gi();
    70   re int u,v,w;
    71   while(m--){
    72     u=gi(),v=gi(),w=gi();
    73     add(u,v,w),add(v,u,w);
    74   }
    75   m=gi();
    76   memset(can,true,sizeof(can));
    77   while(m--){
    78     w=gi(),u=gi(),v=gi();
    79     for(re int i=1;i<=v;i++)
    80       for(re int j=u;j<=day;j++)
    81     if(i<=j)
    82     can[w][i][j]=false;
    83   }
    84   
    85   for(re int i=1;i<=day;i++)
    86     for(re int j=i;j<=day;j++)
    87       spfa(i,j);
    88   DP();
    89   printf("%d",dp[day]);
    90   return 0;
    91 }
  • 相关阅读:
    2015 9月15日 工作计划与执行
    2015 9月14日 工作计划与执行
    2015 9月14日 周计划
    2015 9月11日 工作计划与执行
    2015 9月10日 工作计划与执行
    2015 9月9日 工作计划与执行
    2015 9月8日 工作计划与执行
    js 有用信息集
    2015 9月7日 工作计划与执行
    文件间的编译依存关系 分类: C/C++ 2015-06-10 15:37 34人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/ypz999/p/7207267.html
Copyright © 2011-2022 走看看