传送门
描述
Nowadays, many people want to go to Shanghai to visit the World Exhibition. So there are always a lot of people who are standing along a straight line waiting for entering. Assume that there are N (2 <= N <= 1,000) people numbered 1..N who are standing in the same order as they are numbered. It is possible that two or more person line up at exactly the same location in the condition that those visit it in a group.
There is something interesting. Some like each other and want to be within a certain distance of each other in line. Some really dislike each other and want to be separated by at least a certain distance. A list of X (1 <= X <= 10,000) constraints describes which person like each other and the maximum distance by which they may be separated; a subsequent list of Y constraints (1 <= Y <= 10,000) tells which person dislike each other and the minimum distance by which they must be separated.Your job is to compute, if possible, the maximum possible distance between person 1 and person N that satisfies the distance constraints.
输入
First line: An integer T represents the case of test.
The next line: Three space-separated integers: N, X, and Y.The next X lines: Each line contains three space-separated positive integers: A, B, and C, with 1 <= A < B <= N. Person A and B must be at most C (1 <= C <= 1,000,000) apart.The next Y lines: Each line contains three space-separated positive integers: A, B, and C, with 1 <= A < B <= C. Person A and B must be at least C (1 <= C <= 1,000,000) apart.
输出
First line: An integer T represents the case of test.
The next line: Three space-separated integers: N, X, and Y.The next X lines: Each line contains three space-separated positive integers: A, B, and C, with 1 <= A < B <= N. Person A and B must be at most C (1 <= C <= 1,000,000) apart.The next Y lines: Each line contains three space-separated positive integers: A, B, and C, with 1 <= A < B <= C. Person A and B must be at least C (1 <= C <= 1,000,000) apart.
样例
- Input14 2 11 3 82 4 152 3 4
- Output19
思路
- 题意:1~N人排队,X行中a和b最多距离c,Y行中a和b最少距离c,求最后1和N之间的最大距离
- 题目求1到N的最大距离,则求图中1到N的最短路
- 对于x,b-a<=c,a到b建权值为c的有向边;对于y,b-a>=c => a-b<=-c,b到a建权值-c的有向边;另外,对于第i个人,i+1 - i >=0 => i - i+1<=0,i+1到i建权值为0的有向边。
- spfa 链式向前星
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | #include |