World Exhibition
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1448 Accepted Submission(s): 715
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.
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.
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
Author
alpc20
Source
Recommend
题意:现有n个人,给你x+y个信息,前x条是说a--b之间最多相差c,
后y条信息说明a--b之间最少相差c,求1--n之间的最大距离,若距
离任意输出-1,如果不存在输出-2,否则就输出最大距离
建图条件:(以1为源点)
1.dis[b]-dis[a]<=c
2.dis[b]-dis[a]>=c----dis[a]-dis[b]<=-c
3.dis[i+1]-dis[i]>=0----dis[i]-dis[i+1]<=0
后y条信息说明a--b之间最少相差c,求1--n之间的最大距离,若距
离任意输出-1,如果不存在输出-2,否则就输出最大距离
建图条件:(以1为源点)
1.dis[b]-dis[a]<=c
2.dis[b]-dis[a]>=c----dis[a]-dis[b]<=-c
3.dis[i+1]-dis[i]>=0----dis[i]-dis[i+1]<=0
#include<stdio.h> #include<string.h> #include<iostream> #include<queue> #include<algorithm> using namespace std; #define MAXN 10010 #define MAXM 500000 #define INF 0x3f3f3f int head[MAXN],vis[MAXN],dis[MAXN],used[MAXN]; int n,x,y,cnt; struct node { int u,v,val; int next; }edge[MAXM]; void init() { memset(head,-1,sizeof(head)); cnt=0; } void add(int u,int v,int val) { edge[cnt].u=u; edge[cnt].v=v; edge[cnt].val=val; edge[cnt].next=head[u]; head[u]=cnt++; } void getmap() { for(int i=1;i<n;i++) add(i+1,i,0); int a,b,c; while(x--) { cin>>a>>b>>c; add(a,b,c); } while(y--) { cin>>a>>b>>c; add(b,a,-c); } } void SPFA() { memset(vis,0,sizeof(vis)); memset(dis,INF,sizeof(dis)); dis[1]=0; vis[1]=1; used[1]++; queue<int>q; q.push(1); while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=0; for(int i=head[u];i!=-1;i=edge[i].next) { node E=edge[i]; if(dis[E.v]>dis[u]+E.val) { dis[E.v]=dis[u]+E.val; if(!vis[E.v]) { vis[E.v]=1; used[E.v]++; if(used[E.v]>n) { cout<<-1<<endl; return ; } q.push(E.v); } } } } if(dis[n]>=INF) cout<<-2<<endl; else cout<<dis[n]<<endl; } int main() { int t; cin>>t; while(t--) { cin>>n>>x>>y; init(); getmap(); SPFA(); } return 0; }