Layout
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.
Source
题目类型:差分约束系统
算法分析:由题目可知要求解的是从顶点1到n的最小距离,在nl条边中建从小标号到大标号边权为w的有向边,在nd条边中建从大标号到小标号边权为-w的有向边。最后若图中存在负权值回路,则输出-1。若最后dis[n] = INF,则表示顶点1和n之间没有关系,输出-2。最后若dis[n] = k,则输出k
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
/************************************************* Author :supermaker Created Time :2016/3/24 13:56:18 File Location :C:\Users\abcd\Desktop\TheEternalPoet **************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1000 + 66; struct Node { int v, w, nxt; }; int head[maxn], edgelen; Node edge[maxn*maxn]; void Init () { memset (head, -1, sizeof (head)); memset (edge, -1, sizeof (edge)); edgelen = 0; } void AddEdge (int u, int v, int w) { edge[edgelen].v = v; edge[edgelen].w = w; edge[edgelen].nxt = head[u]; head[u] = edgelen++; } int n, nl, nd, dis[maxn], inq[maxn], cnt[maxn]; bool is_valid; void spfa (int s) { for (int i = 1; i <= n; i++) { dis[i] = INF; inq[i] = cnt[i] = 0; } dis[s] = 0; is_valid = true; queue <int> qu; qu.push (s); inq[s]++, cnt[s]++; while (!qu.empty ()) { int tt = qu.front (); qu.pop (); inq[tt]--; if (cnt[tt] > n) { is_valid = false; return ; } int pa = head[tt]; while (pa != -1) { int pb = edge[pa].v; if (dis[tt] < INF && dis[pb] > dis[tt] + edge[pa].w) { dis[pb] = dis[tt] + edge[pa].w; if (!inq[pb]) { qu.push (pb); inq[pb]++, cnt[pb]++; } } pa = edge[pa].nxt; } } } int main() { //CFF; //CPPFF; while (scanf ("%d%d%d", &n, &nl, &nd) != EOF) { Init (); for (int i = 1; i <= nl; i++) { int u, v, w; scanf ("%d%d%d", &u, &v, &w); int minv = min (u, v), maxv = max (u, v); AddEdge (minv, maxv, w); } for (int i = 1; i <= nd; i++) { int u, v, w; scanf ("%d%d%d", &u, &v, &w); int minv = min (u, v), maxv = max (u, v); AddEdge (maxv, minv, -w); } spfa (1); if (!is_valid) puts ("-1"); else { if (dis[n] == INF) puts ("-2"); else printf ("%d\n", dis[n]); } } return 0; } |
- « 上一篇:poj2607
- poj2337:下一篇 »