POJ 3169 Layout
Layout
Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 316964-bit integer IO format: %lld Java class name: Main
Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbered, and since they can be rather pushy, it is possible that two or more cows can line up at exactly the same location (that is, if we think of each cow as being located at some coordinate on a number line, then it is possible for two or more cows to share the same coordinate).
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.
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
Line 1: Three space-separated integers: N, ML, and MD.
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.
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
Line 1: A single integer. If no line-up is possible, output -1. If cows 1 and N can be arbitrarily far apart, output -2. Otherwise output the greatest possible distance between cows 1 and N.
Sample Input
4 2 1 1 3 10 2 4 20 2 3 3
Sample Output
27
Source
解题:差分约束
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 1010; 18 struct arc{ 19 int to,w; 20 arc(int x = 0,int y = 0){ 21 to = x; 22 w = y; 23 } 24 }; 25 vector<arc>g[maxn]; 26 queue<int>q; 27 int d[maxn],cnt[maxn],N,ML,MD; 28 bool used[maxn]; 29 void spfa(){ 30 for(int i = 1; i <= N; i++){ 31 d[i] = INF; 32 cnt[i] = 0; 33 used[i] = false; 34 } 35 d[1] = 0; 36 used[1] = true; 37 q.push(1); 38 while(!q.empty()){ 39 int u = q.front(); 40 q.pop(); 41 used[u] = false; 42 for(int i = 0; i < g[u].size(); i++){ 43 if(d[g[u][i].to] > d[u] + g[u][i].w){ 44 d[g[u][i].to] = d[u] + g[u][i].w; 45 if(!used[g[u][i].to]){ 46 q.push(g[u][i].to); 47 used[g[u][i].to] = true; 48 cnt[g[u][i].to]++; 49 if(cnt[g[u][i].to] > N){ 50 puts("-1"); 51 return; 52 } 53 } 54 } 55 } 56 } 57 if(d[N] == INF) puts("-2"); 58 else printf("%d\n",d[N]); 59 } 60 int main(){ 61 int i,u,v,w; 62 while(~scanf("%d %d %d",&N,&ML,&MD)){ 63 for(i = 0; i <= N; i++) g[i].clear(); 64 for(i = 0; i < ML; i++){ 65 scanf("%d %d %d",&u,&v,&w); 66 g[u].push_back(arc(v,w)); 67 } 68 for(i = 0; i < MD; i++){ 69 scanf("%d %d %d",&u,&v,&w); 70 g[v].push_back(arc(u,-w)); 71 } 72 for(i = 1; i < N; i++) 73 g[i+1].push_back(arc(i,0)); 74 spfa(); 75 } 76 return 0; 77 }
夜空中最亮的星,照亮我前行