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个点,m对 A B c 表示B-A<=c
要求1和n的差值最大
每个约束B-A<=c 就是B<=A+c 加边A->B 为c的边。
建图以后就是求最短路。
思路:非常经典的一道最短路的题了,有30005个点,用邻接表+堆优化的dijkstra做。
注:之所以用邻接表是因为不能定义a[30005][30005], 也间接说明了数据量很大,所有读入数据一定要用scanf,用cin会超时。
偶然发现了kuangbin本人的博客,引用了一下题意:https://www.cnblogs.com/kuangbin/p/3141375.html
代码:
#include <iostream>
#include <cstring>
#include <queue>
#include <stdio.h>
#define inf 0x3f3f3f
using namespace std;
typedef struct node { // 定义优先队列
int v, date;
} ty;
bool operator < ( const ty &a, const ty &b ) // 定义优先队列
{
return a.date>b.date;
}
const int N = 30005;
struct edge {
int v,w,next;
} a[150005]; // 这个存边,设的大一点
int head[N];
int cnt;
int dis[30005];
int via[30005];
int n,m;
ty t,d;
void dijkstra( int v0 ) // 求最短路。堆优化的dijkstra
{
int i;
memset(dis,inf,sizeof(dis));
dis[v0] = 0;
priority_queue <ty> Q;
t.v = v0;
t.date = 0;
Q.push(t);
while ( !Q.empty() ) {
if ( via[Q.top().v]==1 ) {
Q.pop();
continue ;
}
t = Q.top();
Q.pop();
int u = t.v;
dis[u] = t.date;
via[u] = 1;
for ( i=head[u]; i!=-1; i=a[i].next ) { // 这一块for代码容易出错,好好理解
int j = a[i].v;
if ( via[j]==0 && dis[j]>dis[u]+a[i].w ) {
dis[j] = dis[u]+a[i].w;
d.v = j;
d.date = dis[j];
Q.push(d);
}
}
}
cout << dis[n] << endl;
}
int main()
{
int i,j;
cin >> n >> m;
memset(via,0,sizeof(via));
memset(head,-1,sizeof(head)); // head初始化为-1
cnt = 0;
for ( i=0; i<m; i++ ) {
int x,y,z;
scanf("%d %d %d",&x,&y,&z); // 用邻接表的时候必须用scanf
a[cnt].v = y; // 用cin超时
a[cnt].w = z;
a[cnt].next = head[x];
head[x] = cnt++;
}
dijkstra(1);
return 0;
}