博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj3169——Layout(差分约束+SPFA判断负环)
阅读量:2343 次
发布时间:2019-05-10

本文共 2756 字,大约阅读时间需要 9 分钟。

Description

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

典型的差分约束,包含a-b<=d,那么b+d>=a,构造权值为d的b->a有向边。a-b>=d,那么b-a<=-d,a+(-d)>=b,构造权值为-d的a->b有向边。

输出-2的情形说明1和n之间没有不等式的约束,反映到图中就是两点之间没有·通路

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f#define MAXN 200010#define Mod 10001using namespace std;struct Edge{ int v,w,next;};Edge edge1[MAXN<<1];int head1[MAXN],n,m,e,vis[MAXN],dis[MAXN];int q[MAXN],p[MAXN];void add(Edge *edge,int *head,int u,int v,int w){ edge[e].v=v; edge[e].w=w; edge[e].next=head[u]; head[u]=e; e++;}void spfa(Edge *edge,int *head,int u){ memset(vis,0,sizeof(vis)); memset(p,0,sizeof(p)); for(int i=1; i<=n; ++i) dis[i]=INF; dis[u]=0; queue
q; q.push(u); while(!q.empty()) { u=q.front(); q.pop(); vis[u]=0; p[u]++; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v,w=edge[i].w; if(w+dis[u]
n) printf("-1\n"); else if(dis[n]==INF) printf("-2\n"); else printf("%d\n",dis[n]); } return 0;}

转载地址:http://lscvb.baihongyu.com/

你可能感兴趣的文章
视图包含下列结构是不可以更新的
查看>>
可能返回 null 的 SQL 语句
查看>>
以下关于STL的描述中,错误的有
查看>>
假设某棵二叉查找树的所有键均为1到10的整数,现在我们要查找5。下面____不可能是键的检查序列。
查看>>
给定一个整数sum,从有N个无序元素的数组中寻找元素a、b、c、d,使得 a+b+c+d =sum,最快的平均时间复杂度是____。
查看>>
设二叉树结点的先根序列、中根序列和后根序列中,所有叶子结点的先后顺序____。
查看>>
将整数序列(7-2-4-6-3-1-5)按所示顺序构建一棵二叉排序树a(亦称二叉搜索树),之后将整数8按照二叉排序树规则插入树a中,请问插入之后的树a中序遍历结果是____。
查看>>
IP地址、子网掩码、网络号、主机号、网络地址、主机地址
查看>>
已知int a[]={1,2,3,4,5};int*p[]={a,a+1,a+2,a+3};int **q=p;表达式*(p[0]+1)+**(q+2)的值是____。
查看>>
CPU输出数据的速度远远高于打印机的打印速度,为了解决这一矛盾,可采用()
查看>>
整型字符常量和字符字面量的区别 sizeof(char) 和 sizeof('a')
查看>>
表的主键特点中,说法不正确的是()
查看>>
用变量a给出下面的定义:一个有10个指针的数组,该指针指向一个函数,该函数有一个整形参数并返回一个整型数
查看>>
冯诺依曼工作方式的基本特点是____
查看>>
下列关于文件索引结构的叙述中,哪些是正确的?
查看>>
虚拟存储的容量受到下列哪一个因素的限制影响最大?
查看>>
关于域名和IP描述正确的是?
查看>>
哪些字段适合建立索引?
查看>>
关于group by子句的作用描述正确的是?
查看>>
执行"int x=1;int y=~x;"语句后,y的值为?-----取反运算,补码
查看>>