题目链接:
World Exhibition
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
Problem Description
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个人站成一行,有两个人之间的距离最少是多少,有两个人之间的距离最大是多少,问第一个人到第 n个人之间的距离最大是多少;
思路:
由题意可以得到这样的不等式:
X行:
dis[B]-dis[A]<=C;
dis[A]-dis[B]<=0;
Y行:
dis[A]-dis[B]<=-C;
这是转化成最短路径的写法,当然也可以转化成求最长路径;
当无法满足的时候就是-1,当1和n不连通的时候就是-2;否则就是dis[n]了;
用spfa算法好快;
AC代码:
//#include <bits/stdc++.h> #include <iostream> #include <queue> #include <cmath> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; #define Riep(n) for(int i=1;i<=n;i++) #define Riop(n) for(int i=0;i<n;i++) #define Rjep(n) for(int j=1;j<=n;j++) #define Rjop(n) for(int j=0;j<n;j++) #define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; const LL mod=1e9+7; const double PI=acos(-1.0); const int inf=0x3f3f3f3f; const int N=2e4+5; int cnt,n,x,y,vis[N],head[N],num[N]; int dis[N]; struct Edge { int to,next,val; }edge[4*N]; void add_edge(int s,int e,int val) { edge[cnt].to=e; edge[cnt].next=head[s]; edge[cnt].val=val; head[s]=cnt++; } queue<int>qu; void spfa() { while(!qu.empty())qu.pop(); mst(vis,0); mst(dis,inf); mst(num,0); qu.push(1); dis[1]=0; vis[1]=1; while(!qu.empty()) { int fr=qu.front(); qu.pop(); num[fr]++; if(num[fr]>n) { printf("-1\n"); return ; } for(int i=head[fr];i!=-1;i=edge[i].next) { int x=edge[i].to; if(dis[x]>dis[fr]+edge[i].val) { dis[x]=dis[fr]+edge[i].val; if(!vis[x]) { qu.push(x); vis[x]=1; } } } vis[fr]=0; } if(dis[n]==inf)printf("-2\n"); else printf("%d\n",dis[n]); } int main() { int t; scanf("%d",&t); while(t--) { cnt=0; mst(head,-1); int u,v,w; scanf("%d%d%d",&n,&x,&y); Riep(x) { scanf("%d%d%d",&u,&v,&w); add_edge(u,v,w); add_edge(v,u,0); } Riep(y) { scanf("%d%d%d",&u,&v,&w); add_edge(v,u,-w); } spfa(); } return 0; }