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).Description

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.

因为做差分约束的题目不能保证图的联通,所以要建立超级源点,也可以直接将每一个点放入队列中,因为若图中有两个联通分量,只能便利第一个不能访问第二个,不能保证图的另一部分不存在负环。

所以这个题目要先跑一遍D(0)就是超级源点,然后若存在负环即无解就不求1——N的距离了,有解再求,两遍SPFA。

但是在看RQ的博客的时候发现了一个特殊的建图方式,因为编号小的一定在编号大的左边,我没有考虑,所以这个题数据比较弱,然后下面的代码。

AC代码1:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define INF 1e9
using namespace std;
const int maxn=1000+10;
const int maxm=50000+10; struct Edge
{
int from,to,dist;
Edge(){}
Edge(int f,int t,int d):from(f),to(t),dist(d){}
}; struct BellmanFord
{
int n,m;
int head[maxn],next[maxm];
Edge edges[maxm];
bool inq[maxn];
int cnt[maxn];
int d[maxn]; void init(int n)
{
this->n=n;
m=0;
memset(head,-1,sizeof(head));
} void AddEdge(int from,int to,int dist)
{
edges[m]=Edge(from,to,dist);
next[m]=head[from];
head[from]=m++;
} int bellmanford(int s)
{
memset(inq,0,sizeof(inq));
memset(cnt,0,sizeof(cnt));
queue<int> Q;
for(int i=1;i<=n;i++) d[i]= i==s?0:INF;
Q.push(s); while(!Q.empty())
{
int u=Q.front(); Q.pop();
inq[u]=false;
for(int i=head[u];i!=-1;i=next[i])
{
Edge &e=edges[i];
if(d[e.to] > d[u]+e.dist)
{
d[e.to] = d[u]+e.dist;
if(!inq[e.to])
{
inq[e.to]=true;
Q.push(e.to);
if(++cnt[e.to]>n) return -1;
}
}
}
}
return d[n]==INF?-2:d[n];
}
}BF; int main()
{
int n,ml,md;
while(scanf("%d%d%d",&n,&ml,&md)==3)
{
BF.init(n);
while(ml--)
{
int u,v,d;
scanf("%d%d%d",&u,&v,&d);
BF.AddEdge(u,v,d);
}
while(md--)
{
int u,v,d;
scanf("%d%d%d",&u,&v,&d);
BF.AddEdge(v,u,-d);
}
for(int i=1;i<=n;i++)
BF.AddEdge(0,i,0);
if(BF.bellmanford(0)!=-1) printf("%d\n",BF.bellmanford(1));
else puts("-1");
}
return 0;
}
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define INF 1e9
using namespace std;
const int maxn=1000+10;
const int maxm=50000+10; struct Edge
{
int from,to,dist;
Edge(){}
Edge(int f,int t,int d):from(f),to(t),dist(d){}
}; struct BellmanFord
{
int n,m;
int head[maxn],next[maxm];
Edge edges[maxm];
bool inq[maxn];
int cnt[maxn];
int d[maxn]; void init(int n)
{
this->n=n;
m=0;
memset(head,-1,sizeof(head));
} void AddEdge(int from,int to,int dist)
{
edges[m]=Edge(from,to,dist);
next[m]=head[from];
head[from]=m++;
} int bellmanford()
{
memset(inq,0,sizeof(inq));
memset(cnt,0,sizeof(cnt));
queue<int> Q;
for(int i=1;i<=n;i++) d[i]= i==1?0:INF;
Q.push(1); while(!Q.empty())
{
int u=Q.front(); Q.pop();
inq[u]=false;
for(int i=head[u];i!=-1;i=next[i])
{
Edge &e=edges[i];
if(d[e.to] > d[u]+e.dist)
{
d[e.to] = d[u]+e.dist;
if(!inq[e.to])
{
inq[e.to]=true;
Q.push(e.to);
if(++cnt[e.to]>n) return -1;
}
}
}
}
return d[n]==INF?-2:d[n];
}
}BF; int main()
{
int n,ml,md;
while(scanf("%d%d%d",&n,&ml,&md)==3)
{
BF.init(n);
while(ml--)
{
int u,v,d;
scanf("%d%d%d",&u,&v,&d);
BF.AddEdge(u,v,d);
}
while(md--)
{
int u,v,d;
scanf("%d%d%d",&u,&v,&d);
BF.AddEdge(v,u,-d);
}
for(int i=2;i<=n;i++)
BF.AddEdge(i,i-1,0);
printf("%d\n",BF.bellmanford());
}
return 0;
}

