讲解博客:
http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html
思路:
区间约束
构图+SPFA求最长路
构图:
1.每个点满足:0 <= d[i+1] - d[ i ] < = 1 ; i 向 i + 1 有一条边
2.对输入的每条边满足:d[y] - d[x-1] >= z; x - 1 向 y 有一条边
Code:
#include <bits/stdc++.h>
using namespace std;
int k , n ;
const int AX = 2e5+66;
struct Node{
int to , w;
int next ;
}G[AX];
int dis[AX];
int vis[AX];
int head[AX];
int MAXN;
int tot ;
void addedge( int u , int v , int c ){
G[tot].to = v;
G[tot].w = c;
G[tot].next = head[u];
head[u] = tot++;
}
void SPFA(){
queue<int>q;
q.push(0);
memset( vis , 0 ,sizeof(vis));
for( int i = 0 ; i <= MAXN ; i++ ){
dis[i] = -1;
}
dis[0] = 0;
vis[0] = 1;
while( !q.empty() ){
int v = q.front();
vis[v] --;
q.pop();
for( int i = head[v] ; ~i ; i = G[i].next ){
int tmp = G[i].to;
if( dis[v] + G[i].w > dis[tmp] ){
dis[tmp] = dis[v] + G[i].w;
if( !vis[tmp] ){
q.push(tmp);
vis[tmp] ++;
}
}
}
}
}
int main(){
scanf("%d%d",&k,&n);
int x, y ,z;
memset( head , -1 , sizeof(head) );
MAXN = 0;
tot = 0;
for( int i = 0 ; i < n ; i++ ){
scanf("%d%d%d",&x,&y,&z);
MAXN = max( y , MAXN );
addedge( x - 1 , y , z );
}
// 0 <= d[i]-d[i-1] <= 1
for( int i = 0 ; i <= MAXN ; i++ ){
addedge( i , i + 1 , 0 );
addedge( i + 1 , i , -1 );
}
SPFA();
printf("%d\n",dis[MAXN]);
return 0 ;
}
例题:poj1201
Intervals
Time Limit: 2000MS
Memory Limit: 65536K
Total Submissions: 28882
Accepted: 11142
Description
You are given n closed, integer intervals [ai, bi] and n integers c1, …, cn.
Write a program that:
reads the number of intervals, their end points and integers c1, …, cn from the standard input,
computes the minimal size of a set Z of integers which has at least ci common elements with interval [ai, bi], for each i=1,2,…,n,
writes the answer to the standard output.
Input
The first line of the input contains an integer n (1 <= n <= 50000) – the number of intervals. The following n lines describe the intervals. The (i+1)-th line of the input contains three integers ai, bi and ci separated by single spaces and such that 0 <= ai <= bi <= 50000 and 1 <= ci <= bi - ai+1.
Output
The output contains exactly one integer equal to the minimal size of set Z sharing at least ci elements with interval [ai, bi], for each i=1,2,…,n.
Sample Input
5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1
Sample Output
6
思路:
【例题2】给定n(n <= 50000)个整点闭区间和这个区间中至少有多少整点需要被选中,每个区间的范围为[ai, bi],并且至少有ci个点需要被选中,其中0 <= ai <= bi <= 50000,问[0, 50000]至少需要有多少点被选中。
例如3 6 2 表示[3, 6]这个区间至少需要选择2个点,可以是3,4也可以是4,6(总情况有 C(4, 2)种 )。
这类问题就没有线性约束那么明显,需要将问题进行一下转化,考虑到最后需要求的是一个完整区间内至少有多少点被选中,试着用d[i]表示[0, i]这个区间至少有多少点能被选中,根据定义,可以抽象出 d[-1] = 0,对于每个区间描述,可以表示成d[ bi ] - d[ ai - 1 ] >= ci,而我们的目标要求的是 d[ 50000 ] - d[ -1 ] >= T 这个不等式中的T,将所有区间描述转化成图后求-1到50000的最长路。
这里忽略了一些要素,因为d[i]描述了一个求和函数,所以对于d[i]和d[i-1]其实是有自身限制的,考虑到每个点有选和不选两种状态,所以d[i]和d[i-1]需要满足以下不等式: 0 <= d[i] - d[i-1] <= 1 (即第i个数选还是不选)
这样一来,还需要加入 50000*2 = 100000 条边,由于边数和点数都是万级别的,所以不能采用单纯的Bellman-Ford ,需要利用SPFA进行优化,由于-1不能映射到小标,所以可以将所有点都向x轴正方向偏移1个单位(即所有数+1)。
Code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int k , n ;
const int AX = 2e5+66;
struct Node{
int to , w;
int next ;
}G[AX];
int dis[AX];
int vis[AX];
int head[AX];
int MAXN;
int tot ;
void addedge( int u , int v , int c ){
G[tot].to = v;
G[tot].w = c;
G[tot].next = head[u];
head[u] = tot++;
}
void SPFA(){
queue<int>q;
q.push(0);
memset( vis , 0 ,sizeof(vis));
for( int i = 0 ; i <= MAXN ; i++ ){
dis[i] = -1;
}
dis[0] = 0;
vis[0] = 1;
while( !q.empty() ){
int v = q.front();
vis[v] --;
q.pop();
for( int i = head[v] ; ~i ; i = G[i].next ){
int tmp = G[i].to;
if( dis[v] + G[i].w > dis[tmp] ){
dis[tmp] = dis[v] + G[i].w;
if( !vis[tmp] ){
q.push(tmp);
vis[tmp] ++;
}
}
}
}
}
int main(){
scanf("%d",&n);
int x, y ,z;
memset( head , -1 , sizeof(head) );
MAXN = 0;
tot = 0;
for( int i = 0 ; i < n ; i++ ){
scanf("%d%d%d",&x,&y,&z);
x ++ ;
y ++ ;
MAXN = max( y , MAXN );
addedge( x - 1 , y , z );
}
// 0 <= d[i]-d[i-1] <= 1
for( int i = 0 ; i <= MAXN ; i++ ){
addedge( i , i + 1 , 0 );
addedge( i + 1 , i , -1 );
}
SPFA();
printf("%d\n",dis[MAXN]);
return 0 ;
}