Layout
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15349   Accepted: 7379

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

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

 
题目意思:
给n,m1,m2
n头牛,每头牛跟其他的牛直接的距离有一定的约束
m1个约束1,m2个约束2
约束1:
a b c 表示a牛和b牛之间的距离最多c
约束2:
a b c 表示a牛和b牛之间的距离最少c
问你两头牛之间的最大距离至少是多少才能满足所有的约束
分析:
x[i]表示牛i的在x[i]处或者说牛i在距离原点x[i]的地方
约束1可以表示为:
x[a]-x[b]<=c
约束2可以表示为:
x[b]-x[a]<=-c
<=代表的是最大值,代表的是最短路,表达式形式为x[i]-x[j]<=c
按照j到i建图,权值为c
然后起点是1,跑个最短路(不能使用dj,因为存在负权)
1到n的最短路就是能满足所有牛约束的最小距离值
code:
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
#define INF 9999999999
#define me(a,x) memset(a,x,sizeof(a))
int mon1[]= {,,,,,,,,,,,,};
int mon2[]= {,,,,,,,,,,,,};
int dir[][]= {{,},{,-},{,},{-,}}; int getval()
{
int ret();
char c;
while((c=getchar())==' '||c=='\n'||c=='\r');
ret=c-'';
while((c=getchar())!=' '&&c!='\n'&&c!='\r')
ret=ret*+c-'';
return ret;
}
void out(int a)
{
if(a>)
out(a/);
putchar(a%+'');
} #define max_v 1005
struct node
{
int v;
LL w;
node(int vv=,LL ww=):v(vv),w(ww){}
};
LL dis[max_v];
int vis[max_v];
int cnt[max_v];
vector<node> G[max_v];
queue<int> q; void init()
{
for(int i=;i<max_v;i++)
{
G[i].clear();
dis[i]=INF;
vis[i]=;
cnt[i]=;
}
while(!q.empty())
q.pop();
} int spfa(int s,int n)
{
vis[s]=;
dis[s]=;
q.push(s);
cnt[s]++; while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=; for(int j=;j<G[u].size();j++)
{
int v=G[u][j].v;
LL w=G[u][j].w; if(dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
if(vis[v]==)
{
q.push(v);
cnt[v]++;
vis[v]=; if(cnt[v]>n)
return ;
}
}
}
}
return ;
}
int f(int u,int v)
{
for(int j=;j<G[u].size();j++)
{
if(G[u][j].v==v)
return ;
}
return ;
}
int main()
{
int n,a,b;
while(~scanf("%d %d %d",&n,&a,&b))
{
init();
int x,y,w;
while(a--)
{
scanf("%d %d %d",&x,&y,&w);
if(f(x,y))
G[x].push_back(node(y,w));
}
while(b--)
{
scanf("%d %d %d",&x,&y,&w);
if(f(y,x))
G[y].push_back(node(x,-w));
}
int flag=spfa(,n);
if(flag==)
{
printf("-1\n");
}else if(dis[n]<INF)
{
printf("%lld\n",dis[n]);
}else
{
printf("-2\n");
}
}
return ;
}
/*
题目意思:
给n,m1,m2
n头牛,每头牛跟其他的牛直接的距离有一定的约束
m1个约束1,m2个约束2
约束1:
a b c 表示a牛和b牛之间的距离最多c
约束2:
a b c 表示a牛和b牛之间的距离最少c
问你两头牛之间的最大距离至少是多少才能满足所有的约束 分析:
x[i]表示牛i的在x[i]处或者说牛i在距离原点x[i]的地方 约束1可以表示为:
x[a]-x[b]<=c
约束2可以表示为:
x[b]-x[a]<=-c <=代表的是最大值,代表的是最短路,表达式形式为x[i]-x[j]<=c
按照j到i建图,权值为c
然后起点是1,跑个最短路(不能使用dj,因为存在负权)
1到n的最短路就是能满足所有牛约束的最小距离值 */

