World ExhibitionTime
limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1373 Accepted Submission (s): 673
Problem Descriptionnowadays, many people want to go to Shanghai to visit the World exhibition. So there is always a lot of people who is standing along a straight line waiting for entering. Assume that there is n (2 <= N <=) People numbered 1..N who is standing in the same order as they is number Ed. It is possible, that, or more person, line up at exactly, the same location in the condition, those visit it in a Grou P.
There is something interesting. Some like each of the other, and want to is within a certain distance of each and the line. Some really dislike each, and want to is separated by at least a certain distance. A List of x (1 <= x <=) constraints describes which person like each other and the maximum distance by which They may separated; A subsequent list of y constraints (1 <= y <=) tells which person dislike all other and the minimum distance By which they must is separated.
Your job is to compute, if possible, the maximum possible distance between person 1 and person N that satisfies the Distan CE constraints.
Inputfirst 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 is at the 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 is at least C (1 <= C <= 1,000,000) apart.
Outputfor each line:a a single integer. If no line-up is possible, output-1. If Person 1 and N can is arbitrarily far apart, output-2. Otherwise output The greatest possible distance between person 1 and N.
Sample Input
14 2 11 3 82 4 152 3 4
Sample Output
19
Authoralpc20
Source2010 acm-icpc multi-university Training Contest (--host by Nudt)
/* ***********************************************author:ckbosscreated time:2015 July 30 Thursday 08:16 29 seconds file Name : hdoj3592.cpp************************************************ * * #include <iostream> #include <cstdio># Include <cstring> #include <algorithm> #include <string> #include <cmath> #include <cstdlib > #include <vector> #include <queue> #include <set> #include <map>using namespace Std;const int Maxn=1111;const int inf=0x3f3f3f3f;struct edge{int to,next,cost;} Edge[30300];int adj[maxn],size;void init () {memset (adj,-1,sizeof (ADJ)); size=0;} void Add_edge (int u,int v,int c) {Edge[size].to=v;edge[size].cost=c;edge[size].next=adj[u]; adj[u]=size++;} int N,m1,m2;int dist[maxn],cq[maxn];bool inq[maxn];bool SPFA (int rt) {memset (dist,63,sizeof)); Dist (Memset, sizeof (CQ)); memset (Inq,false,sizeof (INQ));d ist[rt]=0;queue<int> q;inq[rt]=true; Q.push (RT); Cq[rt]=1;while (!q.empty ()) {int U=q.front (); Q.pop (); for (int i=adj[u];~i;I=edge[i].next) {int v=edge[i].to;if (dist[v]>dist[u]+edge[i].cost) {dist[v]=dist[u]+edge[i].cost;if (!inq[v]) { Inq[v]=true;cq[v]++;if (cq[v]>=n) return False;q.push (v);}} Inq[u]=false;} return true;} int main () {//freopen ("In.txt", "R", stdin),//freopen ("OUT.txt", "w", stdout), int t_t;scanf ("%d", &t_t); while (T_ t--) {scanf ("%d%d%d", &n,&m1,&m2), init (); int u,v,c;for (int i=0;i<m1;i++) {scanf ("%d%d%d", &u, &V,&C); Add_edge (u,v,c);} for (int i=0;i<m2;i++) {scanf ("%d%d%d", &u,&v,&c); Add_edge (v,u,-c);} BOOL FG=SPFA (1), if (Fg==false) puts ("1"), else if (Dist[n]==inf) puts ("2"), Else printf ("%d\n", Dist[n]);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDOJ 3592 World Exhibition differential constraint