f[b]-f[a-1]>=c => f[b]>=f[a-1]+c a-1连边到b然后求最长路,然后再连边i-1到i为0,i到i-1为-1,跑最长路即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn=50000+5;
const int inf=100000000;
struct edge{
int from,to,dist;
};
struct BellmanFord{
int n,m;
vector<edge> edges;
vector<int> G[maxn];
bool inq[maxn];
int d[maxn];
int p[maxn];
int cnt[maxn];
void init(int n){
this->n=n;
for(int i=0;i<n;i++) G[i].clear();
edges.clear();
}
void addedge(int from,int to,int dist){
edges.push_back((edge){from,to,dist});
m=(int)edges.size();
G[from].push_back(m-1);
}
void bellmanford(int s){
queue<int> q;
memset(inq,0,sizeof(inq));
memset(cnt,0,sizeof(cnt));
for(int i=0;i<n;i++) d[i]=-inf;
d[s]=0;
q.push(s);
inq[s]=true;
while(!q.empty()){
int u=q.front();q.pop();
inq[u]=false;
for(int i=0;i<G[u].size();i++){
edge &e=edges[G[u][i]];
if(d[e.to]<d[u]+e.dist){
d[e.to]=d[u]+e.dist;
p[e.to]=G[u][i];
if(!inq[e.to]) {q.push(e.to);inq[e.to]=true;}
}
}
}
}
};
BellmanFord solver;
int n;
int main()
{
while(~scanf("%d",&n))
{
solver.init(maxn);
int l=maxn,r=0;
int a,b,c;
for(int i=0;i<n;i++){
scanf("%d%d%d",&a,&b,&c);
a++;b++;
l=min(l,a-1);
r=max(r,b);
solver.addedge(a-1,b,c);
}
for(int i=l;i<=r;i++) {solver.addedge(i-1,i,0);solver.addedge(i,i-1,-1);}
solver.bellmanford(l);
printf("%d\n",solver.d[r]);
}
return 0;
}