标签:txt front lld out display push class 设计 ane
餐巾计划问题
Time Limit: 1000 MS Memory Limit: 65536 KB
#include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #include<algorithm> #include<cstring> #include<queue> #include<stack> #include<ctime> #include<vector> using namespace std; int n,m; struct node { int next,to; long long cap,cost; }edge[400001]; int sze=1,head[5001]; long long a[5001]; void putin(int from,int to,long long cap,long long cost) { sze++; edge[sze].to=to; edge[sze].cap=cap; edge[sze].cost=cost; edge[sze].next=head[from]; head[from]=sze; } void in(int from,int to,long long cap,long long cost) { putin(from,to,cap,cost); putin(to,from,0,-cost); } long long ans,f[5001]; int pre[5001],vis[5001]; bool bfs(int src,int des) { int i; for(i=0;i<=n*2+1;i++){f[i]=1e18;vis[i]=0;} queue<int>mem; mem.push(src); f[src]=0;vis[i]=1; while(!mem.empty()) { int x=mem.front();mem.pop(); vis[x]=0; for(i=head[x];i!=-1;i=edge[i].next) { int y=edge[i].to; if(edge[i].cap&&f[y]>f[x]+edge[i].cost) { f[y]=f[x]+edge[i].cost; pre[y]=i; if(!vis[y]){mem.push(y);vis[y]=1;} } } } if(f[des]==1e18)return 0; else return 1; } void change(int src,int des) { int x=des; long long flow=1e18; while(x!=src) { flow=min(flow,edge[pre[x]].cap); x=edge[pre[x]^1].to; } x=des; while(x!=src) { ans+=flow*edge[pre[x]].cost; edge[pre[x]].cap-=flow; edge[pre[x]^1].cap+=flow; x=edge[pre[x]^1].to; } } void mincostflow(int src,int des) { while(bfs(src,des))change(src,des); } int main() { int i,j; scanf("%d",&n); memset(head,-1,sizeof(head)); long long s,d1,c1,d2,c2; scanf("%lld%lld%lld%lld%lld",&s,&d1,&c1,&d2,&c2); for(i=1;i<=n;i++)scanf("%lld",&a[i]); for(i=1;i<=n;i++) { if(i>=2)in(i+n-1,i+n,2e8,0); in(0,i,a[i],0); in(0,n+i,a[i],s); if(i+d1<=n)in(i,i+n+d1,a[i],c1); if(i+d2<=n)in(i,i+n+d2,a[i],c2); in(n+i,n*2+1,a[i],0); } mincostflow(0,n*2+1); cout<<ans; return 0; }
标签:txt front lld out display push class 设计 ane
原文地址:http://www.cnblogs.com/huangdalaofighting/p/6942064.html