hdu-6437-最大费用流

Problem L.Videos

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 492    Accepted Submission(s): 239


Problem Description
C-bacteria takes charge of two kinds of videos: ’The Collection of Silly Games’ and ’The Collection of Horrible Games’.
For simplicity’s sake, they will be called as videoA and videoB.
There are some people who want to watch videos during today, and they will be happy after watching videos of C-bacteria.
There are n hours a day, m videos are going to be show, and the number of people is K.
Every video has a type(videoA or videoB), a running time, and the degree of happi- ness after someone watching whole of it.
People can watch videos continuous(If one video is running on 2pm to 3pm and another is 3pm to 5pm, people can watch both of them).
But each video only allows one person for watching.
For a single person, it’s better to watch two kinds to videos alternately, or he will lose W happiness.
For example, if the order of video is ’videoA, videoB, videoA, videoB, …’ or ’B, A, B, A, B, …’, he won’t lose happiness; But if the order of video is ’A, B, B, B, A, B, A, A’, he will lose 3W happiness.
Now you have to help people to maximization the sum of the degree of happiness.
 

 

Input
Multiple query.
On the first line, there is a positive integer T, which describe the number of data. Next there are T groups of data.
for each group, the first line have four positive integers n, m, K, W : n hours a day, m videos, K people, lose W happiness when watching same videos).
and then, the next m line will describe m videos, four positive integers each line S, T, w, op : video is the begin at S and end at T, the happiness that people can get is w, and op describe it’s tpye(op=0 for videoA and op=1 for videoB).
There is a blank line before each groups of data.
T<=20, n<=200, m<=200, K<=200, W<=20, 1<=S<T<=n, W<=w<=1000,
op=0 or op=1
 

 

Output
Your output should include T lines, for each line, output the maximum happiness for the corresponding datum.
 

 

Sample Input
2 10 3 1 10 1 5 1000 0 5 10 1000 1 3 9 10 0 10 3 1 10 1 5 1000 0 5 10 1000 0 3 9 10 0
 

 

Sample Output
2000 1990
 

 

Source
    
    首先要知道,求最大费用的话,可以将费用取相反数然后求最小费用,前提是没有负环。
    把每个电影看作是一个结点,由于每个电影只能被看一次,所以将一个节点分作两个中间连一条<cap=1,cost=0>的边,建立源点
S和S‘,S向S’建立一条<K,0>的边表示最多K个人,S‘向每个电影连<1,-w[i]>表示第一部观看的影片,建立汇点T,将所有电影点向T连接一条
<1,0>的边,然后电影之间根据条件建边,在满足t[i]<=s[j]的情况下,如果op相同,从i的结束点向j的出发点建立一条<1,-w[j]>的边,否则
<1,-(w[j]-W)>.
    然后跑最小费用流就好了。
    
 1 #include<bits/stdc++.h> 
 2 using namespace std;  
 3 #define LL long long 
 4 #define mp make_pair
 5 #define pb push_back
 6 #define inf 0x3f3f3f3f
 7 #define pii pair<int,int>
 8 
 9 int first[1010],d[1010],a[1010],p[1010],tot,W,N,S,T,K,M;
10 bool vis[1010];
11 struct Edge{
12     int u,v,w,cap,flow,next;
13 }e[100010];
14 void add(int u,int v,int w,int cap){
15     e[tot]=Edge{u,v,w,cap,0,first[u]};
16     first[u]=tot++;
17     e[tot]=Edge{v,u,-w,0,0,first[v]};
18     first[v]=tot++;
19 } 
20 int spfa(int &flow,int &cost){
21     memset(d,inf,sizeof(d));
22     memset(vis,0,sizeof(vis));
23     queue<int>q;
24     d[S]=0,vis[S]=1,a[S]=inf,p[S]=-1;
25     q.push(S);
26     while(!q.empty()){
27         int u=q.front();
28         q.pop();
29         vis[u]=0;
30         for(int i=first[u];~i;i=e[i].next){
31             if(e[i].cap>e[i].flow && d[e[i].v]>d[u]+e[i].w){
32                 d[e[i].v]=d[u]+e[i].w;
33                 p[e[i].v]=i;
34                 a[e[i].v]=min(a[u],e[i].cap-e[i].flow);
35                 if(!vis[e[i].v]){
36                     q.push(e[i].v);
37                     vis[e[i].v]=1;
38                 }
39             }
40         }
41     }
42     if(d[T]==inf) return 0;
43     flow+=a[T];
44     cost+=d[T]*a[T];
45     int u=T;
46     while(u!=S){
47         e[p[u]].flow+=a[T];
48         e[p[u]^1].flow-=a[T];
49         u=e[p[u]].u;
50     }
51     return 1;
52 }
53 int solve(){
54     int flow=0,cost=0;
55     while(flow<K&&spfa(flow,cost));
56     return -cost;
57 }
58 int s[220],t[220],w[220],op[220];
59 int main()  
60 {
61     int cas,i,j;
62     scanf("%d",&cas);
63     while(cas--){
64         tot=0;
65         memset(first,-1,sizeof(first));
66         scanf("%d%d%d%d",&N,&M,&K,&W);
67         for(i=1;i<=M;++i){
68             scanf("%d%d%d%d",s+i,t+i,w+i,op+i);
69             add(i,i+M,0,1);
70         }
71         add(M*2+1,M*2+2,0,K);
72         for(i=1;i<=M;++i) {
73             add(M*2+2,i,-w[i],1);
74             add(i+M,M*2+3,0,1);
75         }
76         for(i=1;i<=M;++i){
77             for(j=1;j<=M;++j){
78                 if(i==j)continue;
79                 if(s[j]>=t[i]){
80                     if(op[i]==op[j]){
81                         add(i+M,j,-(w[j]-W),1);
82                     }
83                     else{
84                         add(i+M,j,-w[j],1);
85                     }
86                 }
87             }
88         }
89         S=M*2+1,T=M*2+3;
90         cout<<solve()<<endl;
91     }
92     return 0;  
93 }  

 

posted @ 2018-08-23 17:12  *zzq  阅读(223)  评论(0编辑  收藏  举报