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.
飞鼠的幼儿园班上经常发糖果,全班Infinite个糖果由飞鼠分配给包括飞鼠(和史努比)在内的n个孩子。表现乖的人得到的糖果多很正常,但其中可能 有小孩A 觉得 无论自己的糖果多么少,另一个小孩B都不能得到 比自己多 超过c个的糖果。飞鼠不敢让同学们不满意,因为他们会告诉老师。
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?
史努比和飞鼠在同一个班上,飞鼠经常跟他攀比。飞鼠希望在自己不被告发的前提下,使自己得到比史努比尽量多的糖果,并央求你告诉他“飞鼠的糖果 - 史努比的糖果”数目的最大值。
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.
多组数据,输入到文件末尾
每组数据开头n和m,(m表示对糖果数的m对要求,飞鼠标号为n,史努比标号为1)
下面m行每行A、B、C,表示糖果数要满足“B的糖果数 ≤ A的糖果数 + C”
Output one line with only the largest difference desired. The difference is guaranteed to be finite.
每组数据一行答案,保证有解。所有数都在int范围内。
分析一下这道题的条件,设c[i]表示 i 的糖果数,发现“c[B] <= c[A] + C” 相似于 “dis[B] <= dis[A] + weight”,后者是一张图中每个点到原点最短路满足的条件,而且,每个点的最短路都是满足上述条件的最大值。于是,把A向B连一条边权为C的边,再从1到n跑一遍最短路就完了。(建议别用SPFA)