题目:http://poj.org/problem?id=1201
http://acm.hdu.edu.cn/showproblem.php?pid=1384
题意:给n个连续闭区间和n个数(c1 - cn),你找到一个最小集合,使这个集合和每个闭区间有ci个公共数,求这个集合的最小大小
思路:往差分约束系统上想了好久,不会做。。。。设Si为区间[0, i)的区间长度,则[a, b]的长度为Sb - Sa,根据题意有Sb - Sa >= ci。为保证图的连通性,又有Si - Si-1 >= 0,
Si - Si-1 >= 1,于是建立差分约束系统,求最短路,求最长路也可以
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 50010;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> P;
struct node
{
int to, cost, next;
}g[N*10];
int head[N], dis[N], used[N];
int n, cnt;
void add_edge(int v, int u, int cost)
{
g[cnt].to = u;
g[cnt].cost = cost;
g[cnt].next = head[v];
head[v] = cnt++;
}
void spfa(int s)
{
memset(dis, 0x3f, sizeof dis);
memset(used, 0, sizeof used);
queue<int> que;
used[s] = true, dis[s] = 0;
que.push(s);
while(! que.empty())
{
int v = que.front(); que.pop();
used[v] = false;
for(int i = head[v]; i != -1; i = g[i].next)
{
int u = g[i].to;
if(dis[u] > dis[v] + g[i].cost)
{
dis[u] = dis[v] + g[i].cost;
if(! used[u])
{
used[u] = true;
que.push(u);
}
}
}
}
}
int main()
{
int a, b, c;
while(~ scanf("%d", &n))
{
memset(head, -1, sizeof head);
cnt = 0;
int maxx = -INF, minn = INF;
for(int i = 0; i < n; i++)
{
scanf("%d%d%d", &a, &b, &c);
add_edge(b + 1, a, -c);
minn = min(minn, a);
maxx = max(maxx, b + 1);
}
for(int i = minn; i < maxx; i++)
{
add_edge(i + 1, i, 0);
add_edge(i, i + 1, 1);
}
spfa(maxx);
printf("%d\n", -dis[minn]);
}
return 0;
}