您的位置:首页 > 其它

POJ 3680 Intervals

2014-09-16 17:30 351 查看

Intervals

Time Limit: 5000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 3680
64-bit integer IO format: %lld Java class name: Main

You are given N weighted open intervals. The ith interval covers (ai, bi) and weighs wi. Your task is to pick some of the intervals to maximize the total weights under the limit that no point in the real axis is covered more than k times.

Input

The first line of input is the number of test case.The first line of each test case contains two integers, N and K (1 ≤ K ≤ N ≤ 200).The next N line each contain three integers ai, bi, wi(1 ≤ ai < bi ≤ 100,000, 1 ≤ wi ≤ 100,000) describing the intervals. There is a blank line before each test case.

Output

For each test case output the maximum total weights in a separate line.

Sample Input

4

3 1
1 2 2
2 3 4
3 4 8

3 1
1 3 2
2 3 4
3 4 8

3 1
1 100000 100000
1 2 3
100 200 300

3 2
1 100000 100000
1 150 301
100 200 300

Sample Output

14
12
100000
100301

Source

POJ Founder Monthly Contest – 2008.07.27

解题:离散化+费用流

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 1000;
struct arc{
int v,w,f,next;
arc(int x = 0,int y = 0,int z = 0,int nxt = 0){
v = x;
w = y;
f = z;
next = nxt;
}
};
arc e[10000];
int head[maxn],d[maxn],p[maxn],tot;
int n,k,S,T,lisan[maxn],cnt,x[maxn],y[maxn],sc[maxn];
bool in[maxn];
queue<int>q;
void add(int u,int v,int w,int f){
e[tot] = arc(v,w,f,head[u]);
head[u] = tot++;
e[tot] = arc(u,-w,0,head[v]);
head[v] = tot++;
}
bool spfa(){
for(int i = 0; i <= T; i++){
d[i] = INF;
in[i] = false;
p[i] = -1;
}
while(!q.empty()) q.pop();
d[S] = 0;
in[S] = true;
q.push(S);
while(!q.empty()){
int u = q.front();
q.pop();
in[u] = false;
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].f > 0 && d[e[i].v] > d[u] + e[i].w){
d[e[i].v] = d[u] + e[i].w;
//cout<<"end:"<<e[i].v<<endl;
p[e[i].v] = i;
if(!in[e[i].v]){
in[e[i].v] = true;
q.push(e[i].v);
}
}
}
}
return p[T] > -1;
}
int solve(){
int tmp = 0,minV;
while(spfa()){
minV = INF;
for(int i = p[T]; ~i; i = p[e[i^1].v])
minV = min(minV,e[i].f);
for(int i = p[T]; ~i; i = p[e[i^1].v]){
tmp += minV*e[i].w;
e[i].f -= minV;
e[i^1].f += minV;
}
//cout<<"tmp:"<<tmp<<endl;
}
return tmp;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&k);
memset(head,-1,sizeof(head));
cnt = tot = 0;
//cout<<"n:"<<n<<endl;
for(int i = 0; i < n; i++){
scanf("%d %d %d",x+i,y+i,sc+i);
lisan[cnt++] = x[i];
lisan[cnt++] = y[i];
}
sort(lisan,lisan+cnt);
int cnt1 = 1;
for(int i = 1; i < cnt; i++)
if(lisan[cnt1-1] != lisan[i]) lisan[cnt1++] = lisan[i];
cnt = cnt1;
S = 0;
T = cnt;
//cout<<"n:"<<n<<endl;
for(int i = 0; i < n; i++){
int tx = lower_bound(lisan,lisan+cnt,x[i])-lisan;
int ty = lower_bound(lisan,lisan+cnt,y[i])-lisan;
//cout<<tx<<" "<<ty<<" ---"<<endl;
add(tx,ty,-sc[i],1);
add(tx,ty,0,k);
}
for(int i = 0; i < cnt; i++) add(i,i+1,0,k);
printf("%d\n",-solve());
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: