您的位置:首页 > 其它

【费用流】【bzoj 2245】: [SDOI2011]工作安排

2015-03-20 16:32 309 查看
学习zkw费用流之后发现不会写普通的了

正好翻代码的时候看到了这个。。。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i,l,r) for(int i=(l),___t=(r);i<=___t;i++)
#define per(i,r,l) for(int i=(r),___t=(l);i>=___t;i--)
#define MS(arr,x) memset(arr,x,sizeof(arr))
#define LL long long
#define INE(i,u,e) for(int i=head[u];~i;i=e[i].next)
inline const int read()
{int r=0,k=1;char c=getchar();for(;c<'0'||c>'9';c=getchar())if(c=='-')k=-1;
for(;c>='0'&&c<='9';c=getchar())r=r*10+c-'0';return k*r;}
/////////////////////////////////////////////////
const int inf=0x3f3f3f3f;
const int N=600;
const int M=400000;
int m,n;
bool A[255][255];
int t[255],w[255];
struct edge{int v,flow,cap,cost,next;}e[M];
int head
,k;
int S,T;
int dis
,pre
;
/////////////////////////////////////////////////
void adde(int u,int v,int f,int c){e[k].v=v;e[k].flow=0;e[k].cap=f;e[k].cost=c;e[k].next=head[u];head[u]=k++;}
void ins(int u,int v,int f1,int f2,int c){adde(u,v,f1,c);adde(v,u,f2,-c);}
inline LL F(LL i){return e[i].cap-e[i].flow;}
inline void MIN(LL &a,LL b){if(a>b)a=b;}
bool spfa()
{
static queue<int>q;
static bool inq
={0};
MS(dis,inf); dis[S]=0; q.push(S);
while(!q.empty())
{
int u=q.front(); q.pop(); inq[u]=0;
INE(i,u,e) if(F(i))
{
int v=e[i].v;
if(dis[v]>dis[u]+e[i].cost)
{
dis[v]=dis[u]+e[i].cost;
pre[v]=i;
if(!inq[v]) q.push(v),inq[v]=1;
}
}
}
return dis[T]!=inf;
}
LL MCMF()
{
LL maxf=0,minc=0;
while(spfa())
{
LL mi=inf;
for(int i=T;i^S;i=e[pre[i]^1].v)
MIN(mi,F(pre[i]));
for(int i=T;i^S;i=e[pre[i]^1].v)
e[pre[i]].flow+=mi,e[pre[i]^1].flow-=mi;
maxf+=mi; minc+=mi*dis[T];
}
return minc;
}
/////////////////////////////////////////////////
void input()
{
MS(head,-1);
m=read(); n=read();
S=0; T=m+n+1;
rep(i,1,n)
{
int c=read();
ins(m+i,T,c,0,0);
}
rep(i,1,m) rep(j,1,n) if(read()) ins(i,m+j,inf,0,0);
rep(i,1,m)
{
int s=read();
rep(j,1,s) t[j]=read();
t[s+1]=inf;
rep(j,1,s+1) w[j]=read();
rep(j,1,s+1) ins(S,i,t[j]-t[j-1],0,w[j]);
}
}
void solve()
{
cout<<MCMF()<<endl;
}
/////////////////////////////////////////////////
int main()
{
input(),solve();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: