2424: [HAOI2010]订货

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #define M 10000
 5 #define inf 2139062143
 6 using namespace std;
 7 int cnt=1,n,m,s,ans,T,d[M],q[2*M],f[M],head[M],next[10*M],u[10*M],v[10*M],w[10*M],fro[10*M],fr[M];
 8 void jia1(int a1,int a2,int a3,int a4)
 9 {
10     cnt++;
11     next[cnt]=head[a1];
12     head[a1]=cnt;
13     fro[cnt]=a1;
14     u[cnt]=a2;
15     v[cnt]=a3;
16     w[cnt]=a4;
17 }
18 void jia(int a1,int a2,int a3,int a4)
19 {
20     jia1(a1,a2,a3,a4);
21     jia1(a2,a1,0,-a4);
22     return;
23 }
24 bool spfa()
25 {
26     memset(d,127,sizeof(int)*(T+1));
27     memset(fr,0,sizeof(int)*(T+1));
28     memset(f,0,sizeof(int)*(T+1));
29     d[0]=0;
30     f[0]=1;
31     q[1]=0;
32     int h=0,t=1;
33     for(;h<t;)
34       {
35         h++;
36         int p=q[h];
37         f[p]=0;
38           for(int i=head[p];i;i=next[i])
39           if(v[i]&&d[u[i]]>d[p]+w[i])
40             {
41                 d[u[i]]=d[p]+w[i];
42                 fr[u[i]]=i;
43                 if(!f[u[i]])
44                   {
45                     f[u[i]]=1;
46                     t++;
47                     q[t]=u[i];
48                     }
49             }
50       }
51     if(d[T]!=inf)
52       return 1;
53     return 0;
54 }
55 void mcf()
56 {
57     int mx=inf;
58     for(int i=fr[T];i;i=fr[fro[i]])
59       mx=min(mx,v[i]);
60     for(int i=fr[T];i;i=fr[fro[i]])
61       {
62         v[i]-=mx;
63         v[i^1]+=mx;
64         ans+=mx*w[i];
65       }
66     return;
67 }
68 int main()
69 {
70     scanf("%d%d%d",&n,&m,&s);
71     T=n+1;
72     for(int i=1;i<=n;i++)
73       {
74         int a1;
75         if(i!=n)
76           jia(i,i+1,s,m);
77         scanf("%d",&a1);
78         jia(i,T,a1,0);
79       }
80     for(int i=1;i<=n;i++)
81       {
82         int a1;
83         scanf("%d",&a1);
84         jia(0,i,inf,a1);
85       }
86     for(;spfa();)
87       mcf();
88     printf("%d\n",ans);
89     return 0;
90 }
91 

网络流

你可能感兴趣的:(2424: [HAOI2010]订货)