Concert Hall Scheduling UVALive

来源:互联网 发布:量化交易算人工智能吗 编辑:程序博客网 时间:2024/05/29 10:27

Concert Hall Scheduling UVALive - 2796

网络流·费用流

题目大意:

一个著名的音乐厅因为财务状况恶化快要破产,你临危受命,试图通过管理的手段来拯救它,方法之一就是优化演出安排,既聪明的决定接受或拒绝哪些乐团的演出申请,使得音乐厅的收益最大化。该音乐厅有两个完全相同的房间,因此个乐团在申请演出的时候并不会指定房间,你只需要随便分配一个即可。每个演出都会持续若干天,每个房间每天只能举行一场演出。申请数目n为不超过100的正整数,每个申请用3个整数i,j,w来表示,表示从第i天到第j天,愿意支付w元。

题解:

设置366个结点,表示365天(结点1到结点2表示第一天)。设置超级源点,连向第一个结点,容量为2(两个音乐厅),费用为0。设置超级汇点,使结点366连向它,容量为2,费用为0。然后,前365个结点,每个节点连向他的下一个节点,容量为2,费用为0,表示时间的流逝。最后对于每个申请,i到j + 1连一条边,容量为1,费用为负的w。跑费用流。

Code:

#include <iostream>#include <cstdio>#include <cstring>#include <queue>#define D(x) cout<<#x<<" = "<<x<<"  "#define E cout<<endlusing namespace std;const int N = 505;const int M = 10005;const int INF = 0x3f3f3f3f;int n,S,T;struct Edge{    int from,to,next,cap,flow,cost;}e[M*2];int head[N],ec=1;void clear(){ memset(head,0,sizeof(head)); ec=1; }void add(int a,int b,int cap,int cost){    ec++; e[ec].from=a; e[ec].to=b;     e[ec].next=head[a]; head[a]=ec;    e[ec].cap=cap; e[ec].flow=0; e[ec].cost=cost;}void add2(int a,int b,int cap,int cost){//  D(a); D(b); D(cap); D(cost); E;    add(a,b,cap,cost); add(b,a,0,-cost);}bool vis[N]; int d[N],pre[N];bool spfa(){    memset(vis,false,sizeof(vis));    memset(d,0x3f,sizeof(d));    queue<int> q; q.push(S); vis[S]=true; d[S]=0;    while(!q.empty()){        int u=q.front(); q.pop(); vis[u]=false;        for(int i=head[u];i;i=e[i].next){            int v=e[i].to;            if(e[i].cap>e[i].flow && d[v]>d[u]+e[i].cost){                d[v]=d[u]+e[i].cost; pre[v]=i;                if(!vis[v]){ vis[v]=true; q.push(v); }            }         }    }    return d[T]!=INF;}int mxf(){    int cost=0, flow=0, a;    while(spfa()){        a=INF;        for(int pos=T,i=pre[T]; pos!=S; pos=e[i].from,i=pre[pos]){            a=min(a,e[i].cap-e[i].flow);        }        for(int pos=T,i=pre[T]; pos!=S; pos=e[i].from,i=pre[pos]){            e[i].flow+=a; e[i^1].flow-=a; cost+=e[i].cost*a;        }        flow+=a;    }    return cost;}int main(){    freopen("a.in","r",stdin);    while(~scanf("%d",&n) && n){        clear();        S=400; T=S+1;        int a,b,c;        for(int i=1;i<=n;i++){            scanf("%d%d%d",&a,&b,&c);            add2(a,b+1,1,-c);        }        for(int i=1;i<=365;i++){            add2(i,i+1,2,0);        }        add2(S,1,2,0); add2(366,T,2,0);        int cost=-mxf();        printf("%d\n",cost);    }}
原创粉丝点击