#include <cstdio> #include <algorithm> #include <queue> #include <cstring> using namespace std;
const int maxN = 1e9;
struct node { int ele; int w; node* next; node() { ele = -1; w = -2; next = NULL; } node(int e, int _w) :ele(e), w(_w) { next = NULL; } };
class linkedList { private: node* header; int size; public: linkedList() { header = NULL; size = 0; } ~linkedList() { clear(); } int getSize() { return size; } node* getHeader() { return header; } void add(int ele, int w) { node* p = new node(ele, w); if (header == NULL) header = p; else { node* temp = header; node* temp2 = NULL; while (temp != NULL) { temp2 = temp; temp = temp->next; } temp2->next = p; } size++; } void clear() { node* temp = header; node* ttemp = NULL; while (temp != NULL) { ttemp = temp; temp = temp->next; ttemp = NULL; } header = NULL; delete temp; delete ttemp; delete header; size = 0; } };
const int maxSize = 50050;
class graph { private: linkedList arr[maxSize]; int dis[maxSize]; int vis[maxSize]; int dots; int size; public: graph() { dots = 0; size = 0; memset(dis, -maxN, maxSize); memset(vis, 0, maxSize); } graph(int n) { dots = n; size = 0; memset(dis, -maxN, maxSize); memset(vis, 0, maxSize); } ~graph() { } void add(int u, int v, int w) { arr[u].add(v, w); size++; } int spfa(int s, int e) { for (int i = 0; i <= dots; i++) { dis[i] = -maxN; vis[i] = 0; } dis[s] = 0; vis[s] = 1; queue<int> q; q.push(s); while (!q.empty()) { int x = q.front(); q.pop(); vis[x] = 0; for (node* p = arr[x].getHeader(); p != NULL; p = p->next) { int w = p->w; int v = p->ele; if (dis[v] < dis[x] + w) { dis[v] = dis[x] + w; if (!vis[v]) { q.push(v); vis[v] = 1; } } } } return dis[e]; } };
int main() { int n; scanf_s("%d", &n); graph G(n);
int left = maxN, right = -1; while (n--) { int a, b, c; scanf_s("%d %d %d", &a, &b, &c); G.add(a, b + 1, c); left = min(left, a); right = max(right, b + 1); } for (int i = left; i <= right; i++) { G.add(i + 1, i, -1); G.add(i, i + 1, 0); } int ans = G.spfa(left, right); printf("%d\n", ans);
return 0; }
|