HDU 6437 Videos(最小费用最大流)

Problem L.Videos

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


 

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

2018 Multi-University Training Contest 10

 

 

Recommend

chendu   |   We have carefully selected several similar problems for you:  6437 6436 6435 6434 6433 

 

 

【思路】

把视频拆点,开始时间和结束时间连一条边,容量为1,费用为-happiness,若一个视频结束时间小于等于另一个视频,则前者的结束时间与后者的开始时间连一条边,容量为1,费用为w或0(同类型为w,不同类型则为0),源点s与各视频开始时间连一条边,容量为1,费用为0,汇点t与各视频结束时间连一条边,容量为1,费用为0,最后再加一个超级源点S与源点s连一条容量为k,费用为0的边。网络流真的是很神妙的做法啊,建图也很难想,比赛时全程往DP和贪心方向想,然后就没然后了。

 

【代码】

//******************************************************************************
// File Name: 1012.cpp
// Author: Shili_Xu
// E-Mail: shili_xu@qq.com
// Created Time: 2018年08月22日 星期三 15时06分29秒
//******************************************************************************

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
typedef long long ll;

const int MAXN = 500, INF = 0x3f3f3f3f;

struct edge {
	int from, to, cap, flow, cost;

	edge() {}
	edge(int u, int v, int c, int f, int w) : from(u), to(v), cap(c), flow(f), cost(w) {}
};

int t, n, m, k, w;
vector<edge> e;
vector<int> g[MAXN];
int d[MAXN], a[MAXN], p[MAXN], st[MAXN], ed[MAXN], tp[MAXN];
bool inq[MAXN];
queue<int> q;

void init()
{
	e.clear();
	for (int i = 0; i <= n; i++) g[i].clear();
}

void add_edge(int u, int v, int c, int w)
{
	e.push_back(edge(u, v, c, 0, w));
	e.push_back(edge(v, u, 0, 0, -w));
	g[u].push_back(e.size() - 2);
	g[v].push_back(e.size() - 1);
}

bool spfa(int s, int t, int &flow, int &cost)
{
	memset(d, 0x3f, sizeof(d));
	memset(inq, false, sizeof(inq));
	d[s] = 0; q.push(s); inq[s] = true;
	a[s] = INF;
	while (!q.empty()) {
		int u = q.front(); q.pop(); inq[u] = false;
		for (int i = 0; i < (int)g[u].size(); i++) {
			edge &now = e[g[u][i]];
			if (now.cap - now.flow > 0 && d[now.to] > d[u] + now.cost) {
				a[now.to] = min(a[u], now.cap - now.flow);
				p[now.to] = g[u][i];
				d[now.to] = d[u] + now.cost;
				if (!inq[now.to]) {
					q.push(now.to);
					inq[now.to] = true;
				}
			}
		}
	}
	if (d[t] == INF) return false;
	for (int i = t; i != s; i = e[p[i]].from) {
		e[p[i]].flow += a[t];
		e[p[i] ^ 1].flow -= a[t];
	}
	flow += a[t];
	cost += d[t] * a[t];
	return true;
}

int min_cost_max_flow(int s, int t, int	&cost)
{
	int ans = 0;
	cost = 0;
	while (spfa(s, t, ans, cost));
	return ans;
}

int main()
{
	scanf("%d", &t);
	while (t--) {
		int x;
		scanf("%d %d %d %d", &x, &m, &k, &w);
		n = 2 * m + 2;
		init();
		add_edge(0, 1, k, 0);
		for (int i = 1; i <= m; i++) {
			int get;
			scanf("%d %d %d %d", &st[i], &ed[i], &get, &tp[i]);
			add_edge(2 * i,  2 * i + 1, 1, -get);
			add_edge(1, 2 * i, 1, 0);
			add_edge(2 * i + 1, n, 1, 0);
		}
		for (int i = 1; i <= m - 1; i++) {
			for (int j = i + 1; j <= m; j++) {
				if (ed[i] <= st[j]) {
					if (tp[i] == tp[j])
						add_edge(2 * i + 1, 2 * j, 1, w);
					else
						add_edge(2 * i + 1, 2 * j, 1, 0);
				}
				if (ed[j] <= st[i]) {
					if (tp[j] == tp[i])
						add_edge(2 * j + 1, 2 * i, 1, w);
					else
						add_edge(2 * j + 1, 2 * i, 1, 0);
				}

			}
		}
		int ans;
		min_cost_max_flow(0, n, ans);
		printf("%d\n", -ans);
	}
	return 0;
}

 

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可 6私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值