P2517-订货【网络流,费用流】
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                P2517-订货【网络流,费用流】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                正題
AC鏈接: 
 https://www.luogu.org/record/show?rid=7949532
大意
有n個月,每個月商品價格di,需求量Ui。有容量為S的倉庫,一個商品匯存一個月要m。求最低成本
解題思路
首先是月份做為點,成本作為費用,需求作為容量。 
 剛開始想的是專門做一個點作為倉庫,后來發現其實可以直接向下連邊。 
 然后就是這樣構圖: 
  
 然后求費用流就好了
代碼
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct line{int to,w,c,next; }a[2001]; int tot,n,m,s,t,f[601],ls[601],tail,answer; int state[601],x,y,w,c,ans,head,pre[601],S; bool v[601]; void addl(int x,int y,int w,int c) {a[++tot].to=y;a[tot].w=w;a[tot].c=c;a[tot].next=ls[x];ls[x]=tot;a[++tot].to=x;a[tot].w=0;a[tot].c=-c;a[tot].next=ls[y];ls[y]=tot; } bool spfa() {memset(f,127/3,sizeof(f));memset(v,0,sizeof(v));head=0;tail=1;v[s]=true;state[1]=s;f[s]=0;while (head!=tail){head=head%500+1;int x=state[head];for (int q=ls[x];q;q=a[q].next){int y=a[q].to;if (a[q].w&&f[x]+a[q].c<f[y]){f[y]=f[x]+a[q].c;pre[y]=q;if (!v[y]){v[y]=true;tail=tail%500+1;state[tail]=y;}}}v[x]=false;}if (f[t]>=707406378) return 0;else return 1; } void upway() {int k=2147483647,now=t;while (now!=s){k=min(k,a[pre[now]].w);now=a[pre[now]^1].to;}ans+=f[t]*k;now=t;answer+=k;while (now!=s){a[pre[now]].w-=k;a[pre[now]^1].w+=k;now=a[pre[now]^1].to;} } int main() {tot=1;scanf("%d%d%d",&n,&m,&S);s=n+1;t=n+2;for (int i=1;i<=n;i++)scanf("%d",&x),addl(i,t,x,0);//完成訂單for (int i=1;i<=n;i++)scanf("%d",&x),addl(s,i,1e9,x);//進貨for (int i=1;i<n;i++)addl(i,i+1,S,m);//存while (spfa()){upway();}printf("%d",ans); }總結
以上是生活随笔為你收集整理的P2517-订货【网络流,费用流】的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: P2053-修车【网络流,费用流】
- 下一篇: tcl空调故障代码 tcl空调故障代码介