图论--差分约束--POJ 3169 Layout(超级源汇建图)的更多相关文章

  1. 图论--差分约束--POJ 3159 Candies

    Language:Default Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 43021   Accep ...

  2. 图论--差分约束--POJ 1364 King

    Description Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen p ...

  3. 网络流--最大流--POJ 2139(超级源汇+拆点建图+二分+Floyd)

    Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the ...

  4. 差分约束系统 POJ 3169 Layout

    题目传送门 题意:有两种关系,n牛按照序号排列,A1到B1的距离不超过C1, A2到B2的距离不小于C2,问1到n的距离最大是多少.如果无限的话是-2, 如果无解是-1 分析:第一种可以写这样的方程: ...

  5. 图论--差分约束--POJ 2983--Is the Information Reliable?

    Description The galaxy war between the Empire Draco and the Commonwealth of Zibu broke out 3 years a ...

  6. 图论--差分约束--POJ 1201 Intervals

    Intervals Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 30971 Accepted: 11990 Descripti ...

  7. 图论--网络流--最大流--POJ 3281 Dining (超级源汇+限流建图+拆点建图)

    Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, an ...

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

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

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

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

随机推荐

  1. 分享layui的table的一些小技巧,前端分页

    最近一直在折腾报表,期间使用了layui的table做展示(版本号:2.5.5) 起初:以为是查询结果出来后,前端和服务端分页一弄就完事了,参考例子,但是sql写得太长太长了,翻页困难,数据库是老旧的 ...

  2. Mysql 临时表+视图

    原文地址:http://www.cnblogs.com/mrdz/p/6195878.html 学习内容: 临时表和视图的基本操作... 临时表与视图的使用范围... 1.临时表   临时表:临时表, ...

  3. 最全的中文NLP资源库,你确定不来看一下吗?

    最全的中文NLP资源库,你确定不来看一下吗? 22/100 发布文章 qq_39248703 hello,小伙伴们大家好,今天给大家分享NLP资源库,可以说是最全的资源库了,很多包非常有趣,值得收藏, ...

  4. GitHub+PicGo构建免费图床及其高效使用

    搭建免费图床全过程! 一.搭建缘由 一开始搭建博客,避免不了要用许多图片,最初使用七牛云来做博客图床,但是后来发现,七牛云只有30天的临时域名,hhhhhhh,果然啊,天下就没有免费的好事啊~后来就发 ...

  5. curl 交叉编译 支持http2和openssl

    touch run.sh chmod 755 run.sh mkdir build cd build ../run.sh run.sh #!/bin/bash #cd /build ../config ...

  6. ConcurrentHashMap 同步安全 的真正含义(stringbuff 是同步安全的,stringbutter 不安全)

    同步安全的集合,在多线程下用到这个map是安全的,但这个安全指的是什么?线程安全指的是指get.remove.put等操作时即同一对象,同一时间只有一个线程能在这几个方法上运行,也就是说线程安全是在这 ...

  7. 【python实现卷积神经网络】池化层实现

    代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...

  8. echarts多个数据添加多个纵坐标

    在我们echarts开发中,肯定会遇到一个问题.那就是当有多个数据且数据大小差距太大时,就会出现有些数据小到看不到的情况.所以在遇到这种情况时,我通常的解决办法就是给他多加一个坐标轴. option  ...

  9. L24 word2vec

    词嵌入基础 我们在"循环神经网络的从零开始实现"一节中使用 one-hot 向量表示单词,虽然它们构造起来很容易,但通常并不是一个好选择.一个主要的原因是,one-hot 词向量无 ...

  10. stand up meeting 1/11/2016

    part 组员                工作              工作耗时/h 明日计划 工作耗时/h    UI 冯晓云 跑通打印机功能,尝试与pdf读取部分结合;生词本卡片选择简略释义 ...