problem
Nowadays, many people want to go to Shanghai to visit the World Exhibition. So there are always a lot of people who are standing along a straight line waiting for entering. Assume that there are N (2 <= N <= 1,000) people numbered 1..N who are standing in the same order as they are numbered. It is possible that two or more person line up at exactly the same location in the condition that those visit it in a group.
There is something interesting. Some 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 X (1 <= X <= 10,000) constraints describes which person like each other and the maximum distance by which they may be separated; a subsequent list of Y constraints (1 <= Y <= 10,000) tells which person 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 person 1 and person N that satisfies the distance constraints.
Input
First line: An integer T represents the case of test.
The next line: Three space-separated integers: N, X, and Y.
The next X lines: Each line contains three space-separated positive integers: A, B, and C, with 1 <= A < B <= N. Person A and B must be at most C (1 <= C <= 1,000,000) apart.
The next Y lines: Each line contains three space-separated positive integers: A, B, and C, with 1 <= A < B <= C. Person A and B must be at least C (1 <= C <= 1,000,000) apart.
Output
For each line: A single integer. If no line-up is possible, output -1. If person 1 and N can be arbitrarily far apart, output -2. Otherwise output the greatest possible distance between person 1 and N.
题意
共有 N N 个人按的编号排队,给出 X X 组互相亲近的人,之间的距离不能超过 ,给出 Y Y 组互相厌恶的人,之间的距离不能小于,问第 1 1 个人和第个人之间的最大距离
题解
依题意
对于相互亲近的人有
dB−dA≤C
d
B
−
d
A
≤
C
即
w(A,B)=C
w
(
A
,
B
)
=
C
对于相互厌恶的人有
dB−dA≥C
d
B
−
d
A
≥
C
即
w(B,A)=−C
w
(
B
,
A
)
=
−
C
由于
N
N
个人按的编号排队,则有
d[i+1]−d[i]≥0
d
[
i
+
1
]
−
d
[
i
]
≥
0
即
w(i+1,i)=0
w
(
i
+
1
,
i
)
=
0
对约束图求最短路即为解
若存在负环即无解
代码
#include<bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int maxn = 1100;
struct node
{
int to,w;
node (int i=0,int j=0)
{
to = i, w = j;
}
};
int N,d[maxn],cnt[maxn];
bool vis[maxn];
vector <node> e[maxn];
bool spfa()
{
memset(vis,0,sizeof(vis));
memset(cnt,0,sizeof(cnt));
for (int i=1;i<=N;i++) d[i] = inf;
queue <int> Q;
Q.push(1);
d[1] = 0, vis[1] = 1;
while (!Q.empty())
{
int u = Q.front();
for (int i=0;i<e[u].size();i++)
{
int v = e[u][i].to;
int dis = e[u][i].w;
if (d[v] > d[u]+dis)
{
d[v] = d[u]+dis;
if (!vis[v])
{
vis[v] = 1;
Q.push(v);
cnt[v]++;
if (cnt[v] > N) return false;
}
}
}
Q.pop();
vis[u] = 0;
}
return true;
}
int main()
{
int T,X,Y,A,B,C;
scanf("%d",&T);
while (T--)
{
scanf("%d %d %d",&N,&X,&Y);
for (int i=1;i<=N;i++) e[i].clear();
for (int i=0;i<X;i++)
{
scanf("%d %d %d",&A,&B,&C);
e[A].push_back(node(B,C));
}
for (int i=0;i<Y;i++)
{
scanf("%d %d %d",&A,&B,&C);
e[B].push_back(node(A,-C));
}
for (int i=1;i<N;i++)
e[i+1].push_back(node(i,0));
if (spfa())
{
if (d[N] == inf) printf("-2\n");
else
printf("%d\n",d[N]);
}
else
printf("-1\n");
}
return 0;
}