标签:
Description
Input
Output
Sample Input
5 3 7 3 8 10 3 6 8 1 1 3 1 10 11 1
Sample Output
6
Source
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <stack> #include <vector> using namespace std; const int INF = 0x3f3f3f3f; const int N = 5e4 + 10; struct Edge { int v, cost; Edge() {} Edge(int v, int cost) : v(v), cost(cost) {} }; vector<Edge> E[N]; void add(int u, int v, int w) { E[u].push_back(Edge(v, w)); } bool vis[N]; int d[N]; void SPFA(int st, int n) { memset(vis, false, sizeof vis); for(int i = 1; i <= n; ++i) d[i] = -INF; d[st] = 0; vis[st] = true; queue<int> que; while(!que.empty()) que.pop(); que.push(st); while(!que.empty()) { int u = que.front(); que.pop(); vis[u] = false; int sx = E[u].size(); for(int i = 0; i < sx; ++i) { int v = E[u][i].v; int w = E[u][i].cost; if(d[v] < d[u] + w) { d[v] = d[u] + w; if(!vis[v]) { vis[v] = true; que.push(v); } } } } } int main() { int n; while(~scanf("%d", &n)) { int a, b, c, m = 0, st = INF; for(int i = 0; i < n; ++i) { scanf("%d%d%d", &a, &b, &c); a+=2; b+=2; add(a - 1, b, c); m = max(m, b); st = min(st, a - 1); } for(int i = 2; i <= m; ++i) { add(i, i - 1, -1); add(i - 1, i, 0); } SPFA(st, m); printf("%d\n", d[m]); } return 0; }
标签:
原文地址:http://www.cnblogs.com/orchidzjl/p/5289825.html