1242 Layout
2005 Usaco
time limit: 1 sspace limit: 128000 KBtitle level: Golden Gold
<:section class= "Hbox" ><:aside class= "col-sm-11 bg-white-only" >
Title Description
Description
When waiting in line for feeding, cows like to stand close to their friends. FJ have n (2<=n<=1000) cows, numbered from 1 to N, standing along a straight line waiting to be fed. The order of the cows in the queue is the same as their number. Because cows are quite slim, there may be two or more cows standing in the same position. Even if we imagine cows standing on a single axis, it allows two or more cows to have the same horizontal axis.
Some cows are fond of each other, and they hope that the distance between the two is no more than a given number L. On the other hand, some cows are very disgusted with each other, and they hope that the distance between the two is not less than a given number d. This article gives a description of the good graces between two cows, and gives a description of the dislike of the two cows by the MD bar. (1<=ml,md<=10000,1<=l,d<=1000000)
Your job is: if there are no scenarios to meet the requirements, output-1; If number 1th cows and n
The distance between cows can be arbitrarily large, output-2; otherwise, the maximum distance between the number 1th cows and the n cows may be calculated if all the requirements are met.
Enter a description
Input Description 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 description
Output Description 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
Sample Input
4 2 1
1 3 10
2 4 20
2 3 3
Sample output
Sample Output
27
Data range and Tips
Data Size & Hint Solvingdifferential!!! (I'm not very good at learning ... )
1#include <cstdio>2#include <queue>3#include <iostream>4#include <algorithm>5 #defineMAXN 10000+106 #defineINF 0x7fffffff7 8 using namespacestd;9 Ten intHEAD[MAXN],CNT,DIS[MAXN],VIS[MAXN],INQ[MAXN],N,ML,MD; One A structSS - { - intTo,next,edge; the}e[100000<<1]; - - voidInsertintUintVintedge) - { +e[++cnt].to=v,e[cnt].next=head[u],e[cnt].edge=edge,head[u]=CNT; - } + A intSPFA () at { -queue<int>que; -dis[1]=0; -vis[1]=1; -inq[1]++; -Que.push (1); in intflag=0; - while(!que.empty ()) to { + intNow=que.front (); Que.pop (); vis[now]=0; - for(intI=head[now];i;i=e[i].next) the { * if(dis[e[i].to]>dis[now]+E[i].edge) $ {Panax Notoginsengdis[e[i].to]=dis[now]+E[i].edge; - if(!vis[e[i].to]) the { +vis[e[i].to]=1; Que.push (e[i].to); Ainq[e[i].to]++; the if(inq[e[i].to]>n)return-1; + } - } $ $ } - } - returnDis[n]; the } - Wuyi intMain () the { -scanf"%d%d%d",&n,&ml,&MD); Wu for(intI=1; i<=ml;i++) - { About intx, y, z $scanf"%d%d%d",&x,&y,&z); - Insert (x, y, z); - } - for(intI=1; i<=md;i++) A { + intx, y, z thescanf"%d%d%d",&x,&y,&z); -Insert (y,x,-z); $ } the for(intI=1; i<=n;i++) dis[i]=INF; the intans=SPFA (); the if(Ans==inf) cout<<-2<<Endl; the Elsecout<<ans<<Endl; - return 0; in}
C + + path advanced codevs1242 (layout)