zoukankan      html  css  js  c++  java
  • [bzoj]1003: [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
     1 #include<iostream>
     2 #include<queue>
     3 #include<cstring>
     4 using namespace std;
     5 
     6 struct Edge
     7 {
     8     int to,w,next;
     9 }E[1000];
    10 int node=0,head[50];
    11 
    12 int n,m,k,e,d;
    13 bool flag[101][21]={0};//flag表示第i天第j好港口被封锁
    14 long long cost[101][101]={0};//cost表示从第i天到第j天的最短路径
    15 long long F[101];//F表示到i天的花费
    16 
    17 void insert(int u,int v,int w)
    18 {
    19     node++;
    20     E[node]=Edge{v,w,head[u]};
    21     head[u]=node;
    22 }
    23 
    24 int spfa(int x,int y)
    25 {
    26     bool block[21],vis[21];
    27     int dist[21];
    28     memset(block,0,sizeof(block));
    29     memset(dist,0x7f,sizeof(dist));
    30     memset(vis,0,sizeof(vis));
    31     for(int i=x;i<=y;i++)
    32         for(int j=1;j<=m;j++)
    33             if(flag[i][j]) block[j]=1;
    34     dist[1]=0;vis[1]=1;
    35     queue<int> Q;
    36     Q.push(1);
    37     while(!Q.empty())
    38     {
    39         int q=Q.front();
    40         Q.pop();
    41         int i=head[q];
    42         while(i)
    43         {
    44             if(!block[E[i].to]&&dist[E[i].to]>dist[q]+E[i].w)
    45             {
    46                 dist[E[i].to]=dist[q]+E[i].w;
    47                 if(!vis[E[i].to])
    48                 {
    49                     Q.push(E[i].to);
    50                     vis[E[i].to]=1;
    51                 }
    52             }
    53             i=E[i].next;
    54         }
    55         vis[q]=0;
    56     }
    57     return dist[m];
    58 }
    59 
    60 int main()
    61 {
    62     cin>>n>>m>>k>>e;
    63     memset(F,0x7f,sizeof(F));
    64     for(int i=1;i<=e;i++)
    65     {
    66         int x,y,z;
    67         cin>>x>>y>>z;
    68         insert(x,y,z);
    69         insert(y,x,z);
    70     }
    71     cin>>d;
    72     for(int i=1;i<=d;i++)
    73     {
    74         int p,a,b;
    75         cin>>p>>a>>b;
    76         for(int j=a;j<=b;j++)
    77             flag[j][p]=1;
    78     }
    79     for(int i=1;i<=n;i++)
    80         for(int j=1;j<=n;j++)
    81             cost[i][j]=spfa(i,j);
    82     for(int i=1;i<=n;i++)//F[i]=min{F[j]+k+cost[j+1][i]*(i-j)}
    83     {
    84         F[i]=cost[1][i]*i;
    85         for(int j=1;j<i;j++)
    86             F[i]=min(F[i],F[j]+k+cost[j+1][i]*(i-j));
    87     }
    88     cout<<F[n]<<endl;
    89     return 0;
    90 }
  • 相关阅读:
    Leetcode 238. Product of Array Except Self
    Leetcode 103. Binary Tree Zigzag Level Order Traversal
    Leetcode 290. Word Pattern
    Leetcode 205. Isomorphic Strings
    Leetcode 107. Binary Tree Level Order Traversal II
    Leetcode 102. Binary Tree Level Order Traversal
    三目运算符
    简单判断案例— 分支结构的应用
    用switch判断月份的练习
    java基本打印练习《我行我素购物系统》
  • 原文地址:https://www.cnblogs.com/InWILL/p/5962532.html
Copyright © 2011-2022 走看看