Layout
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 13461 | Accepted: 6455 |
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.
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
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.
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.
Source
题意大体就是::有N坨猪在排队~有ML的条件是A和B的距离要小于D~~有MD个条件是A和B的距离要大于D,求1到N的最大距离~如果没有就输出-1~~如果无限大就输出-2(负环)~;
这道题就用差分约束来做~~ 如果a,b的最大距离为c :: dis[ a ] - dis[ b ] < = c;如果a,b的最小距离为c:dis[ b ] - dis[ a ] < = - val;
因为输入约束条件是按<=输入的~那么最后就按照最短路径的形式遍历计算就好~~
但是我觉得很奇怪的是(感觉是出题人没想到的bug)::如果输出 3 2 0 1 2 2 1 3 1~~表示1和2距离2,1和3距离1~输出是2~~但是觉得很有问题的是~这是按在排队啊!!第3只竟然比第二只距离第一只要远!!估计是没考虑到吧。
#include<cstdio>
#include<queue>
#include<iostream>
using namespace std;
int head[10000];
int m, n1, n2;
struct edge
{
int to;
int ne;
int len;
}ed[10000];
int time[10000];
bool vis[10000];
int d[10000];
int cnt = 0;
void init()
{
for (int s = 0; s <= 9999; s++)
{
head[s] = -1;
}
for (int s = 0; s <= m; s++)
{
d[s] = 999999;
ed[s].ne = -1;
}
memset(vis, 0, sizeof(vis));
memset(time, 0, sizeof(time));
}
void add(int from, int to, int len)
{
ed[cnt].to = to;
ed[cnt].len = len;
ed[cnt].ne = head[from];
head[from] = cnt++;
}
int spfa()
{
d[1] = 0;
queue<int>q;
q.push(1);
vis[1] = 1;
while (!q.empty())
{
int t = q.front();
q.pop();
for (int s = head[t]; ~s; s = ed[s].ne)
{
if (d[ed[s].to] > d[t] + ed[s].len)
{
d[ed[s].to] = d[t] + ed[s].len;
if (!vis[ed[s].to])
{
vis[ed[s].to] = 1;
time[ed[s].to]++;
if (time[ed[s].to] > m)
{
return -1;
}
q.push(ed[s].to);
}
}
}
vis[t] = 0;
}
if (d[m] == 999999)
{
return -2;
}
return d[m];
}
int main()
{
while (cin >> m >> n1>>n2)
{
cnt = 0;
init();
for (int s = 0; s < n1; s++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
}
for (int s = 0; s < n2; s++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(b, a, -c);
}
cout << spfa() << endl;
}
return 0;
}