费用流
#include<bits/stdc++.h>
using namespace std;
const int N = 500;
const int inf = 0x3f3f3f3f;
int T, n, m, K, W, tar;
bool inq[N];
int a[N], d[N], p[N];
struct Movie{
int s, t, w, op;
void read(){
scanf("%d %d %d %d", &s, &t, &w, &op);
}
}mv[N];
struct Edge{
int from, to, cap, flow, cost;
Edge(){}
Edge(int from, int to, int cap, int cost):from(from),to(to),cap(cap),cost(cost){
flow = 0;
}
};
vector<Edge> G;
vector<int> V[N];
void add_edge(int from, int to, int cap, int cost){
G.push_back(Edge(from, to, cap, cost));
G.push_back(Edge(to, from, 0, -cost));
int x = G.size();
V[from].push_back(x - 2);
V[to].push_back(x - 1);
}
bool BellmanFord(int &flow, int &cost){
queue<int> Q;
for(int i=0; i<=tar; i++){
inq[i] = 0;
d[i] = inf;
a[i] = inf;
}
d[0] = 0; inq[0] = 1; p[0] = -1;
Q.push(0);
int x;
while(!Q.empty()){
x = Q.front(); Q.pop();
inq[x] = 0;
for(int i=0; i<V[x].size(); i++){
int j = V[x][i];
Edge &e = G[j];
if(e.cap > e.flow && d[e.to] > d[x] + e.cost){
d[e.to] = d[x] + e.cost;
p[e.to] = j;
a[e.to] = min(a[e.to], e.cap - e.flow);
if(!inq[e.to]){
inq[e.to] = 1;
Q.push(e.to);
}
}
}
}
if(d[tar] == inf) return 0;
x = tar;
flow += a[tar];
cost += a[tar] * d[tar];
while(x){
G[p[x]].flow += a[tar];
G[p[x]^1].flow -= a[tar];
x = G[p[x]].from;
}
return 1;
}
int solve(){
tar = m * 2 + 2;
for(int i=0; i<=tar; i++) V[i].clear();
G.clear();
add_edge(0, tar - 1, K, 0);
add_edge(tar - 1, tar, K, 0);
int c;
for(int i=1; i<=m; i++){
add_edge(tar - 1, i, 1, 0);
add_edge(i, i+m, 1, -mv[i].w);
add_edge(i+m, tar, 1, 0);
for(int j=1; j<=m; j++){
if(mv[i].t <= mv[j].s){
if(mv[i].op == mv[j].op){
c = W;
} else {
c = 0;
}
add_edge(i+m, j, 1, c);
}
}
}
int flow, cost;
flow = cost = 0;
while(BellmanFord(flow, cost));
return -cost;
}
int main(){
scanf("%d", &T);
while(T--){
scanf("%d %d %d %d", &n, &m, &K, &W);
for(int i=1; i<=m; i++){
mv[i].read();
}
printf("%d\n", solve());
}
return 0;
}