hdu-3592 World Exhibition (differential constraint)

Source: Internet
Author: User

Topic Links:

World Exhibition

Time limit:2000/1000 MS (java/others)

Memory limit:32768/32768 K (java/others)


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 Test Instructions:n individual stand into a row, there are two people between the minimum distance, there are two of the maximum distance between people, ask the first person to nth person distance between the maximum is how much; Ideas:Such inequalities can be obtained by test instructions:x Line:dis[b]-dis[a]<=c;dis[a]-dis[b]<=0;y line:dis[a]-dis[b]<=-c;this is translated into the shortest path of the writing, of course, can also be translated into the longest path;when not satisfied is 1, when 1 and N is not connected is-2; otherwise it is dis[n];with SPFA algorithm good fast; AC Code:
//#include <bits/stdc++.h>#include <iostream>#include<queue>#include<cmath>#include<cstring>#include<algorithm>#include<cstdio>using namespacestd;#defineRiep (n) for (int i=1;i<=n;i++)#defineRIOP (n) for (int i=0;i<n;i++)#defineRJEP (n) for (int j=1;j<=n;j++)#defineRJOP (n) for (int j=0;j<n;j++)#defineMST (SS,B) memset (ss,b,sizeof (ss));typedefLong LongLL;ConstLL mod=1e9+7;Const DoublePi=acos (-1.0);Const intinf=0x3f3f3f3f;Const intn=2e4+5;intCnt,n,x,y,vis[n],head[n],num[n];intDis[n];structedge{intTo,next,val;} edge[4*N];voidAdd_edge (intSintEintval) {edge[cnt].to=e; Edge[cnt].next=Head[s]; Edge[cnt].val=Val; Head[s]=cnt++;} Queue<int>Qu;voidSPFA () { while(!qu.empty ()) Qu.pop (); MST (Vis,0);    MST (Dis,inf); MST (NUM,0); Qu.push (1); dis[1]=0; vis[1]=1;  while(!Qu.empty ()) {        intFr=Qu.front ();        Qu.pop (); NUM[FR]++; if(num[fr]>N) {printf ("-1\n"); return ; }         for(inti=head[fr];i!=-1; i=Edge[i].next) {            intx=edge[i].to; if(dis[x]>dis[fr]+edge[i].val) {Dis[x]=dis[fr]+Edge[i].val; if(!Vis[x])                    {Qu.push (x); VIS[X]=1; }}} Vis[fr]=0; }    if(Dis[n]==inf) printf ("-2\n"); Elseprintf"%d\n", Dis[n]);}intMain () {intT; scanf ("%d",&t);  while(t--) {CNT=0; MST (Head,-1); intu,v,w; scanf ("%d%d%d",&n,&x,&y); Riep (x) {scanf ("%d%d%d",&u,&v,&W);            Add_edge (U,V,W); Add_edge (V,u,0); } Riep (y) {scanf ("%d%d%d",&u,&v,&W); Add_edge (V,u,-W);    } SPFA (); }    return 0;}

hdu-3592 World Exhibition (differential constraint)

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.