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

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1003

    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*改变运输路线的次数。

    数据好”强“……

    用最短路处理出第 i 天到第 j 天的最短路,保存在 cost 数组中,然后按时间DP,dp[i] = min( dp[i], dp[j] + cost[j+1][i] + K )

    这数据范围怎么搞都行吧

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <queue>
     6 #define rep(i,l,r) for(int i=l; i<=r; i++)
     7 #define clr(x,y) memset(x,y,sizeof(x))
     8 #define travel(x) for(Edge *p=last[x]; p; p=p->pre)
     9 using namespace std;
    10 const int INF = 0x3f3f3f3f;
    11 const int maxn = 25;
    12 inline int read(){
    13     int ans = 0, f = 1;
    14     char c = getchar();
    15     for(; !isdigit(c); c = getchar())
    16     if (c == '-') f = -1;
    17     for(; isdigit(c); c = getchar())
    18     ans = ans * 10 + c - '0';
    19     return ans * f;
    20 }
    21 int n,m,P,K,x,y,e,z,D,d[maxn],cost[110][110],dp[110];
    22 bool blocked[maxn][110],invalid[maxn];
    23 struct Edge{
    24     Edge *pre;
    25     int to,cost;
    26 }edge[10010];
    27 Edge *last[maxn],*pt = edge;
    28 struct Node{
    29     int x,d;
    30     Node(int _x,int _d) : x(_x), d(_d){}
    31     inline bool operator < (const Node &_Tp) const {
    32         return d > _Tp.d;
    33     }
    34 };
    35 priority_queue <Node> q;
    36 inline void addedge(int x,int y,int z){
    37     pt->pre = last[x]; pt->to = y; pt->cost = z; last[x] = pt++;
    38 }
    39 void dijkstra(){
    40     clr(d,INF); d[1] = 0; q.push(Node(1,0));
    41     while (!q.empty()){
    42         Node now = q.top(); q.pop();
    43         if (d[now.x] != now.d) continue;
    44         travel(now.x){
    45             if (invalid[p->to]) continue;
    46             if (d[p->to] > d[now.x] + p->cost){
    47                 d[p->to] = d[now.x] + p->cost;
    48                 q.push(Node(p->to,d[p->to]));
    49             }
    50         }
    51     }
    52 }
    53 int main(){
    54     n = read(); m = read(); K = read(); e = read(); clr(last,0);
    55     rep(i,1,e){
    56         x = read(); y = read(); z = read();
    57         addedge(x,y,z); addedge(y,x,z);
    58     }
    59     D = read(); clr(blocked,0);
    60     rep(i,1,D){
    61         P = read(); x = read(); y = read();
    62         rep(j,x,y) blocked[P][j] = 1;
    63     }
    64     clr(cost,INF);
    65     rep(i,1,n){
    66         rep(j,i,n){
    67             clr(invalid,0);
    68             rep(k,2,m-1) rep(l,i,j)
    69             if (blocked[k][l]) invalid[k] = 1;
    70             dijkstra();
    71             if (d[m] < INF) cost[i][j] = d[m] * (j-i+1);
    72         }
    73     }
    74     clr(dp,INF); dp[0] = 0;
    75     rep(i,1,n) rep(j,0,i-1) dp[i] = min(dp[i],dp[j] + cost[j+1][i] + K);
    76     printf("%d
    ",dp[n] - K);
    77     return 0;
    78 }
    View Code
  • 相关阅读:
    RE
    【LeetCode】198. House Robber
    【LeetCode】053. Maximum Subarray
    【LeetCode】152. Maximum Product Subarray
    【LeetCode】238.Product of Array Except Self
    【LeetCode】042 Trapping Rain Water
    【LeetCode】011 Container With Most Water
    【LeetCode】004. Median of Two Sorted Arrays
    【LeetCode】454 4Sum II
    【LeetCode】259 3Sum Smaller
  • 原文地址:https://www.cnblogs.com/jimzeng/p/bzoj1003.html
Copyright © 2011-2022 走看看