题目:http://poj.org/problem?id=1201
题意:有n个区间,[ai,bi]区间至少选择ci个点,要满足这些条件,最少要多少个点
思路:
d[v]-w>=d[u],d[i]-d[i-1] <= 1,d[i]-d[i-1] >= 0
v,u建负边,i-1,i建正边1,建负边0,依旧是求最短路,maxn->minn
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<bitset>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int N = 50005;
struct edge
{
int to,nxt,d;
edge(int t = 0,int n = 0,int d = 0):to(t),nxt(n),d(d){}
}E[N*5];
int n;
int head[N*5],d[N],tot;
bitset< N<<1 > vis;
void init()
{
memset(head,-1,sizeof(head));
tot = 0;
}
void add_edge(int s,int t,int d)
{
E[tot] = edge(t,head[s],d);
head[s] = tot++;
}
void SPFA(int s)
{
memset(d,0x3f,sizeof(d));
vis.reset();
d[s] = 0;
queue<int> q;
q.push(s);
vis[s] = 1;
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = 0;
for(int i = head[u];~i;i = E[i].nxt)
{
int v = E[i].to;
if(d[v] > d[u]+E[i].d)
{
d[v] = d[u]+E[i].d;
if(!vis[v])
{
vis[v] = 1;
q.push(v);
}
}
}
}
}
int main()
{
while(~scanf("%d",&n))
{
init();
int s = 1,t = n;
int minn = 50000,maxn = 0;
int u,v,w;
for(int i = 1;i <= n;i++)
{
scanf("%d%d%d",&u,&v,&w);
add_edge(v+1,u,-w);
minn = min(minn,u);
maxn = max(maxn,v+1);
}
for(int i = minn;i < maxn;i++)
add_edge(i,i+1,1),add_edge(i+1,i,0);
SPFA(maxn);
printf("%d\n",-d[minn]);
}
}