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
首先题目中的隐含条件是
d[i] <= d[i + 1]
即d[i + 1] + 0 >= d[i]
然后对那ML个条件
d[y] - d[x] <= w -> d[x] + w >= d[y]
对那MD个条件
d[y] - d[x] >= w -> d[y] + (-w) >= d[x]
然后呢就可以加边了。
对于一个不等式 d[u] + w >= d[v] 我们可以加边(u, v , w)
这是由于在一个图里,从s出发,到各个顶点的v最短距离为d[v],那么对一个边(u, v, w) 必然有d[u] + w >= d[v]
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 55555
#define maxm 222222
int n;
int head[maxm], tol=1,dis[maxn],cnt[maxn];
bool vis[maxn];
struct edge
{
int to, next;
int cost;
}g[maxm];
void add(int u, int v, int c)
{
g[tol].cost = c;
g[tol].to = v;
g[tol].next = head[u];
head[u] = tol++;
}
int spfa()
{
for (int i = 2; i<maxn; i++)
dis[i] = INF;
queue<int>que;
cnt[1]++;
dis[1] = 0;
vis[1] = 1;
que.push(1);
while (!que.empty())
{
int u = que.front();
que.pop();
vis[u] = 0;
for (int i = head[u]; i != 0; i = g[i].next)
{
int v = g[i].to;
int c = g[i].cost;
if (dis[v]>dis[u] + c)
{
dis[v] = dis[u] + c;
if (!vis[v])
{
vis[v] = 1;
que.push(v);
cnt[v]++;
if (cnt[v]>n)return 1;
}
}
}
}
return 0;
}
int main()
{
int ml, md, u, v, w;
scanf("%d%d%d", &n, &ml, &md);
while (ml--)
{
scanf("%d%d%d", &u, &v, &w);
add(u, v, w);
}
while (md--)
{
scanf("%d%d%d", &v, &u, &w);
add(u, v, -w);
}
for (int i = 1; i < n; i++)
add(i + 1, i, 0);
if (spfa())printf("-1\n");
else if (dis[n] == INF)printf("-2\n");
else printf("%d\n", dis[n]);
return 0;
}