传送门
同 AcWing 362 区间、洛谷 SP116 INTERVAL - Intervals、UVA 1723 Intervals
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<int, int> p;
const double pi(acos(-1));
const int maxn(5e4 + 10);
const int maxm(5e5 + 10);
bool vis[maxn];
int ecnt, head[maxn], dis[maxn];
struct edge {
int to, wt, nxt;
} edges[maxm];
template<typename T>
inline const T read()
{
T x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return x * f;
}
template<typename T>
inline void write(T x, bool ln)
{
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) write(x / 10, false);
putchar(x % 10 + '0');
if (ln) putchar(10);
}
void addEdge(int u, int v, int w)
{
edges[ecnt].to = v;
edges[ecnt].wt = w;
edges[ecnt].nxt = head[u];
head[u] = ecnt++;
}
void spfa()
{
queue<int> q;
q.push(0);
dis[0] = 0;
vis[0] = true;
while (not q.empty()) {
int u = q.front();
q.pop();
vis[u] = false;
for (int i = head[u]; compl i; i = edges[i].nxt) {
int v = edges[i].to, w = edges[i].wt;
if (dis[v] < dis[u] + w) {
dis[v] = dis[u] + w;
if (not vis[v]) {
vis[v] = true;
q.push(v);
}
}
}
}
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("input.txt", "r", stdin);
#endif
memset(head, -1, sizeof head);
memset(dis, -1, sizeof dis);
int n = read<int>(), mx = 0;
for (int i = 1; i <= n; ++i) {
int a = read<int>() + 1, b = read<int>() + 1, w = read<int>();
mx = max(mx, b);
addEdge(a - 1, b, w);
}
for (int i = 1; i <= mx; ++i) {
addEdge(i - 1, i, 0);
addEdge(i, i - 1, -1);
}
spfa();
printf("%d
", dis[mx]);
return 0;
}