HDU-3592 World Exhibition difference Constraint

Source: Internet
Author: User
World Exhibition

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 568 accepted submission (s): 264

Problem description

Nowadays,
Please 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
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
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
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 input1
4 2 1
1 3 8
2 4 15
2 3 4

Sample output19 this question is to list all the relationships. At the beginning, I understood this question as an undirected graph. The result is decisive and incorrect. In fact, this question is a directed graph. Because there is no possibility of going back when computing queues exist, if there are discontinuous constraints in the middle, there is no link between them and output-2, that is, there is no link between 1 and N, if the output value is-1, a negative ring occurs. I don't know why this problem occurs. When constructing an edge, you can use the inverse adjacent table, and the adjacent table is always wa. The Code is as follows:
# Include <cstdio> # include <cstring> # include <iostream> # include <cstdlib> # include <vector> # include <queue> # define INF 0x7f7f7f7fusing namespace STD; struct node {int POs, DIS;} Info; int dis [1005], Ti [1005], inqu [1005]; bool spfa (int n, vector <node> VEC []) {queue <int> q; vector <node>: iterator it; for (INT I = 0; I <= N; ++ I) {dis [I] = inf; Ti [I] = 0; inqu [I] = 0;} dis [1] = 0; q. push (1); inqu [1] = 1; while (! Q. empty () {int Pos = Q. front (); q. pop (); inqu [POS] = 0; For (IT = VEC [POS]. begin (); it! = VEC [POS]. end (); ++ it) {If (DIS [it-> POS]> dis [POS] + It-> dis) {dis [it-> POS] = dis [POS] + It-> DIS; If (! Inqu [it-> POS]) {q. push (IT-> POS); inqu [it-> POS] = 1; Ti [it-> POS] ++; If (Ti [it-> POS]> N) {return false ;}}}return true;} int main () {int t, x, y, z, n, x, y; scanf ("% d ", & T); While (t --) {scanf ("% d", & N, & X, & Y); vector <node> VEC [1, 1005]; for (INT I = 1; I <= x; ++ I) {scanf ("% d", & X, & Y, & Z); info. pos = Y, info. dis = z; VEC [X]. push_back (Info) ;}for (INT I = 1; I <= y; ++ I) {scanf ("% d", & X, & Y, & Z); info. pos = x, info. dis =-Z; VEC [Y]. push_back (Info);}/* For (INT I = 2; I <= N; ++ I) {info. pos = I, info. dis = 0; VEC [I-1]. push_back (Info);} * // If (spfa (n, VEC) {If (DIS [N] = inf) {printf ("-2 \ n");} else {printf ("% d \ n", DIS [N]-Dis [1]);} else {printf ("-1 \ n") ;}} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.