POJ 3169 Layout 差分约束系统
介绍下差分约束系统:就是多个2未知数不等式形如(a-b<=k)的形式
问你有没有解,或者求两个未知数的最大差或者最小差
转化为最短路(或最长路)
1:求最小差的时候,不等式转化为b-a>=k的标准形式建图,求最长路
2:求最大差的时候,不等式转化为b-a<=k的标准形式建图,求最短路
然后具体的写的好的博客以供大家参考
1 http://www.cnblogs.com/void/archive/2011/08/26/2153928.html
2 http://blog.csdn.net/zhang20072844/article/details/7788672
3 http://www.cnblogs.com/pony1993/archive/2012/09/01/2666996.html
注:由于spfa比较慢,所以全是正权的时候用dij,负权用spfa,写spfa的时候,有时候用堆栈写起来更快(我也不知道为啥,当然手写的更快)
代码:
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
typedef long long LL;
const int N=1e3+;
const int INF=0x3f3f3f3f;
struct Edge{
int v,w,next;
}edge[];
bool inq[N];
int tot,n,ml,md,head[N],d[N],cnt[N];
void add(int u,int v,int w){
edge[tot].v=v;
edge[tot].w=w;
edge[tot].next=head[u];
head[u]=tot++;
}
queue<int>q;
bool spfa(int s){
for(int i=;i<=n;++i)
d[i]=INF,cnt[i]=,inq[i]=;
while(!q.empty())q.pop();
d[s]=;
q.push(s),cnt[s]=,inq[s]=true;
while(!q.empty()){
int u=q.front();
q.pop();
inq[u]=false;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].v;
if(d[v]>d[u]+edge[i].w){
d[v]=d[u]+edge[i].w;
if(!inq[v]){
q.push(v);
inq[v]=;
++cnt[v];
if(cnt[v]>n)return false;
}
}
}
}
return true;
}
int main(){
scanf("%d%d%d",&n,&ml,&md);
memset(head,-,sizeof(head));
for(int i=;i<=ml;++i){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
for(int i=;i<n;++i)add(i+,i,);
for(int i=;i<=md;++i){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(v,u,-w);
}
if(!spfa())printf("-1\n");
else{
if(d[n]==INF)printf("-2\n");
else printf("%d\n",d[n]);
}
return ;
}
POJ 3169 Layout 差分约束系统的更多相关文章
- POJ 3169 Layout(差分约束啊)
题目链接:http://poj.org/problem? id=3169 Description Like everyone else, cows like to stand close to the ...
- POJ 3169 Layout(差分约束+链式前向星+SPFA)
描述 Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 ...
- poj 3169 Layout 差分约束模板题
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6415 Accepted: 3098 Descriptio ...
- POJ 3169 Layout (差分约束)
题意:给定一些母牛,要求一个排列,有的母牛距离不能超过w,有的距离不能小于w,问你第一个和第n个最远距离是多少. 析:以前只是听说过个算法,从来没用过,差分约束. 对于第 i 个母牛和第 i+1 个, ...
- PKU 3169 Layout(差分约束系统+Bellman Ford)
题目大意:原题链接 当排队等候喂食时,奶牛喜欢和它们的朋友站得靠近些.FJ有N(2<=N<=1000)头奶牛,编号从1到N,沿一条直线站着等候喂食.奶牛排在队伍中的顺序和它们的编号是相同的 ...
- POJ 3169 Layout(差分约束 线性差分约束)
题意: 有N头牛, 有以下关系: (1)A牛与B牛相距不能大于k (2)A牛与B牛相距不能小于k (3)第i+1头牛必须在第i头牛前面 给出若干对关系(1),(2) 求出第N头牛与第一头牛的最长可能距 ...
- POJ 3169 Layout (差分约束系统)
Layout 题目链接: Rhttp://acm.hust.edu.cn/vjudge/contest/122685#problem/S Description Like everyone else, ...
- POJ 3169 Layout (spfa+差分约束)
题目链接:http://poj.org/problem?id=3169 差分约束的解释:http://www.cnblogs.com/void/archive/2011/08/26/2153928.h ...
- poj 3169 Layout(差分约束)
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6549 Accepted: 3168 Descriptio ...
随机推荐
- 实现一个div在浏览器水平居中
第一种方法: div { margin: 0 auto; width: 960px; } 第二种方法(兼容IE): body { text-align: center; } div { margin: ...
- 一步步学习NHibernate(8)——HQL查询(2)
请注明转载地址:http://www.cnblogs.com/arhat 在上一章中,老魏带着大家学习了HQL语句,发现HQL语句还是非常不错的,尤其是在懒加载的时候,书写起来比较的舒服,但是这里老魏 ...
- <三> SQL 基础
SQL查询的一般形式,以及被逻辑处理的顺序 (8) select (9) distinct (11) <TOP_specification> <select_list> (1) ...
- 转:Java Annotation详解
转载自:http://william750214.javaeye.com/blog/298104 元数据的作用 如果要对于元数据的作用进行分类,目前还没有明确的定义,不过我们可以根据它所起的作用,大致 ...
- C#如何控制方法的执行时间,超时则强制退出方法执行
转自:http://outofmemory.cn/code-snippet/1762/C-how-control-method-zhixingshijian-chaoshi-ze-force-quit ...
- jquery and event
jquery阻止事件冒泡 event.stopPropagation(); event.cancelBubble = true; jquery阻止默认操作 event.preventDefault() ...
- 在Unity中高效工作(下)
原地址:http://www.unity蛮牛.com/thread-20005-1-1.html Tips for Creating Better Games and Working More Eff ...
- FZU 2150 Fire Game(BFS)
点我看题目 题意 :就是有两个熊孩子要把一个正方形上的草都给烧掉,他俩同时放火烧,烧第一块的时候是不花时间的,每一块着火的都可以在下一秒烧向上下左右四块#代表草地,.代表着不能烧的.问你最少花多少时间 ...
- 练习-libev和pyev示例
事件循环,IO复用,还是理解深刻一点好. 比较LIBEV和PYEV,发现PYTHON库只是对LIBEV作了简单的语法转换. 到了这个层次,就一个字:DIAO!!! libev的C版: #include ...
- 练习--LINUX进程间通信之消息队列MSG
https://www.ibm.com/developerworks/cn/linux/l-ipc/part3/ 继续坚持,或许不能深刻理解,但至少要保证有印象. ~~~~~~~~~~~~~~ 消息队 ...