HDU - 6437 Problem L.Videos (最小费用最大流)

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

 

          这道题就是说n人可以去看m场电影,看一场电影会获得快乐值,连续看同一类型的电影会损失快乐值,求安排后能获得的最大的快乐值。(每个电影只能一个人看)

         最小费用最大流,将电影拆点,拆点间流量为1,价值为电影获得快乐值,然后一个究极源点到一个超级源点,流量就是人数n,价值是0。然后所有的电影末点与超级汇点一个流量为1,价值为0的链接。最后是每个电影都可以与他之后播放的电影相连线,如果类型一样的话,价值为-w,否则为w。

         最后求的是最大的快乐之,再把之前步骤里所有的价值取反得结果即可。

       

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 1200;
const int maxm = 100050;
const int inf = 0x3f3f3f3f;
struct *** {
	int v, ne, cap, flow, cost, u;
}ed[maxm];
struct ***er {
	int st, en, v, kind;
}spot[maxn];
int head[maxn], cnt;
int pre[maxn], dis[maxn];
bool vis[maxn];
int en, m, N;
void init() {
	cnt = 0;
	memset(head, -1, sizeof(head));
}
void add(int u, int v, int cap, int cost) {
	ed[cnt].v = v; ed[cnt].cap = cap; ed[cnt].flow = 0; ed[cnt].u = u;
	ed[cnt].cost = cost; ed[cnt].ne = head[u]; head[u] = cnt++;
	ed[cnt].v = u; ed[cnt].cap = 0; ed[cnt].flow = 0; ed[cnt].u = v;
	ed[cnt].cost = -cost; ed[cnt].ne = head[v]; head[v] = cnt++;
}
bool spfa(int st, int en) {
	queue<int>q;
	for (int s = 0; s <= en; s++) {//这里视情况而定
		dis[s] = inf; vis[s] = 0; pre[s] = -1;
	}
	dis[st] = 0;
	vis[st] = 1;
	q.push(st);
	while (!q.empty()) {
		int u = q.front(); q.pop();
		vis[u] = 0;
		for (int s = head[u]; ~s; s = ed[s].ne) {
			int v = ed[s].v;
			if (ed[s].cap > ed[s].flow&&dis[v] > dis[u] + ed[s].cost) {
				dis[v] = dis[u] + ed[s].cost;
				pre[v] = s;
				if (!vis[v]) {
					vis[v] = 1; q.push(v);
				}
			}
		}
	}
	if (pre[en] == -1)return 0;
	return 1;
}
int MinMax(int st, int en, int &cost) {
	int flow = 0;
	cost = 0;
	while (spfa(st, en)) {		
		int min = inf;
		for (int s = pre[en]; ~s; s = pre[ed[s ^ 1].v]) {
	
			if (min > ed[s].cap - ed[s].flow)
				min = ed[s].cap - ed[s].flow;
		}
		for (int s = pre[en]; ~s; s = pre[ed[s ^ 1].v]) {
			ed[s].flow += min;
			ed[s ^ 1].flow -= min;
			cost += ed[s].cost*min;
		}
		flow += min;
	}
	return flow;
}
int main() {
	int te;
	scanf("%d", &te);
	while (te--) {
		init();
		int m, n, sum, publish;
		scanf("%d%d%d%d", &m, &n, &sum, &publish);
		for (int s = 1; s <= n; s++)
			scanf("%d%d%d%d", &spot[s].st, &spot[s].en, &spot[s].v, &spot[s].kind);
	    en = 2 * n + 2;
		for (int s = 1; s <= n; s++) {
			add(s, s+n, 1, -spot[s].v);
			add(0, s, 1, 0);
			add(s+n, en, 1, 0);
			for (int w = 1; w <= n; w++) {
				if (s == w)continue;
				if (spot[s].en <= spot[w].st)
					add(s + n, w, 1, publish*(spot[s].kind == spot[w].kind ? 1 : 0));
			}
		}
		N = 2 * n + 1;
		add(N, 0, sum, 0);
		int cost;
		MinMax(N, en, cost);
		printf("%d\n", -cost);
	}
}

 

全部评论

相关推荐

04-10 11:02
已编辑
北方民族大学 全栈开发
“无名小卒,还是名扬天下?”我知道很多人都不觉得我能走到今天这一步,当然,也包括我自己。在我的人生里,有两部作品刻下了最深的烙印:《斗破苍穹》与《龙族》。它们总被人拿来对照:一边是萧炎的桀骜轻狂,一边是路明非的怯懦衰颓。有人说,天蚕土豆没见过魂天帝,但江南见过真凯撒。我时常觉得,自己就是那个衰小孩路明非。可路明非可以开挂,我不可以;我也无数次幻想过,能拥有萧炎那般年少轻狂的人生,可我没有他与生俱来的逆天天赋。我只是个平庸的普通人,一个看过《斗破苍穹》却开不了挂的路明非,只能一步一步往上爬。从我下定决心找实习的那一刻起,我就给自己定下了目标:“我一定要为字节跳动卖命.jpg”。萧炎有他的三年之约,我有我的两年半之约(其实是一年半)。2024.11.20,科大讯飞的第一封实习offer落进邮箱,我迈出了这场奔赴的第一步。2025.8.18,放弃百度转正的安稳机会,转身走进前路未卜的不确定里。我很感谢我在百度的mentor,是她从茫茫人海选中了我,给了我大厂实习的机会。即便有段时间我状态差、产出不理想,她依旧愿意认可我、希望我留下转正。2025.11.14,我选择走进字节跳动,以实习生的身份重新出发。2026.3.25&nbsp;-&nbsp;3.31,一周速通上海飞书,幸遇赏识我的伯乐,斩获Special&nbsp;Offer。被告知面试通过的那一刻,我的内心无比平静,就像这个offer本就该属于我。不是侥幸,是应得的。这一路,有人看轻过我的出身,不相信我能走到这里;也有人在我看不见前路的时候,替我举过灯。没有他们的鼓励与支撑,就没有今天站在这里的我。我看到了自强不息的激荡,那是一个双非的伟大乐章!我是雨夜迈巴赫,我要开启属于我的新篇章了。
在看牛客的本杰明很勇...:真心祝贺l总 我永远的偶像 我滴神
春招至今,你收到几个面试...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务