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

Some cows like each other and want to be within a 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 ML (1 <= ML <= 10,000) constraints describes which cows like each other and the maximum distance by which they may be separated; a subsequent list of MD constraints (1 <= MD <= 10,000) tells which cows 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 cow 1 and cow N that satisfies the distance constraints.

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. Cows A and B must be at 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 be 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 1
1 3 10
2 4 20
2 3 3

Sample Output

27

Hint

Explanation of the sample: 

There are 4 cows. Cows #1 and #3 must be no more than 10 units apart, cows #2 and #4 must be no more than 20 units apart, and cows #2 and #3 dislike each other and must be no fewer than 3 units apart. 

The best layout, in terms of coordinates on a number line, is to put cow #1 at 0, cow #2 at 7, cow #3 at 10, and cow #4 at 27.

 

思路:

这题很明显就是差分约束的题。做了之后算是巩固了差分约束的建边关系吧。

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <iostream>
  4 #include <algorithm>
  5 #include <vector>
  6 #include <queue>
  7 #include <set>
  8 #include <map>
  9 #include <string>
 10 #include <math.h>
 11 #include <stdlib.h>
 12 #include <time.h>
 13 using namespace std;
 14 
 15 #define INF 0x3f3f3f3f
 16 #define MAXN 100000
 17 #define LL long long
 18 using namespace std;
 19 
 20 struct Edge{
 21     int to;
 22     int next;
 23     int val;
 24 }edge[MAXN];
 25 
 26 
 27 int res;
 28 int cnt[MAXN];
 29 int head[MAXN];
 30 int dist[MAXN];
 31 bool vis[MAXN];
 32 
 33 void init()
 34 {
 35     memset(head,-1, sizeof(head));
 36     res = 0;
 37 }
 38 
 39 void add(int u,int v,int w)
 40 {
 41     edge[res].to = v;
 42     edge[res].val = w;
 43     edge[res].next = head[u];
 44     head[u] = res++;
 45 }
 46 
 47 bool SPFA(int n)
 48 {
 49     memset(vis,false,sizeof(vis));
 50     memset(cnt,0, sizeof(cnt));
 51     for (int i=1;i<=n;i++)
 52         dist[i]=INF;
 53     dist[1] = 0;
 54     vis[1] = true;
 55     cnt[1]++;
 56     deque<int > q;
 57     q.push_back(1);
 58     while (!q.empty())
 59     {
 60         int now = q.front();
 61         q.pop_front();
 62         vis[now] = false;
 63         for (int i=head[now];i!=-1;i=edge[i].next)
 64         {
 65             int v = edge[i].to;
 66             if (dist[v]>dist[now]+edge[i].val) {
 67                 dist[v] = dist[now] + edge[i].val;
 68                 if (!vis[v])
 69                 {
 70                     if (dist[v]<dist[q.front()] && !q.empty())
 71                         q.push_front(v);
 72                     else
 73                         q.push_back(v);
 74                     vis[v] = true;
 75                     if (++cnt[v]>n)
 76                         return false;
 77                 }
 78             }
 79         }
 80     }
 81     return true;
 82 }
 83 
 84 
 85 
 86 int main()
 87 {
 88     int n,ml,md;
 89     scanf("%d%d%d",&n,&ml,&md);
 90     init();
 91     for (int i=1;i<=ml;i++)
 92     {
 93         int u,v,w;
 94         scanf("%d%d%d",&u,&v,&w);
 95         if (u>v)
 96             swap(u,v);
 97         //大-小<=c ,有向边(小,大):c
 98         add(u,v,w);
 99     }
100     for (int i=1;i<=md;i++)
101     {
102         int u,v,w;
103         scanf("%d%d%d",&u,&v,&w);
104         if (u<v)
105             swap(u,v);
106         //大-小>=c,小-大<=-c,有向边(大,小):-c
107         add(u,v,-w);
108     }
109     if (!SPFA(n))
110         printf("-1\n");
111     else if (dist[n] == INF)
112         printf("-2\n");
113     else
114         printf("%d\n",dist[n]);
115     return 0;
116 }