ACM: spfa求解 图论题 (对差分约束…
来源:互联网 发布:杭州创业软件官网 编辑:程序博客网 时间:2024/05/29 13:20
Description
Like everyone else, cows like to stand close to their friendswhen queuing for feed. FJ has N (2 <= N<= 1,000) cows numbered 1..N standing along astraight line waiting for feed. The cows are standing in the sameorder as they are numbered, and since they can be rather pushy, itis possible that two or more cows can line up at exactly the samelocation (that is, if we think of each cow as being located at somecoordinate on a number line, then it is possible for two or morecows to share the same coordinate).
Some cows like each other and want to be within a certain distanceof each other in line. Some really dislike each other and want tobe separated by at least a certain distance. A list of ML (1<= ML <= 10,000) constraintsdescribes which cows like each other and the maximum distance bywhich they may be separated; a subsequent list of MD constraints (1<= MD <= 10,000) tells which cowsdislike each other and the minimum distance by which they must beseparated.
Your job is to compute, if possible, the maximum possible distancebetween cow 1 and cow N that satisfies the distanceconstraints.
Input
Line 1: Three space-separated integers: N, ML, and MD.
Lines 2..ML+1: Each line contains three space-separated positiveintegers: 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-separatedpositive integers: A, B, and D, with 1 <= A< B <= N. Cows A and B must be atleast 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. Otherwiseoutput 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 unitsapart, 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 unitsapart.
The best layout, in terms of coordinates on a number line, is toput cow #1 at 0, cow #2 at 7, cow #3 at 10, and cow #4 at 27.
题意:
解题思路:
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
#define MAX 10005
const int INF =
struct node
{
}edges[2*MAX];
int n;
int ml, md;
int first[MAX];
int ans[MAX];
int num;
bool vis[MAX];
void init()
{
}
void spfa()
{
}
int main()
{
//
}
- ACM: spfa求解 图论题 (对差分约束…
- ACM: 差分约束 图论题 poj 2983 sp…
- ACM: 图论题 poj 1201 差分约束
- ACM: 图论题 poj 1275 差分约束题
- ACM: spfa+dfs 图论题 poj 2679
- ACM: 图论题 poj3259 bellman_ford…
- ACM: 图论题 poj 3…
- ACM: 图论题 poj 1…
- ACM: 图论题 poj 1…
- ACM: 图论题 BFS + STL + priority…
- ACM: 图论题 poj 2240 (floyd + ST…
- ACM: 匈牙利算法 图论题 poj 3041 …
- ACM: 有向欧拉图 图论题 poj 1386 …
- poj 3159 差分约束+最短…
- poj 1364 King 差分约束
- poj 1201 Intervals (差分约束)
- poj 1364 King (差分约束)
- ACM: 二分图最佳匹配 图论题 poj 3…
- ACM: hash题 poj 2503
- ACM: 哈希暴力求解 哈希题 poj 184…
- ACM: 最小费用最大流 图论题 poj 2…
- ACM: K次最小费用最大流 图论题 po…
- ACM: 最小费用用最大流 图论题 poj…
- ACM: spfa求解 图论题 (对差分约束…
- ZOJ的轻松ACMER语录.. 狗一样的学…
- ACM: 二分法 数论题 poj 1905
- 数据
- ACM: 二分图最大匹配 hdu 2063
- ACM: 差分约束 图论题 poj 2983 sp…
- ACM: 最大流 图论题 poj 1797
- 图论: 最大流
- 图论: 最小费用最大流