一、内容

During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’s class a large bag of candies and had flymouse distribute them. All the kids loved candies very much and often compared the numbers of candies they got with others. A kid A could had the idea that though it might be the case that another kid B was better than him in some aspect and therefore had a reason for deserving more candies than he did, he should never get a certain number of candies fewer than B did no matter how many candies he actually got, otherwise he would feel dissatisfied and go to the head-teacher to complain about flymouse’s biased distribution.

snoopy shared class with flymouse at that time. flymouse always compared the number of his candies with that of snoopy’s. He wanted to make the difference between the numbers as large as possible while keeping every kid satisfied. Now he had just got another bag of candies from the head-teacher, what was the largest difference he could make out of it?

Input

The input contains a single test cases. The test cases starts with a line with two integers N and M not exceeding 30 000 and 150 000 respectively. N is the number of kids in the class and the kids were numbered 1 through N. snoopy and flymouse were always numbered 1 and N. Then follow M lines each holding three integers A, B and c in order, meaning that kid A believed that kid B should never get over c candies more than he did.

Output

Output one line with only the largest difference desired. The difference is guaranteed to be finite.

Sample Input

2 2
1 2 5
2 1 4

Sample Output

5

二、思路


  • 差分约束
  • 由于B的糖与A的糖的差不会超过c, B-A <= c 那么求解这类不等式可使用差分约束。B<= A + c 建立一条A–>B 权值为c的边。
  • 求解1和n的最大差值。 那么就是求 n - 1 <= c1 + c2 …cn 后面的C的最大值。转化为求到的是1到n的最短距离。可以使用spfa 或 djkstra求解。
  • 使用spfa时候注意会超时,这时候我们可以使用栈优化一下
  • 由于数据量较大,可以使用快读加快速度。

三、代码

queue换做栈:

#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;
const int N = 3e4 + 5, M = 15e4 + 5;
struct E {
int v, w, next;
} e[M];
int n, m, a, b, w, len = 1, h[N], d[N];
bool vis[N];
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void add(int u, int v, int w) {
e[len].v = v;
e[len].w = w;
e[len].next = h[u];
h[u] = len++;
}
int spfa() {
memset(d, 0x3f, sizeof(d));
d[1] = 0; //求1到n的最短距离就是1和n的最大差值
stack<int> q;
q.push(1);
while (!q.empty()) {
int u = q.top();
q.pop();
vis[u] = false;
for (int j = h[u]; j; j = e[j].next) {
int v = e[j].v;
int w = d[u] + e[j].w;
if (w < d[v]) {
d[v] = w;
if (!vis[v]) q.push(v), vis[v] = true;
}
}
}
return d[n];
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
a = read(); b = read(); w = read();
//scanf("%d%d%d", &a, &b, &w);
add(a, b, w);
}
printf("%d", spfa());
return 0;
}

djkstra:

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N = 3e4 + 5, M = 15e4 + 5;
struct E {
int v, w, next;
} e[M];
struct Node {
int v, d;
Node(int d, int v): d(d), v(v){}
bool operator < (const Node &w) const {
return d > w.d;
}
};
int n, m, a, b, w, len = 1, h[N], d[N];
bool vis[N];
void add(int u, int v, int w) {
e[len].v = v;
e[len].w = w;
e[len].next = h[u];
h[u] = len++;
}
int djkstra() {
memset(d, 0x3f, sizeof(d));
d[1] = 0; //求1到n的最短距离就是1和n的最大差值
priority_queue<Node> q;
q.push(Node(0, 1));
while (!q.empty()) {
int u = q.top().v;
q.pop();
if (vis[u]) continue;
vis[u] = true;
for (int j = h[u]; j; j = e[j].next) {
int v = e[j].v;
int w = d[u] + e[j].w;
if (w < d[v]) {
d[v] = w;
q.push(Node(d[v], v));
}
}
}
return d[n];
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &a, &b, &w);
add(a, b, w);
}
printf("%d", djkstra());
return 0;
}