BZOJ 1003 物流运输 题解 【SPFA+DP】

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

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

———————————————————————分割线———————————————————————

这道题看似不太好做,但是注意到n(1<=n<=100)、m(1<=m<=20)的数据范围,不难想到一些奇怪的乱搞方法。

首先,暴逆 暴力枚举从第 i 天到第 j 天不改路线,走同一路径最小成本 cost( i , j ) , 这里使用SPFA解决,即最短路乘以天数。

接下来就是DP,方程如下:

f( i ) = min { f( i ) , f( j ) + cost( i , j ) + K }  

[ATTENTION]:最终的答案一定要减去K,因为开始时多加了一次。

代码:

  1 /**************************************************************
  2     Problem: 1003
  3     User: shadowland
  4     Language: C++
  5     Result: Accepted
  6     Time:52 ms
  7     Memory:7424 kb
  8 ****************************************************************/
  9  
 10 #include "bits/stdc++.h"
 11 #define INF  (2147483647)
 12   
 13 using namespace std ;
 14 const int maxN = 1100 ;
 15 struct Path{int to , val , next;};
 16 typedef long long QAQ ;
 17 inline int gmin ( int x , int y ) {return x < y ? x : y ; }
 18   
 19 Path e[ maxN<<3<<1 ] ; 
 20   
 21 int F[ maxN ] , Dis[ maxN ] , head[ maxN ] , cost[ maxN ][ maxN ] , In[ maxN ] ;
 22 bool visited[ maxN ] , crash[ maxN ] , target[ maxN ][ maxN ] ;
 23   
 24 int INPUT ( ){
 25          int x=0,f=1;char ch=getchar();
 26          while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 27          while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
 28          return x*f;
 29 }
 30   
 31 int cnt = 0 ;
 32   
 33 inline void DP_Init_1 ( int i , int j , int M ){
 34          cost [ i ][ j ] = Dis[ M ] * ( Dis[M]>=0x3f3f3f3f ? 1 : j - i + 1 ) ;
 35 }
 36 void DP_Init_2 ( ){
 37          memset(F,0x3f3f3f3f,sizeof(F));
 38          F[0]=0;
 39 }
 40 void DP ( int N ,int _k) {
 41          for ( int i=1 ; i<=N ; ++i ) {
 42                for ( int j=0 ; j<i ; ++j ) {
 43                        F[i]=gmin(F[i],F[j]+cost[j+1][i]+_k);
 44                }
 45          } 
 46          return ;
 47 }
 48   
 49 void Add_Edge ( const int x , const int y , const int _val ) {
 50          e[ ++cnt ].to = y ;
 51          e[ cnt ].val = _val ;
 52          e[ cnt ].next = head[ x ] ;
 53          head[ x ] = cnt ;
 54 }
 55   
 56 bool    SPFA ( const int S , const int N ) {
 57         int  t , temp ;
 58         queue <int>    Q;
 59         memset( visited , 0, sizeof ( visited ) ) ; 
 60         memset( Dis , 0x3f3f3f3f , sizeof ( Dis ) ) ; 
 61         memset( In , 0 , sizeof ( In ) ) ;
 62         Q.push( S ) ;
 63         visited[ S ] = true ;
 64         Dis[ S ] = 0 ;
 65         while( !Q.empty( ) ) {
 66                  t = Q.front( ) ;Q.pop( ) ;visited[ t ] = false ;
 67                  for ( int i=head[t] ; i ; i = e[ i ] . next ) {
 68                          temp = e[ i ].to;
 69                          if ( crash [ temp ] ) continue ;
 70                          if ( Dis[ temp ] >Dis[ t ] + e[ i ].val ) {
 71                                  Dis[ temp ] = Dis[ t ] + e[ i ] . val  ;
 72                                  if( !visited[ temp ] ) {
 73                                          Q.push( temp ) ;
 74                                          visited[ temp ] = true ;
 75                                          if( ++In[temp] > N ) return false ;
 76                                  }
 77                      
 78                          }
 79                  }
 80         }
 81         return true ;
 82 }
 83 void DEBUG_ ( int n ) {
 84         for ( int i=1 ; i<=n ; ++i ) {
 85                 printf ("%d ",F[i]);
 86         }
 87         putchar('
');
 88 }
 89 void DEBUG__ ( int n ) {
 90         for ( int i=1 ; i<=n ; ++i ) {
 91                 for ( int j=1 ; j<=n ; ++j ) {
 92                         printf ("%d ",cost[ i ][ j ]);
 93                 }
 94                 putchar('
');
 95         }
 96         putchar('
');
 97 }
 98 int main ( ) {
 99     int N , M , K , E ; 
100     N = INPUT ( ) ;M = INPUT ( ) ;K = INPUT ( ) ;E = INPUT ( ) ;
101     for ( int i=1 ; i<=E ; ++i ) {
102             int _x , _y , _val ;
103             _x = INPUT ( );_y = INPUT ( ) ; _val = INPUT ( ) ;
104             Add_Edge ( _x , _y , _val ) ;
105             Add_Edge ( _y , _x , _val ) ;
106     }
107     int D = INPUT ( ) ;
108     for ( int i=1 ; i<=D ; ++i ){
109             int from = INPUT ( ) ; int start = INPUT ( ) ; int end = INPUT ( ) ;
110             for ( int j=start ; j<=end ; ++j )
111                     target[ from ][ j ] = true ;
112     }
113     for ( int i=1 ; i<=N ; ++i ) {
114             for ( int j=i ; j<=N ; ++j ) {
115                     memset ( crash , false , sizeof ( crash ) ) ;
116                     for ( int k=2 ; k<M ;++k ){
117                             for ( int q=i ; q<=j ; ++q ) {
118                                     if ( target [ k ][ q ] ){
119                                             crash[k]=true;
120                                             break;
121                                     }
122                             }
123                               
124                     }
125                     SPFA( 1 , N ) ;
126                     DP_Init_1 ( i , j , M ) ;
127             }
128     }
129     DP_Init_2 ( ) ;
130     DP ( N , K ) ;
131     //DEBUG_( N ); 
132     //DEBUG__( N ); 
133     printf("%d",F[N]-K);
134     return 0;
135 }

2016-10-01 00:49:40

(完)

原文地址:https://www.cnblogs.com/shadowland/p/5925293.html