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.
题意
共有NN个人按的编号排队,给出XX组互相亲近的人,之间的距离不能超过 ,给出YY组互相厌恶的人,之间的距离不能小于,问第11个人和第个人之间的最大距离
题解
依题意
对于相互亲近的人有 dB−dA≤CdB−dA≤C 即 w(A,B)=Cw(A,B)=C
对于相互厌恶的人有 dB−dA≥CdB−dA≥C 即 w(B,A)=−Cw(B,A)=−C
由于NN个人按的编号排队,则有d[i+1]−d[i]≥0d[i+1]−d[i]≥0 即 w(i+1,i)=0w(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;
}

该博客讨论了一个关于世博会排队的问题,其中人们根据喜好关系需要保持特定距离。问题要求计算第一个人和最后一个人间能实现的最大距离,满足喜欢彼此的人之间不超过设定距离,不喜欢的人之间至少保持设定距离。通过构建约束图并寻找最短路径来解决此问题。如果存在负环,则表示无法满足条件。
140

被折叠的 条评论
为什么被折叠?



