Layout POJ - 3169
Like others, cows like to be close to their friends when they line up to feed. FJ has n (2 < = n < = 1000) cows numbered 1... N standing in a straight line waiting for feeding. Cows stand in the same order as the numbers, and since they can be quite aggressive, it is possible that two or more cows can line up in exactly the same position (that is, if we think each cow is located at a coordinate on the number axis, it is possible that two or more cows share the same coordinate).
Some cows like each other and want to keep a certain distance from each other. Some people really hate each other and want to be separated at least a certain distance. Ml (1 < = ml < = 10000) constraint list describes which cows like each other and the maximum distance they can be separated; The subsequent MD constraint list (1 < = MD < = 10000) indicates which cows do not like each other and the minimum distance they must be separated.
If possible, your job is to calculate the maximum possible distance between cow 1 and cow N that meet the distance constraint.
input
Line 1: three space separated integers: N, ML, and MD.
Line 2... ML+1: each line contains three positive integers separated by spaces: A, B and D, where 1 < = a < B < = n. Cows a and B must be separated by D at most (1 < = d < = 1000000).
ML+2... ML+MD+1 line: each line contains three space separated positive integers: A, B and D, where 1 < = a < B < = n. Cows a and B must be separated by at least d (1 < = d < = 1000000).
output
Line 1: single integer. If there is no possible queue, output - 1. If cows 1 and N can be any distance, output - 2. Otherwise, the maximum possible distance between cow 1 and N is output.
Sample input
4 2 1
1 3 10
2 4 20
2 3 3
Sample output
27
hint
Sample description:
There are four cows. The distance between cows #1 and #3 shall not exceed 10 units, the distance between cows #2 and #4 shall not exceed 20 units, and cows #2 and #3 dislike each other and shall not be less than 3 units apart.
In terms of coordinates on the number axis, the best layout is to place cow #1 at 0, cow #2 at 7, cow #3 at 10 and cow #4 at 27.
Train of thought; With numbers 1... N standing on a straight line waiting for feeding, this sentence can be imagined as a number axis. 2 is behind 1, 3 is behind 2, and the entered AB must meet a < B. therefore, for ML, there is B-A < = w, and the shift B < = a + W, so there is a w weight path from a to B. for MD, there is B-A > = w, and the shift gets a < = B + (- W), so there is a (- W) weight path from B to a. Then run some of the shortest paths. If book [x] > = n, output - 1. If dis[n]==inf, output - 2, the others are true.
#include <cstring> #include <iostream> #include <queue> #include<stdio.h> using namespace std; typedef long long ll; const int N=1000100; int dis[N],head[N],Cnt[N],vist[N],cnt,n; struct Edge{ int to; ll w; int next; }edge[N<<1]; void add_edge(int u,int v,ll w) { edge[cnt].to=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++; } bool SPFA() { memset(vist,0,sizeof(vist)); memset(Cnt,0,sizeof(Cnt)); dis[1]=0; queue<int> q; q.push(1); vist[1]=1; while(!q.empty()){ int cur=q.front(); vist[cur]=0; q.pop(); for(int i=head[cur];~i;i=edge[i].next){ int next=edge[i].to; if(dis[next]>dis[cur]+edge[i].w){ dis[next]=dis[cur]+edge[i].w; if(!vist[next]){ q.push(next); vist[next]=1; Cnt[next]++; if(Cnt[next]>n) return false; } } } } return true; } int main() { int i,j,l,d,a,b; ll w; while(cin>>n>>l>>d){ memset(head,-1,sizeof(head)); cnt=0; memset(dis,0x3f3f3f,sizeof(dis)); int inf=dis[0]; for(i=1;i<=l;i++){ scanf("%d%d%lld",&a,&b,&w); add_edge(a,b,w); } for(i=1;i<=d;i++){ scanf("%d%d%lld",&a,&b,&w); add_edge(b,a,-w); } if(!SPFA()){ printf("-1\n"); } else{ if(dis[n]==inf){ printf("-2\n"); } else cout<<dis[n]<<endl; } } }