poj 3169 Layout(线性差分约束,spfa:跑最短路+判断负环)的更多相关文章

  1. poj 3169 Layout(差分约束+spfa)

    题目链接:http://poj.org/problem?id=3169 题意:n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0.这些牛的距离存在着一些约束关系:1.有m ...

  2. (简单) POJ 3169 Layout,差分约束+SPFA。

    Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...

  3. HDU 3592 World Exhibition(线性差分约束,spfa跑最短路+判断负环)

    World Exhibition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. POJ 3169 Layout 【差分约束】+【spfa】

    <题目链接> 题目大意: 一些母牛按序号排成一条直线.有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离.如果没有最大距离输出-1,如果1.n之间距离任意就 ...

  5. poj 3169 Layout (差分约束)

    3169 -- Layout 继续差分约束. 这题要判起点终点是否连通,并且要判负环,所以要用到spfa. 对于ML的边,要求两者之间距离要小于给定值,于是构建(a)->(b)=c的边.同理,对 ...

  6. POJ 3169 Layout(差分约束+最短路)题解

    题意:有一串数字1~n,按顺序排序,给两种要求,一是给定u,v保证pos[v] - pos[u] <= w:二是给定u,v保证pos[v] - pos[u] >= w.求pos[n] - ...

  7. poj 3169 Layout(差分约束)

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6549   Accepted: 3168 Descriptio ...

  8. O - Layout(差分约束 + spfa)

    O - Layout(差分约束 + spfa) Like everyone else, cows like to stand close to their friends when queuing f ...

  9. POJ 3167 Layout(差分约束)

    题面 Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 ...

随机推荐

  1. C 中结构体对齐

    参考 百度百科内存对齐 对齐作用 可以使得以最少的次数将操作数加载到寄存器中,如果数据没有对齐,则当CPU以最小读取数据大小从内存读入数据时可能只取到了一部分数据,而对齐情况下可以一次读入. 对齐修改 ...

  2. 页面中多个script块之间方法与变量共享问题

    JS是按照代码块来进行编译和执行的,代码块间相互独立,但变量和方法共享,按顺序执行. 如: <script type='text/javascript'> var m = 0,n = 1, ...

  3. 【读书笔记】iOS-网络-HTTP-请求内容

    一,GET方法. 从服务器获取一段内容,用HTTP术语来说就是实体.GET请求通常不包含请求体,不过也是可以包含的.有些网络缓存设施只会缓存GET响应.GET请求通常不会导致服务器端的数据变化. 二, ...

  4. vuex 管理vue-router的传值

    假设有这样的一种情况,在两个组件中.一个组件[A]主要是比如说放表格数据,而另外一个组件[B]是专门用来向组件A的表格添加数据的表单.这个时候就是两个兄弟组件之间传递数据了.首先想到的是使用兄弟组件传 ...

  5. ArcGIS 中取出面上最大的Z值的坐标点

    def MaxZ(shape):        line = shape.getPart(0)     pnt = line.next()     maxValue = float("-in ...

  6. 深入解读《Gartner2017年商业智能和分析平台魔力象限报告》

    文 | 帆软数据应用研究院 船长 2017年2月16日,Gartner发布了2017年BI商业智能和分析平台魔力象限报告,笔者这里进行一些解读,帮助大家更好了解市场状况和趋势. 一.几家欢笑几家愁 和 ...

  7. Origin绘制双Y轴图的方法

    1.已有数据绘图如下,其中网络流量的单位是M Bytes/s,与另外两组数据的单位(时间)不同,现在要为其添加右侧Y轴. 2.首先选中该图像,找到工具条,点击第三个按钮“Add Right-Y Lay ...

  8. JavaScript判断变量名是否存在数组中

    直接上代码: JavaScript代码: var array=[{name:"张珊",sex:"男"}]; console.log(array); if(arr ...

  9. Android比较实用的性能优化

    Android设备作为一种移动设备,无论是内存还是CPU的性能都受到了很大的限制,这导致Android程序的性能问题异常突出,随着产品的不断更新迭代,对于性能优化提出了更高的要求.本篇文章从稳定性.流 ...

  10. Python Django框架笔记(四):数据分页和CSRF跨站点请求伪造

    (一)数据分页  可以参考  https://docs.djangoproject.com/en/2.0/topics/pagination/ 模板:如果只要显示 1.2.3.4.5.6....的话, ...