bzoj2245: [SDOI2011]工作安排

来源:互联网 发布:dede企业网站源码 编辑:程序博客网 时间:2024/05/21 21:01

费用流模板,调了半天把ans改成 long long就过了什么鬼…

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;queue<int> q;const int inf=0x7f7f7f7f;int read(){    char ch=getchar();int f=0;    while(ch<'0'||ch>'9')ch=getchar();    while(ch>='0'&&ch<='9'){f=(f<<1)+(f<<3)+ch-'0';ch=getchar();}    return f;}int n,m,head[505],a[505],s,t,prep[505],pree[505],dis[505];bool vis[505];long long ans;struct node{    int from;    int to;    int next;    int w;    int cost;}edge[200005];int tot,T[15],W[15];void add(int u,int v,int w,int cost){    edge[tot].from=u;    edge[tot].to=v;    edge[tot].w=w;    edge[tot].cost=cost;    edge[tot].next=head[u];    head[u]=tot++;}bool spfa(){    q.push(s);    memset(dis,0x7f7f7f7f,sizeof(dis));    dis[s]=0;    while(!q.empty())    {        int x=q.front();        q.pop();        vis[x]=0;        for(int i=head[x];i!=-1;i=edge[i].next)        {            if(edge[i].w&&dis[edge[i].to]>dis[x]+edge[i].cost)            {                dis[edge[i].to]=dis[x]+edge[i].cost;                prep[edge[i].to]=x;                pree[edge[i].to]=i;                if(!vis[edge[i].to])                {                    vis[edge[i].to]=1;                    q.push(edge[i].to);                }            }        }    }    if(dis[t]!=0x7f7f7f7f)    return 1;    return 0;}void mcmf(){    while(spfa())    {        int now=t,flow=0x7f7f7f7f;        while(now!=s)        {            flow=min(flow,edge[pree[now]].w);            now=prep[now];        }        now=t;        while(now!=s)        {            edge[pree[now]].w-=flow;            edge[pree[now]^1].w+=flow;            ans+=1LL*flow*edge[pree[now]].cost;            now=prep[now];        }    }}int main(){    memset(head,-1,sizeof(head));    m=read(),n=read();    s=0,t=m+n+1;    for(int i=1;i<=n;i++)    {        a[i]=read();        add(i+m,t,a[i],0);        add(t,i+m,0,0);    }    for(int i=1;i<=m;i++)    {        for(int j=1;j<=n;j++)        {            int x=read();            if(x)            {                add(i,j+m,inf,0);                add(j+m,i,0,0);            }        }    }    for(int i=1;i<=m;i++)    {        int x=read(),cost;        for(int j=1;j<=x;j++)        T[j]=read();        for(int j=1;j<=x;j++)        {            cost=read();            add(s,i,T[j]-T[j-1],cost);            add(i,s,0,-cost);        }        cost=read();        add(s,i,inf,cost);        add(i,s,0,-cost);    }    mcmf();    cout<<ans;}
原创粉丝点击