题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6437
因为每个视频看的人数都有限制(只能一个人看),所以可以用流量表示这个限制的值为1,然后求最大值可以把费用取反变成求最小费用。
对每个视频拆点,中间的费用为-w【i】,开头到其和其到结尾的费用都是0,保证只计算一次值。两个起点用来限制输入的人数,流量为k,其余的流量都为1。
建模真的挺重要的
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define mod 998244353
#define For(i,m,n) for(int i=m;i<=n;i++)
#define Dor(i,m,n) for(int i=m;i>=n;i--)
#define LL long long
#define lan(a,b) memset(a,b,sizeof(a))
#define sqr(a) a*a
#define type int
using namespace std;
//s源点t汇点N总点数
const int maxn=1005;
const int maxm=100005;
int n,m;
int s, t;
struct node {
int u, v, next;
type cap, flow, cost;
}edge[maxm];
int head[maxn], cnt;
int pre[maxn];
type dis[maxn];
bool vis[maxn];
int N;
void init () {
memset (head, -1, sizeof head);
cnt = 0;
}
void add_edge (int u, int v, type cap, type cost) {
edge[cnt].u = u, edge[cnt].v = v, edge[cnt].cap = cap, edge[cnt].flow = 0;
edge[cnt].cost = cost, edge[cnt].next = head[u], head[u] = cnt++;
edge[cnt].u = v, edge[cnt].v = u, edge[cnt].cap = 0, edge[cnt].flow = 0;
edge[cnt].cost = -cost, edge[cnt].next = head[v], head[v] = cnt++;
}
bool spfa (int s, int t) {
queue <int> q;
for (int i = 0; i < N; i++) {
dis[i] = INF;
vis[i] = 0;
pre[i] = -1;
}
dis[s] = 0;
vis[s] = 1;
q.push (s);
while (!q.empty ()) {
int u = q.front (); q.pop ();
vis[u] = 0;
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].v;
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] = 1;
q.push (v);
}
}
}
}
if (pre[t] == -1)
return 0;
else
return 1;
}
int MCMF (int s, int t, type &cost) {
type flow = 0;
cost = 0;
while (spfa (s, t)) {
type Min = INF;
for (int i = pre[t]; i != -1; i = pre[edge[i^1].v]) {
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].v]) {
edge[i].flow += Min;
edge[i^1].flow -= Min;
cost += edge[i].cost*Min;
}
flow += Min;
}
return flow;
}
int l[210],r[210],w[210],op[210];
int main()
{
int tt;
scanf("%d",&tt);
while(tt--)
{
init();
int n,m,k,W;
scanf("%d%d%d%d",&n,&m,&k,&W);
For(i,1,m)
{
scanf("%d%d%d%d",&l[i],&r[i],&w[i],&op[i]);
}
s=0,t=2*m+2;
add_edge(0,1,k,0);
For(i,1,m)add_edge(1,i+1,1,0),add_edge(i+1,i+1+m,1,-w[i]),add_edge(i+1+m,t,1,0);
For(i,1,m)
{
For(j,i+1,m)
{
int tem;
if(op[i]==op[j])tem=W;
else tem=0;
if(r[i]<=l[j])
add_edge(i+1+m,j+1,1,tem);
else if(r[j]<=l[i])
add_edge(j+1+m,i+1,1,tem);
}
}
int ans;
N=2*m+3;
MCMF(s,t,ans);
printf("%d\n",-ans);
}
return 0;
}