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

    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

    正解:spfa+划分型dp。

    f[i]=max(f[j]+dis[j+1..i]),dis为最短路,所以这就是n^2遍spfa。


    //It is made by wfj_2048~
    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <cstdio>
    #include <vector>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #define inf (1LL<<40)
    #define il inline
    #define RG register
    #define ll long long
     
    using namespace std;
     
    struct edge{ ll nt,to,dis; }g[1000010];
     
    ll no[110][110],f[110],head[30],cnt[30],dis[30],q[1000010],n,m,k,e,d,num;
     
    il ll gi(){
        RG ll x=0,q=0; RG char ch=getchar(); while ((ch<'0' || ch>'9') && ch!='-') ch=getchar();
        if (ch=='-') q=1,ch=getchar(); while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar(); return q ? -x : x;
    }
     
    il void insert(RG ll from,RG ll to,RG ll dis){ g[++num]=(edge){head[from],to,dis},head[from]=num; return; }
     
    il ll spfa(){
        RG ll h=0,t=1; for (RG int i=2;i<=m;++i) dis[i]=inf; dis[1]=0,q[t]=1;
        while (h<t){
        RG ll x=q[++h];
        for (RG ll i=head[x];i;i=g[i].nt){
            RG ll v=g[i].to; if (cnt[v]) continue;
            if (dis[v]>dis[x]+g[i].dis) q[++t]=v,dis[v]=dis[x]+g[i].dis;
        }
        }
        return dis[m];
    }
     
    il void work(){
        n=gi(),m=gi(),k=gi(),e=gi(); for (RG ll i=1;i<=e;++i){ RG ll u=gi(),v=gi(),w=gi(); insert(u,v,w),insert(v,u,w); }
        d=gi(); for (RG ll i=1;i<=d;++i){ RG ll id=gi(),a=gi(),b=gi(); for (RG ll j=a;j<=b;++j) no[id][j]=1; }
        for (RG ll i=1;i<=n;++i){
        memset(cnt,0,sizeof(cnt)); for (RG ll p=1;p<=m;++p) for (RG ll q=1;q<=i;++q) if (no[p][q]){ cnt[p]=1; break; }
        RG ll l=spfa(); if (l<inf) f[i]=i*l; else f[i]=inf;
        for (RG ll j=1;j<i;++j){
            memset(cnt,0,sizeof(cnt)); for (RG ll p=1;p<=m;++p) for (RG ll q=j+1;q<=i;++q) if (no[p][q]){ cnt[p]=1; break; }
            RG ll l=spfa(); if (l<inf) f[i]=min(f[i],f[j]+(i-j)*l+k);
        }
        }
        printf("%lld
    ",f[n]); return;
    }
     
    int main(){
        work();
        return 0;
    }

  • 相关阅读:
    自定义udf添加一列
    spark执行命令 监控执行命令
    R链接hive/oracle/mysql
    [Hive_6] Hive 的内置函数应用
    [Hive_add_6] Hive 实现 Word Count
    [Hive_add_5] Hive 的 join 操作
    【爬坑】远程连接 MySQL 失败
    [Hive_add_4] Hive 命令行客户端 Beeline 的使用
    [Hive_5] Hive 的 JDBC 编程
    [Hive_add_3] Hive 进行简单数据处理
  • 原文地址:https://www.cnblogs.com/wfj2048/p/6416581.html
Copyright © 2011-2022 走看看