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

时间:2023-12-10 22:03:02

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,因为开始时多加了一次。

代码:

 /**************************************************************
Problem: 1003
User: shadowland
Language: C++
Result: Accepted
Time:52 ms
Memory:7424 kb
****************************************************************/ #include "bits/stdc++.h"
#define INF (2147483647) using namespace std ;
const int maxN = ;
struct Path{int to , val , next;};
typedef long long QAQ ;
inline int gmin ( int x , int y ) {return x < y ? x : y ; } Path e[ maxN<<<< ] ; int F[ maxN ] , Dis[ maxN ] , head[ maxN ] , cost[ maxN ][ maxN ] , In[ maxN ] ;
bool visited[ maxN ] , crash[ maxN ] , target[ maxN ][ maxN ] ; int INPUT ( ){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} int cnt = ; inline void DP_Init_1 ( int i , int j , int M ){
cost [ i ][ j ] = Dis[ M ] * ( Dis[M]>=0x3f3f3f3f ? : j - i + ) ;
}
void DP_Init_2 ( ){
memset(F,0x3f3f3f3f,sizeof(F));
F[]=;
}
void DP ( int N ,int _k) {
for ( int i= ; i<=N ; ++i ) {
for ( int j= ; j<i ; ++j ) {
F[i]=gmin(F[i],F[j]+cost[j+][i]+_k);
}
}
return ;
} void Add_Edge ( const int x , const int y , const int _val ) {
e[ ++cnt ].to = y ;
e[ cnt ].val = _val ;
e[ cnt ].next = head[ x ] ;
head[ x ] = cnt ;
} bool SPFA ( const int S , const int N ) {
int t , temp ;
queue <int> Q;
memset( visited , , sizeof ( visited ) ) ;
memset( Dis , 0x3f3f3f3f , sizeof ( Dis ) ) ;
memset( In , , sizeof ( In ) ) ;
Q.push( S ) ;
visited[ S ] = true ;
Dis[ S ] = ;
while( !Q.empty( ) ) {
t = Q.front( ) ;Q.pop( ) ;visited[ t ] = false ;
for ( int i=head[t] ; i ; i = e[ i ] . next ) {
temp = e[ i ].to;
if ( crash [ temp ] ) continue ;
if ( Dis[ temp ] >Dis[ t ] + e[ i ].val ) {
Dis[ temp ] = Dis[ t ] + e[ i ] . val ;
if( !visited[ temp ] ) {
Q.push( temp ) ;
visited[ temp ] = true ;
if( ++In[temp] > N ) return false ;
} }
}
}
return true ;
}
void DEBUG_ ( int n ) {
for ( int i= ; i<=n ; ++i ) {
printf ("%d ",F[i]);
}
putchar('\n');
}
void DEBUG__ ( int n ) {
for ( int i= ; i<=n ; ++i ) {
for ( int j= ; j<=n ; ++j ) {
printf ("%d ",cost[ i ][ j ]);
}
putchar('\n');
}
putchar('\n');
}
int main ( ) {
int N , M , K , E ;
N = INPUT ( ) ;M = INPUT ( ) ;K = INPUT ( ) ;E = INPUT ( ) ;
for ( int i= ; i<=E ; ++i ) {
int _x , _y , _val ;
_x = INPUT ( );_y = INPUT ( ) ; _val = INPUT ( ) ;
Add_Edge ( _x , _y , _val ) ;
Add_Edge ( _y , _x , _val ) ;
}
int D = INPUT ( ) ;
for ( int i= ; i<=D ; ++i ){
int from = INPUT ( ) ; int start = INPUT ( ) ; int end = INPUT ( ) ;
for ( int j=start ; j<=end ; ++j )
target[ from ][ j ] = true ;
}
for ( int i= ; i<=N ; ++i ) {
for ( int j=i ; j<=N ; ++j ) {
memset ( crash , false , sizeof ( crash ) ) ;
for ( int k= ; k<M ;++k ){
for ( int q=i ; q<=j ; ++q ) {
if ( target [ k ][ q ] ){
crash[k]=true;
break;
}
} }
SPFA( , N ) ;
DP_Init_1 ( i , j , M ) ;
}
}
DP_Init_2 ( ) ;
DP ( N , K ) ;
//DEBUG_( N );
//DEBUG__( N );
printf("%d",F[N]-K);
return ;
}

2016-10-01 00:49:40

(完)