首页 > 其他 > 详细

POJ 3169 Layout (差分约束+SPFA)

时间:2014-07-30 23:50:55      阅读:537      评论:0      收藏:0      [点我收藏+]
Layout
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6832   Accepted: 3292

Description

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.

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.

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

Hint

Explanation of the sample: 

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.
 

题意:输入N, ML, MD, N表示有N个牛按1-N排成一排,ML,表示有ML行,每行输入A, B, D表示A牛和B牛最远距离为D, MD表示有MD行,每行输入A,B,D表示A牛和B牛最近距离为D,求满足所有条件的1-N的最大距离。

 

思路:

从题目样例来看 N = 4, ML = 2, MD = 1

1和牛3之间距离最大为10,牛2和牛4之间距离最大为20

2和牛3之间距离最短为3,设d[i]为牛i到牛1的距离,所以可以列出方程组:

d[3] - d[1] <= 10

d[4] - d[2] <= 20

d[3] - d[2] >= 3

还有隐含条件 d[i] <= d[i+1]

典型的差分约束。

可以把方程组写成如下形式:

d[3] - d[1] <= 10

d[4] - d[2] <= 20

d[2] - d[3] <= -3 则最后可以得出d[N] - d[1] <= x,建图用spfa求最短路即可。

(差分约束做最短路的时候得到的是最大值,做最长路的时候得到的是最小值)

需要注意的是如果求不出最短路,则输出-1,若1-N可以是无穷,则输出-2。那么要再加上一个判断负环操作。

 

 

 1 #include <iostream>
 2 #include <queue>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6 #define maxn 1010
 7 #define maxm 55000
 8 #define INF 999999999
 9 int head[maxn], dist[maxn], ans; 
10 int N, ML, MD, a, b, c;
11 struct Edge{
12     int u, v, w, next;
13 }edge[maxm];
14 void addedge(int u, int v, int w){
15     edge[ans].u = u; edge[ans].v = v; edge[ans].w = w;
16     edge[ans].next = head[u]; head[u] = ans++;
17 }
18 bool spfa(int start){
19     int judge[maxn];     //判断负环 
20     queue <int> q;
21     bool inq[maxn];
22     for(int i = 0;i <= N; i++){
23         judge[i] = 0;
24         dist[i] = INF;
25     }
26     memset(inq, 0, sizeof(inq));    
27     q.push(start);
28     inq[start] = 1;
29     dist[start] = 0;
30     while(!q.empty()){
31         int x = q.front(); q.pop();
32         for(int i = head[x]; i != -1; i = edge[i].next){ 
33              if(dist[edge[i].v] > dist[x] + edge[i].w){
34                 dist[edge[i].v] = dist[x] + edge[i].w;
35                 if(!inq[edge[i].v]){       //如果已经在队列中了,就不要重复加了 
36                     inq[edge[i].v] = 1;
37                     q.push(edge[i].v); 
38                     judge[edge[i].v]++;
39                     if(judge[edge[i].v] > N) return false;
40                 }
41             }
42         }
43         inq[x] = 0;
44     }
45     return true;
46 }
47 int main(){
48     while(scanf("%d%d%d",&N, &ML, &MD)!=EOF){
49         ans = 0; //初始化边数为0 
50         memset(head, -1, sizeof(head));
51            while(ML--){    // d[b] - d[a] <= c
52             scanf("%d%d%d",&a,&b,&c);
53             addedge(a,b,c);
54         }
55         while(MD--){   //d[a] - d[b] <= -c
56             scanf("%d%d%d",&a,&b,&c); 
57             addedge(b,a,-c);
58         }
59         for(int i = 2; i <= N; i++)   addedge(i,i-1,0); //d[i-1] - d[i] <= 0
60                     
61         if(spfa(1)){            
62             if(dist[N] == INF) printf("-2\n");
63             else printf("%d\n",dist[N]);
64             
65         }
66         
67         else printf("-1\n");
68     }
69     
70     return 0;
71 }

 

 
 

POJ 3169 Layout (差分约束+SPFA),布布扣,bubuko.com

POJ 3169 Layout (差分约束+SPFA)

原文:http://www.cnblogs.com/titicia/p/3879485.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!