• BZOJ 1003[ZJOI2006]物流运输(SPFA+DP)


    Problem 1003. -- [ZJOI2006]物流运输

    1003: [ZJOI2006]物流运输

    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 10402  Solved: 4603
    [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

    HINT

     

    Source

     
    [Submit][Status][Discuss]


    HOME Back

    题解:

    由于题目给的数据只有100,20个港口;所以我们可以预处理出任意两点间的最短你距离;然后,假设cost[i][j]表示:从第i天到第j天的最短距离;

    dp[i]表示从第一个港口到第i个的费用的最小值;则dp[i]=min(dp[j]+cost[j+1][i]*(i-j)+k);对于每一个i,枚举其前面的点即可;

    dp[m]即为所求答案;

    参考代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define clr(a,val) memset(a,val,sizeof a)
     4 #define eps 1e-6
     5 #define RI register int
     6 typedef long long ll;
     7 const int INF=0x3f3f3f3f;
     8 const int maxn=1010;
     9 int u,v,w,tot,head[maxn],dis[maxn];
    10 int vis[maxn],temp[maxn],flag[maxn][110];
    11 struct Edge{
    12     int v,w,nxt;
    13 } edge[maxn<<2]; 
    14 inline void Init()
    15 {
    16     tot=0;clr(flag,0);
    17     clr(head,-1);
    18 }
    19 inline void addedge(int u,int v,int w)
    20 {
    21     edge[tot].v=v;
    22     edge[tot].w=w;
    23     edge[tot].nxt=head[u];
    24     head[u]=tot++;
    25 }
    26 inline void SPFA()
    27 {
    28     queue<int> q;
    29     clr(vis,0);clr(dis,INF);
    30     q.push(1); vis[1]=1;dis[1]=0;
    31     while(!q.empty())
    32     {
    33         int u=q.front(); q.pop();vis[u]=0;
    34         for(int e=head[u];e!=-1;e=edge[e].nxt)
    35         {
    36             int v=edge[e].v;
    37             if(!temp[v]&&dis[u]+edge[e].w<dis[v])
    38             {
    39                 dis[v]=dis[u]+edge[e].w;
    40                 if(!vis[v]) q.push(v),vis[v]=1; 
    41             }
    42         }
    43     }
    44 }
    45 int n,m,k,e,d,a,b,num;
    46 int cost[110][110],dp[110];
    47 int main()
    48 {
    49     Init();
    50     scanf("%d%d%d%d",&n,&m,&k,&e);
    51     for(int i=1;i<=e;++i)
    52     {
    53         scanf("%d%d%d",&u,&v,&w);
    54         addedge(u,v,w);addedge(v,u,w);
    55     }
    56     scanf("%d",&num);clr(cost,INF);
    57     for(int i=1;i<=num;++i)
    58     {
    59         scanf("%d%d%d",&d,&a,&b);
    60         for(int j=a;j<=b;++j) flag[d][j]=1;
    61     }
    62     for(int i=1;i<=n;++i)
    63     {
    64         clr(temp,0);
    65         for(int j=i;j<=n;++j) 
    66         {
    67             for(int k=1;k<=m;++k) temp[k]|=flag[k][j];
    68             SPFA();
    69             cost[i][j]=dis[m];    
    70         }
    71     }
    72     dp[0]=-k;
    73     for(int i=1;i<=n;++i)
    74     {
    75         dp[i]=INF;
    76         for(int j=0;j<i;++j) 
    77             if(cost[j+1][i]!=INF) dp[i]=min(dp[i],dp[j]+cost[j+1][i]*(i-j)+k);    
    78     }    
    79     printf("%d
    ",dp[n]);
    80     return 0;
    81 }
    82  
    View Code
  • 相关阅读:
    写给初入职场小白的建议
    Python 博客园备份迁移脚本
    x64dbg 2022 最新版编译方法
    LyScript 实现Hook隐藏调试器
    驱动开发:内核中的链表与结构体
    x64dbg 实现插件Socket反向通信
    x64dbg 插件开发SDK环境配置
    C/C++ Capstone 引擎源码编译
    2:手写 instanceof 方法
    3:zindex
  • 原文地址:https://www.cnblogs.com/csushl/p/10061635.html
Copyright © 2020-2023  润新知