Hdu 6437 Problem L.Videos 杭电多校第十场

Problem L.Videos

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


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
 
网络流最大流最大费,我们把每个节目拆成一条容量为1,费用为-w的边,在它的镜像点接上能继续看下去的电影,如果电影种类相同,费为w,否则为0,和汇点,在它的原点连接上每个人,人连原点。
这样转化成了负费下求最小费。
ANS取个绝对值即可。
注:一定要拆点,比赛时因为没拆点wa了一发。
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500010;
const int MAXM = 500010;
const int INF  = 0x3f3f3f3f;
struct Edge
{
    int from, to, cap, flow, cost, next;
};
Edge edge[MAXM];
int head[MAXN], edgenum;
int pre[MAXN];
int dist[MAXN];
bool vis[MAXN];
void init()
{
    edgenum = 0;
    memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int w, int c)
{
  //  cout<<u<<" "<<v<<" "<<w<<" "<<c<<endl;

    Edge E1 = {u, v, w, 0, c, head[u]};
    edge[edgenum] = E1;
    head[u] = edgenum++;
    Edge E2 = {v, u, 0, 0, -c, head[v]};
    edge[edgenum] = E2;
    head[v] = edgenum++;
}
bool SPFA(int s, int t)
{
    queue<int> Q;
    memset(dist, INF, sizeof(dist));
    memset(vis, false, sizeof(vis));
    memset(pre, -1, sizeof(pre));
    dist[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)
        {
            Edge E = edge[i];
            if(dist[E.to] > dist[u] + E.cost && E.cap > E.flow)
            {
                dist[E.to] = dist[u] + E.cost;
                pre[E.to] = i;
                if(!vis[E.to])
                {
                    vis[E.to] = true;
                    Q.push(E.to);
                }
            }
        }
    }
    return pre[t] != -1;
}
void MCMF(int s, int t, int &cost, int &flow)
{
    flow = 0;
    cost = 0;
    while(SPFA(s, t))
    {
        int Min = INF;
        for(int i = pre[t]; i != -1; i = pre[edge[i^1].to])
        {
            Edge E = edge[i];
            Min = min(Min, E.cap - E.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;
    }
}
struct nod
{
    int l=0,r=0,w=0,op;
}a[MAXN];
bool cmp(nod q1,nod q2)
{
    if (q1.l!=q2.l) return q1.l<q2.l;
    else return q1.r<q2.r;
}
int main()
{
    //freopen("de.txt","r",stdin);
    int t;
    scanf("%d",&t);
    while(t--)
    {
        init();
        memset(edge,0,sizeof(edge));
        int n,m,k,W;
        scanf("%d%d%d%d",&n,&m,&k,&W);
        for(int i=1;i<=k;i++)
        {
            addEdge(0,i,1,0);//每个人连上源点
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d%d",&a[i].l,&a[i].r,&a[i].w,&a[i].op);
        }
        sort(a+1,a+1+m,cmp);
        for (int i=1;i<=m;++i){
            addEdge(i+k,i+k+m,1,-a[i].w);//把节目拆点
        }
        for (int i=1;i<=m;++i){
            for (int j=1;j<=m;++j){
                if (i==j) continue;
                if (a[i].r<=a[j].l){
                    //printf("%d goto %d\n",a[i].w,a[j].w);
                    if (a[i].op==a[j].op){
                        addEdge(i+k+m,j+k,1,W);//如果种类相同 
                    }
                    else{
                        addEdge(i+k+m,j+k,1,0);//种类不同
                    }
                }
            }
        }
        for (int i=1;i<=k;++i){
            for (int j=1;j<=m;++j){
                addEdge(i,j+k,1,0);//每个人与节目相连
            }
        }
        for (int i=1;i<=m;++i){
            addEdge(i+k+m,k+m+m+1,1,0);//节目与汇点相连
        }
        int ans1,ans2;
        MCMF(0,k+m+m+1,ans1,ans2);
        printf("%d\n",-ans1);
    }
    return 0;
}

  

posted @ 2018-08-23 10:49  行远山  阅读(167)  评论(0编辑  收藏  举报