HDU6437 Videos 最小费用最大流 2018 Multi-University Training Contest 10

Problem L.Videos

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


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

 

以每个电影的起点和终点建点,以电影时间建边,容量为1,费用为-hapiness,当两个电影的播放时间没有冲突时,对对应的起点和终点建容量为1,同类型费用为w,不同类型费用为0的边,所有电影的起点都与源点s相连,终点与汇点t相连。

再在S和s间建一条容量为k的边(保证不超过k人)

#include <bits/stdc++.h>
using namespace std;
int T,n,m,k,w;
struct K{
        int d,p;
};

struct Node{
        int s,t,flow,p;
        bool operator < (const Node &a)const{
                return (s == a.s) ? t < a.t : s < a.s;
        }
} node[500];

const int MAXN = 10000;
const int MAXM = 100000;
const int INF = 0x3f3f3f3f;
struct Edge{
        int to,next,cap,flow,cost;
} edge[MAXM];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;

void init(int n){
        N = 2*m+2;
        tol = 0;
        memset(head, - 1,sizeof(head));
}

void addedge(int u,int v,int cost,int cap){
        edge[tol].to = v;
        edge[tol].cap = cap;
        edge[tol].cost = cost;
        edge[tol].flow = 0;
        edge[tol].next = head[u];
        head[u] = tol++;
        edge[tol].to = u;
        edge[tol].cap = 0;
        edge[tol].cost = - cost;
        edge[tol].flow = 0;
        edge[tol].next = head[v];
        head[v] = tol++;
}

bool spfa(int s,int t){
        queue<int>q;
        for(int i = 0; i <= N; i++){
                dis[i] = INF;
                vis[i] = false;
                pre[i] = - 1;
        }
        dis[s] = 0;
        vis[s] = true;
        q.push(s);
        while(!q.empty()){
                int u = q.front();
                q.pop();
                vis[u] = false;
                for(int i = head[u]; i != - 1; i = edge[i].next){
                        int v = edge[i].to;

                        if(edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost ){
                                dis[v] = dis[u] + edge[i].cost;
                                pre[v] = i;
                                if(!vis[v]){
                                        vis[v] = true;
                                        q.push(v);
                                }
                        }
                }
        }
        if(pre[t] == - 1)
                return false;
        else
                return true;
}

int minCostMaxflow(int s,int t,int &cost){
        int flow = 0;
        cost = 0;
        while(spfa(s,t)){
                int Min = INF;
                for(int i = pre[t]; i != - 1; i = pre[edge[i^1].to]){
                        if(Min > edge[i].cap - edge[i].flow)
                                Min = edge[i].cap - edge[i].flow;
                }
                for(int i = pre[t]; i != - 1; i = pre[edge[i^1].to]){
                        edge[i].flow += Min;
                        edge[i^1].flow -= Min;
                        cost += edge[i].cost * Min;
                }
                flow += Min;
        }
        return flow;
}

int main(){
        scanf("%d",&T);
        while(T--){
                scanf("%d%d%d%d",&n,&m,&k,&w);
                init(n);
                for (int i = 1; i <= m; ++i){
                        int s,t,flow,p;
                        scanf("%d%d%d%d",&s,&t,&flow,&p);
                        node[i] = Node{s,t,flow,p};
                        addedge(i*2,i*2+1,-flow,1);
                        addedge(i*2+1,m*2+2,0,1);
                        addedge(1,i*2,0,1);
                }
                addedge(0,1,0,k);
                for (int i = 1; i <= m-1; ++i){
                        for (int j = i+1; j <= m; ++j){
                                if (node[i].t <= node[j].s){
                                        if (node[i].p == node[j].p )
                                                addedge(i*2+1,j*2,w,1);
                                        else
                                                addedge(i*2+1,j*2,0,1);
                                }
                                if (node[j].t <= node[i].s){
                                        if (node[j].p == node[i].p)
                                                addedge(j*2+1,i*2,w,1);
                                        else
                                                addedge(j*2+1,i*2,0,1);
                                }
                        }
                }
                int cost;
                int flow = minCostMaxflow(0,2*m+2,cost);
                printf("%d\n",-cost);
        }
        return 0;
}
View Code

 

posted @ 2018-08-22 19:54  mizersy  阅读(384)  评论(0编辑  收藏  举报