题目描述
An automobile factory has a car production line. Now the market is oversupply and the production line is often shut down. To make full use of resources, the manager divides the entire production line into N parts (1...N). Some continuous parts can produce sub-products. And each of sub-products has their own value. The manager will use spare time to produce sub-products to make money. Because of the limited spare time, each part of the production line could only work at most K times. And Because of the limited materials, each of the sub-products could be produced only once. The manager wants to know the maximum value could he make by produce sub-products.
输入
The first line of input is T, the number of test case.
The first line of each test case contains three integers, N, K and M.(M is the number of different sub-product).
The next M line each contain three integers Ai, Bi, Wi describing a sub-product. The sub-product has value Wi. Only Ai to Bi parts work simultaneously will the sub-product be produced(include Ai to Bi).
1 ≤ T ≤ 100
1 ≤ K ≤ M ≤ 200
1 ≤ N ≤ 10^5
1 ≤ Ai ≤ Bi ≤ N
1 ≤ Wi ≤10^5
输出
For each test case output the maximum value in a separate line.
样例输入
复制样例数据
4 10 1 3 1 2 2 2 3 4 3 4 8 10 1 3 1 3 2 2 3 4 3 4 8 100000 1 3 1 100000 100000 1 2 3 100 200 300 100000 2 3 1 100000 100000 1 150 301 100 200 300
样例输出
10 8 100000 100301
最长 k 可重区间集
https://www.cnblogs.com/hongyj/p/9433635.html
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const long long INF=1e9;
const int maxn = 510;
//spfa最小费用最大流模板
struct edge
{
int to,nex,cap,cost,flow;
} G[maxn*3];
int dis[maxn],pre[maxn],head[maxn],tot,V;
bool vis[maxn];
void addedge(int u,int v,int cap,int cost)
{
G[tot].to=v;
G[tot].cap=cap;
G[tot].cost=cost;
G[tot].flow=0;
G[tot].nex=head[u];
head[u]=tot;
++tot;
G[tot].to=u;
G[tot].cap=0;
G[tot].cost=-cost;
G[tot].flow=0;
G[tot].nex=head[v];
head[v]=tot;
++tot;
}
void init(int n)
{
V=n;
tot=0;
memset(head,-1,sizeof(head));
}
bool spfa(int s,int t)
{
queue<int>q;
for (int i = 0; i < V; i++)
{
dis[i] = INF;
vis[i] = false;
pre[i] = -1;
}
dis[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 = G[i].nex)
{
int v = G[i].to;
if (G[i].cap>G[i].flow&&dis[v]>dis[u] + G[i].cost)
{
dis[v] = dis[u] + G[i].cost;
pre[v] = i;
if (!vis[v])
{
vis[v] = true;
q.push(v);
}
}
}
}
if (pre[t] == -1)
{
return false;
}
else
{
return true;
}
}
int mcmf(int s, int t, int &cost)
{
int flow = 0;
cost = 0;
while (spfa(s, t))
{
int minn = INF;
for (int i = pre[t]; i !=-1; i = pre[G[i ^ 1].to])
{
if (minn>G[i].cap - G[i].flow)
{
minn = G[i].cap - G[i].flow;
}
}
for (int i = pre[t]; i!=-1 ; i = pre[G[i ^ 1].to])
{
G[i].flow += minn;
G[i ^ 1].flow -= minn;
cost += G[i].cost*minn;
}
flow += minn;
}
return flow;
}
struct node
{
int l, r;
int val;
};//用于离散化
int x, y, n, m;
int k, v;
node a[maxn];
int b[maxn];
map<int, int> mp;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d", &n, &k, &m);
int cnt = 1;
mp.clear();
for (int i = 0; i < m; i++)
{
scanf("%d%d%d", &a[i].l, &a[i].r, &a[i].val);
b[i * 2] = a[i].l;
b[i * 2 + 1] = a[i].r;
}
sort(b, b + m * 2);
mp[b[0]] = cnt;
for (int i = 1; i < m * 2; i++)
{
if (b[i] != b[i - 1])
{
mp[b[i]] = cnt++;
}
}
init(cnt + 2);
int S = cnt + 1, T = cnt;
for (int i = 0; i < m; i++)
{
addedge(mp[a[i].l] - 1, mp[a[i].r], 1, -a[i].val);
}
addedge(S, 0, k, 0);
for (int i = 0; i < cnt; i++)
{
addedge(i, i + 1, INF, 0);
}
int ans = 0;
mcmf(S, T, ans);
printf("%d\n", -ans);
}
return 0;
}