POJ 3169 Layout (differential constrained system + Bellman-ford algorithm)

Source: Internet
Author: User

Layout
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7613 Accepted: 3658

Description

Like everyone else, the cows like to stand close to their friends when the queuing for feed. FJ has n (2 <= n <= $) cows numbered 1..N standing along a straight line waiting for feed. The cows was standing in the same order as they was numbered, and since they can be rather pushy, it was possible that Or more cows can line up at exactly the same location (that's, if we think of each cow as being located at some Coordinat E on a number line and then it was possible for the or more cows to share the same coordinate).

Some cows 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 ML (1 <= ml <=) constraints describes which cows like all other and the maximum distance by which They may separated; A subsequent list of MD constraints (1 <= MD <=) tells which cows dislike each other and the minimum distance By which they must is separated.

Your job is to compute, if possible, the maximum possible distance between cow 1 and cow N that satisfies the distance con Straints.

Input

Line 1:three space-separated integers:n, ML, and MD.

Lines 2..ml+1:each Line contains three space-separated positive integers:a, B, and D, with 1 <= A < B <= N. Cow s A and B must is at the most D (1 <= D <= 1,000,000) apart.

Lines Ml+2..ml+md+1:each Line contains three space-separated positive integers:a, B, and D, with 1 <= A < B <= N. Cows A and B must is at least D (1 <= D <= 1,000,000) apart.

Output

Line 1: A single integer. If no line-up is possible, output-1. If cows 1 and N can be arbitrarily far apart, output-2. Otherwise output The greatest possible distance between cows 1 and N.

Sample Input

4 2 11 3 102 4 202 3 3

Sample Output

27

Hint

Explanation of the sample:

There is 4 cows. Cows #1 and #3 must is no more than units apart, cows #2 and #4 must is no more than, units apart, and cows #2 and #3 Dislike each and must is no fewer than 3 units apart.

The best layout, with terms of coordinates on a number line, was to put cow #1 at 0, Cow #2 at 7, Cow #3 at ten, and cow #4 at 27.

Source

Usaco 2005 December Gold



Test instructions: A barn has n cows, some cows have a better relationship, they want to do not exceed a certain distance. Of course, some cows don't have a good relationship, they want to be more than a certain distance. ML has a good relationship with cows and gives a distance of not more than D for each pair of cows; Similarly, there is a poor relationship between the MD and the cow, and gives the distance d for each pair of cows. Ask if there are any plans to meet the requirements of all cows. If not present, output 1, if present, but the distance between the Ox 1 and the ox n can be arbitrarily large, output-2; otherwise, the maximum distance between the Ox 1 and the ox n is output.


Resolution: The position of the cow I is d[i], first, the bull in accordance with the number sequence, so there is d[i] <= d[i+1] was established. Secondly, the maximum distance for a good cow is limited by the D[al] + DL >= D[BL], and the minimum distance limit for cows with poor relationships is D[ad] + DD <= D[BD]. In this way, the original problem can be transformed into the problem of solving the maximum value of d[n]-d[1] in the case of satisfying the three inequality groups. Of course, the linear programming method can be used to solve. But the problem can be solved more easily. This can be abstracted as an Shang graph, each cow as a vertex, a good or bad relationship between them for the edge, the limit of the distance as the edge of the value. This can be converted to these three inequalities: D[i+1] + 0 >= d[i] means from i+1 to I with a weight of 0 edge, D[al] + DL >= d[bl] means from AL to BL with a weight of the DL, D[BD]-DD >= D[ad] for the B D to ad with an edge that has a weighted value of-DD. The maximum value for D[n]-d[1], corresponding to the shortest path between d[1] to D[n]. Due to the existence of negative weights, the Bellman-ford algorithm is used.




AC Code:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < vector> #include <queue> #include <set> #include <map> #include <string> #include <cmath > #include <cstdlib> #include <ctime> #include <stack>using namespace std; #define INF 123456789# Define LL long Long#define MID (A, B) A + (b-a)/2const int maxn = + 10;const int MAXM = 10000 + 10;int AL[MAXM], Bl[max             M], Dl[maxm];int AD[MAXM], BD[MAXM], Dd[maxm];int d[maxn];int N, ML, md;void solve () {fill (d, D+n, INF);    Initialize d[] d[0] = 0;  for (int i=0, i<n; i++) {for (int j=0; j+1<n; j + +)//check d[j] if d[j+1] slack if (d[j+1] < INF)        D[j] = min (D[j], d[j+1] + 0); for (int j=0; j<ml; j + +)//check D[BL] if D[al] slack if (d[al[j]-1] < INF) d[Bl[j]-1] = min (d[b        L[J]-1], d[Al[j]-1] + dl[j]);            for (int j=0; j<md; j + +)//check D[ad] can be relaxed by D[BD]if (d[Bd[j]-1] < INF) d[Ad[j]-1] = min (d[ad[j]-1], d[Bd[j]-1]-dd[j]);    } int ans = d[n-1];    if (D[0] < 0) ans =-1;    else if (ans = = INF) ans =-2; printf ("%d\n", ans);}    int main () {#ifdef sxk freopen ("In.txt", "R", stdin); #endif//Sxk while (scanf ("%d%d%d", &n, &ml, &AMP;MD)!=eof) {for (int i=0; i<ml; i++) scanf ("%d%d%d",        &al[i], &bl[i], &dl[i]);        for (int i=0; i<md; i++) scanf ("%d%d%d", &ad[i], &bd[i], &dd[i]);    Solve (); } return 0;}




POJ 3169 Layout (differential constrained system + Bellman-ford algorithm)

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.