POJ 3169 Layout (graph theory-differential constraints)

Source: Internet
Author: User

Layout
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6574 Accepted: 3177

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

Field=source&key=usaco+2005+december+gold "style=" Text-decoration:none ">usaco 2005 December Gold


Main topic:

n cows by 1 to n sequence, MD limit and MT Limit, MD line means cow A and cow b difference of up to D,MT limit cow A and cow b difference between the minimum d, ask you cow 1 and cow n the maximum difference?


Problem Solving Ideas:

Restrictions:

1, adjacent cows, the number of large distance greater than the number of small, that is, dist[1]-dist[2]<=0. Dist[2]-dist[3]<=0,dist[3]-dist[4]<=0 .... Dist[n-1]-dist[n]<=0

2, MD limit A and cow B is the most difference D,dist[b]-dist[a]<=d

3, MT limit cow A and cow B difference at least d,dist[b]-dist[a]>=d. namely Dist[b]-dist[a]<=d


V-u<=c, which is the one-way side joining U->V=C

With these elements. Can be solved with a differential constraint. Haha, is not very easy.


Differential constrained learning can be tested: http://www.cnblogs.com/void/archive/2011/08/26/2153928.html


Problem Solving Code:


#include <iostream> #include <queue> #include <cstdio>using namespace Std;const int Maxn=1100;const int maxm=41000;const int inf=0x3f3f3f3f;struct edge{int u,v,w,next;}    E[maxm];int head[maxn],dist[maxn],cnt;int n;void Initial () {cnt=0; for (int i=0;i<=n;i++) head[i]=-1;} void Adde (int u,int v,int W) {e[cnt].u=u,e[cnt].v=v,e[cnt].w=w,e[cnt].next=head[u],head[u]=cnt++;}    void input () {int m,t;    scanf ("%d%d", &m,&t);        while (m-->0) {int u,v,w;        scanf ("%d%d%d", &u,&v,&w);    Adde (U,V,W);        } while (t-->0) {int u,v,w;        scanf ("%d%d%d", &u,&v,&w);    Adde (V,U,-W);        } for (int i=1;i<=n;i++) {if (i>=2) Adde (i,i-1,0);    Adde (0,i,0);    }}bool SPFA (int from) {int S=FROM,NUM[MAXN];    BOOL VISITED[MAXN];        for (int i=0;i<=n;i++) {num[i]=0;        Dist[i]=inf;    Visited[i]=false;    } queue <int> Q;    Q.push (s);    Visited[s]=true; dist[s]=0;    while (!q.empty ()) {S=q.front ();        Q.pop ();            for (int i=head[s];i!=-1;i=e[i].next) {int d=e[i].v;                if (DIST[D]&GT;DIST[S]+E[I].W) {DIST[D]=DIST[S]+E[I].W;                    if (!visited[d]) {visited[d]=true;                    Q.push (d);                    num[d]++;                if (num[d]>n) return false;    }}} Visited[s]=false; } return true;            void Solve () {if (SPFA (0)) {if (SPFA (1)) {if (Dist[n]==inf) printf (" -2\n");        else printf ("%d\n", Dist[n]); }}else printf (" -1\n");}        int main () {while (scanf ("%d", &n)!=eof) {initial ();        Input ();    Solve (); } return 0;}




POJ 3169 Layout (graph theory-differential constraints)

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.