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.
Sample Input
1 4 2 1 1 3 8 2 4 15 2 3 4
Sample Output
19
题意:N个人编号为1-N,并且按照编号顺序排成一条直线,任何两个人的位置不重合,然后给定一些约束条件。X(X <= 100000)组约束Ax Bx Cx(1 <= Ax < Bx <= N),表示Ax和Bx的距离不能大于Cx。Y(X <= 100000)组约束Ay By Cy(1 <= Ay < By <= N),表示Ay和By的距离不能小于Cy。 如果这样的排列存在,输出1-N这两个人的最长可能距离,如果不存在,输出-1,如果无限长输出-2。
思路:这道题的话,不难看出是差分约束,令第x个人的位置为d[x],下面我们列出题里所给的约束条件。
1、对于所有的Ax Bx Cx,有 d[Bx] - d[Ax] <= Cx;
2、对于所有的Ay By Cy,有 d[By] - d[Ay] >= Cy;
3、然后根据我们的设定,有 d[x] >= d[x-1] + 1 (1 < x <= N) (这个条件是表示任何两个人的位置不重合)
而我们需要求的是d[N] - d[1]的最大值,即表示成d[N] - d[1] <= T,要求的就是这个T。
所以我们将所有的不等式都转化成d[x] - d[y] <= z的形式,如下:
1、d[Bx] - d[Ax] <= Cx
2、d[Ay] - d[By] <= -Cy
3、d[x-1] - d[x] <= -1
对于d[x] - d[y] <= z,令z = w(y, x),那么有 d[x] <= d[y] + w(y, x),所以当d[x] > d[y] + w(y, x),我们需要更新d[x]的值,这对应了最短路的松弛操作,于是问题转化成了求1到N的最短路。对于所有满足d[x] - d[y] <= z的不等式,从y向x建立一条权值为z的有向边。 然后从起点1出发,利用spfa求到各个点的最短路,如果1到N不可达,说明最短路(即上文中的T)无限长,输出-2。如果某个点进入队列大于等于N次,则必定存在一条负环,即没有最短路,输出-1。否则T就等于1到N的最短路。
AC代码:
#include <bits/stdc++.h>
typedef long long ll;
const int maxx=1010;
const int maxn=20010;
const int inf=0x3f3f3f3f;
using namespace std;
struct node
{
int v;
int w;
int next;
} edge[maxn];
int head[maxx],dis[maxx],flag[maxx];
bool vis[maxx];
int cnt,n,x,y;
void init()
{
memset(head,-1,sizeof(head));
cnt=0;
}
void add(int a,int b,int w)
{
edge[cnt].v=b;
edge[cnt].next=head[a];
edge[cnt].w=w;
head[a]=cnt++;
}
int spfa(int u)
{
for(int i=1; i<=n; i++)
dis[i]=inf;
memset(vis,0,sizeof(vis));
memset(flag,0,sizeof(flag));
dis[u]=0;
vis[u]=true;
queue<int>q;
q.push(u);
while(!q.empty())
{
int x=q.front();
if(flag[x]>n)
return 0;
q.pop();
vis[x]=false;
for(int i=head[x]; i!=-1; i=edge[i].next)
{
int v=edge[i].v;
int w=edge[i].w;
if(dis[v]>w+dis[x])
{
dis[v]=w+dis[x];
if(!vis[v])
{
q.push(v);
vis[v]=true;
flag[v]++;
}
}
}
}
return 1;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
init();
scanf("%d%d%d",&n,&x,&y);
for(int i=0; i<x; i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
for(int i=0; i<y; i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(b,a,-c);
}
for(int i=1; i<=n; i++)
add(i,i-1,0);
for(int i=1; i<=n; i++)
add(0,i,0);
if(!spfa(1))
printf("-1\n");
else if(dis[n]==inf)
printf("-2\n");
else
printf("%d\n",dis[n]);
}
}