Some cows 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 ML (1 <= ML <= 10,000) constraints describes which cows like each other and the maximum distance by which they may be separated; a subsequent list of MD constraints (1 <= MD <= 10,000) tells which cows 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 cow 1 and cow N that satisfies the distance constraints.
Input
Lines 2..ML+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at most D (1 <= D <= 1,000,000) apart.
Lines ML+2..ML+MD+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at least D (1 <= D <= 1,000,000) apart.
Output
Sample Input
4 2 1 1 3 10 2 4 20 2 3 3
Sample Output
27
Hint
There are 4 cows. Cows #1 and #3 must be no more than 10 units apart, cows #2 and #4 must be no more than 20 units apart, and cows #2 and #3 dislike each other and must be no fewer than 3 units apart.
The best layout, in terms of coordinates on a number line, is to put cow #1 at 0, cow #2 at 7, cow #3 at 10, and cow #4 at 27.
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<stdio.h> 2 #include<string.h> 3 #include<iostream> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 const int INF=0x3f3f3f3f; 8 int d[1005],dis[1005]; 9 struct shudui 10 { 11 int start,value; 12 }str1; 13 vector<shudui>w[1005]; 14 queue<int>r; 15 int main() 16 { 17 int n,m,k; 18 scanf("%d%d%d",&n,&m,&k); 19 while(m--) 20 { 21 int x,y,z; 22 scanf("%d%d%d",&x,&y,&z); 23 str1.start=y; 24 str1.value=z; 25 w[x].push_back(str1); 26 } 27 while(k--) 28 { 29 int x,y,z; 30 scanf("%d%d%d",&x,&y,&z); 31 str1.start=x; 32 str1.value=-z; 33 w[y].push_back(str1); 34 } 35 for(int i=1;i<n;++i) 36 { 37 str1.start=i; 38 str1.value=0; 39 w[i+1].push_back(str1); 40 } 41 memset(d,INF,sizeof(d)); 42 r.push(1); 43 r.push(1); 44 d[1]=0; 45 int flag=0; 46 while(!r.empty()) 47 { 48 int x=r.front(); 49 r.pop(); 50 int y=r.front(); 51 r.pop(); 52 dis[x]=0; 53 if(y>n) 54 { 55 flag=1; 56 break; 57 } 58 int len=w[x].size(); 59 for(int i=0;i<len;++i) 60 { 61 str1=w[x][i]; 62 if(d[str1.start]>d[x]+str1.value) 63 { 64 d[str1.start]=d[x]+str1.value; 65 if(dis[str1.start]) continue; 66 r.push(str1.start); 67 r.push(y+1); 68 } 69 } 70 } 71 if(d[n]!=INF && !flag) 72 printf("%d\n",d[n]); 73 else if(d[n]==INF) printf("-2\n"); 74 else if(flag) printf("-1\n"); 75 return 0; 76 }
我们还要考虑个问题
本题是让我们求最大值,但是我们建图后为什么求最短路而不是求最长路?
参考博客:https://www.cnblogs.com/cenariusxz/p/4785284.html
首先,我们在最短路初始化的时候所有点的dis值均为INF(极大值),也就是说一开始我们认为n点距离1点是无穷大的。
而对于某一条边 d[v] ≤ d[u] + w(即使≥的也是从这个式子转化而成的),也就是u和v之间距离不能超过w这个条件,只会在d[v] > d[u] + w的时候即u和v之间距离超过w时才会被用来松
弛,而松弛的结果也是使两点距离变成可接受范围内的最大值w,也就是每个边只是松弛到可接受范围内的最大值的。
如果u和v之间的距离已经被其他约束条件限制而小于w了,那么这个条件也就不会起作用也不会使其值更小了,因此最短路求的就是可接受范围内1到n距离的最大值了。
如果是 d[u] - d[v] < w,一般利用整数的情况,就是 d[u] - d[v] ≤ w - 1,也可以顺利建图。
如果是 d[u] - d[v] > w,则建立 d[v] - d[u] ≤ -w-1。
建图之后最大值求最短路,最小值求最长路即可。
更多差分约束题目见:https://blog.csdn.net/whereisherofrom/article/details/78922648