Description
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
Hint
32-bit signed integer type is capable of doing all arithmetic.
大致题意 : 有n个小孩分糖果, 对每对小孩A,B有A比B最多不多于C个糖果, 求N比1最多能多多少个糖果.
思路 : 这题是到典型的差分约束(本蒟蒻一开始甚至不知道是啥)的题, 于是去搜了搜什么是差分约束系统
知识点: 差分约束系统
即有多个x-y<=c的不等式约束条件, 问是否有解的问题
而有关差分约束的问题可以通过转化为图论的最短路问题解决, 因为在求解不等式的过程中我们把x,y,c换成d[v] d[u] w[u][v], 就会变成d[v]-d[u]<=w[u,v], 即最短路的松弛操作条件d[v]<=d[u]+w[u,v]
对于本题, 我们可以把每个小朋友的条件看做差分约束来进行建图, 最后用Dijkstra算法走一遍, 求1到n的最短路, 答案就出来啦
代码如下:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <iterator>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <iomanip>
using namespace std;
stringstream ss;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 30010, M = 150010;
const int INF = 0x3f3f3f3f;
int n,m;
int h[N],e[M],ne[M],idx,w[M];
int dist[N];
bool st[N];
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
void dijkstra()
{
memset(dist, 0x3f, sizeof dist);
memset(st, 0, sizeof st);
dist[1] = 0;
priority_queue<PII, vector<PII>, greater<PII> > heap;
heap.push({0,1});
while(heap.size())
{
PII t = heap.top();
heap.pop();
int ver = t.second;
if(st[ver]) continue;
st[ver] = 1;
for(int i = h[ver]; ~i; i = ne[i])
{
int j = e[i];
if(dist[j] > dist[ver] + w[i])
{
dist[j] = dist[ver] + w[i];
heap.push({dist[j], j});
}
}
}
}
int main()
{
int n,m;
scanf("%d %d", &n, &m);
memset(h, -1, sizeof h);
while(m -- )
{
int a,b,c;
scanf("%d %d %d", &a, &b, &c);
add(a,b,c);
}
dijkstra();
cout<<dist[n]<<endl;
}