bzoj 2424(费用流)

2424: [HAOI2010]订货

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 738  Solved: 505
[ Submit][ Status][ Discuss]

Description

某公司估计市场在第i个月对某产品的需求量为Ui,已知在第i月该产品的订货单价为di,上个月月底未销完的单位产品要付存贮费用m,假定第一月月初的库存量为零,第n月月底的库存量也为零,问如何安排这n个月订购计划,才能使成本最低?每月月初订购,订购后产品立即到货,进库并供应市场,于当月被售掉则不必付存贮费。假设仓库容量为S。

Input

第1行:n, m, S (0<=n<=50, 0<=m<=10, 0<=S<=10000)
第2行:U1 , U2 , ... , Ui , ... , Un (0<=Ui<=10000)
第3行:d1 , d2 , ..., di , ... , dn (0<=di<=100)

Output

只有1行,一个整数,代表最低成本

Sample Input

3 1 1000
2 4 8
1 2 4

Sample Output

34


解题思路:费用流。。 没看出来。 

建图:对于每个点连一条到汇点的边,容量为u[i],这样可以保障每个月减去u[i]。 

起点到每个点也建一条费用为d[i]的边进行补充。然后每个点与它后面的点连一条边,

费用为m。


C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<iostream>
#include<cstdio>
#define inf 0x7fffffff
#define T 2001
using namespace std ;
int n , m , s ;
int cnt = 1 , ans ;
int from [ 2005 ] , q [ 2005 ] , dis [ 2005 ] , head [ 2005 ] ;
bool inq [ 2005 ] ;
struct data { int from , to , next , v , c ; } e [ 1000001 ] ;
void ins ( int u , int v , int w , int c )
{
     cnt ++ ;
     e [ cnt ] . from = u ; e [ cnt ] . to = v ;
     e [ cnt ] . v = w ; e [ cnt ] . c = c ;
     e [ cnt ] . next = head [ u ] ; head [ u ] = cnt ;
}
void insert ( int u , int v , int w , int c )
{ ins ( u , v , w , c ) ; ins ( v , u , 0 , - c ) ; }
bool spfa ( )
{
     for ( int i = 0 ; i <= T ; i ++ ) dis [ i ] = inf ;
     int t = 0 , w = 1 , i , now ;
     dis [ 0 ] = q [ 0 ] = 0 ; inq [ 0 ] = 1 ;
     while ( t != w )
     {
         now = q [ t ] ; t ++ ; if ( t == 2001 ) t = 0 ;
         for ( int i = head [ now ] ; i ; i = e [ i ] . next )
         {
             if ( e [ i ] . v && dis [ e [ i ] . to ] > dis [ now ] + e [ i ] . c )
             {
                 from [ e [ i ] . to ] = i ;
                 dis [ e [ i ] . to ] = dis [ now ] + e [ i ] . c ;
                 if ( ! inq [ e [ i ] . to ] )
                 {
                     inq [ e [ i ] . to ] = 1 ;
                     q [ w ++ ] = e [ i ] . to ;
                     if ( w == 2001 ) w = 0 ;
                 }
             }
         }
         inq [ now ] = 0 ;
     }
     if ( dis [ T ] == inf ) return 0 ; return 1 ;
}
void mcf ( )
{
     int i , x = inf ;
     i = from [ T ] ;
     while ( i )
     {
         x = min ( e [ i ] . v , x ) ;
         i = from [ e [ i ] . from ] ;
     }
     i = from [ T ] ;
     while ( i )
     {
         e [ i ] . v -= x ;
         e [ i ^ 1 ] . v += x ;
         ans += x * e [ i ] . c ;
         i = from [ e [ i ] . from ] ;
     }
}
int main ( )
{
scanf ( "%d%d%d" , &n , &m , &s ) ;
for ( int i = 1 ; i <= n ; i ++ )
{
int x ; scanf ( "%d" , &x ) ;
insert ( i , T , x , 0 ) ;
}
for ( int i = 1 ; i <= n ; i ++ )
{
int x ; scanf ( "%d" , &x ) ;
insert ( 0 , i , inf , x ) ;
}
for ( int i = 1 ; i < n ; i ++ ) insert ( i , i + 1 , s , m ) ;
while ( spfa ( ) ) mcf ( ) ;
printf ( "%d" , ans ) ;
return 0 ;
}

你可能感兴趣的:(bzoj 2424(费用流))