介绍下差分约束系统:就是多个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 差分约束系统的更多相关文章

  1. POJ 3169 Layout(差分约束啊)

    题目链接:http://poj.org/problem? id=3169 Description Like everyone else, cows like to stand close to the ...

  2. POJ 3169 Layout(差分约束+链式前向星+SPFA)

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

  3. poj 3169 Layout 差分约束模板题

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6415   Accepted: 3098 Descriptio ...

  4. POJ 3169 Layout (差分约束)

    题意:给定一些母牛,要求一个排列,有的母牛距离不能超过w,有的距离不能小于w,问你第一个和第n个最远距离是多少. 析:以前只是听说过个算法,从来没用过,差分约束. 对于第 i 个母牛和第 i+1 个, ...

  5. PKU 3169 Layout(差分约束系统+Bellman Ford)

    题目大意:原题链接 当排队等候喂食时,奶牛喜欢和它们的朋友站得靠近些.FJ有N(2<=N<=1000)头奶牛,编号从1到N,沿一条直线站着等候喂食.奶牛排在队伍中的顺序和它们的编号是相同的 ...

  6. POJ 3169 Layout(差分约束 线性差分约束)

    题意: 有N头牛, 有以下关系: (1)A牛与B牛相距不能大于k (2)A牛与B牛相距不能小于k (3)第i+1头牛必须在第i头牛前面 给出若干对关系(1),(2) 求出第N头牛与第一头牛的最长可能距 ...

  7. POJ 3169 Layout (差分约束系统)

    Layout 题目链接: Rhttp://acm.hust.edu.cn/vjudge/contest/122685#problem/S Description Like everyone else, ...

  8. POJ 3169 Layout (spfa+差分约束)

    题目链接:http://poj.org/problem?id=3169 差分约束的解释:http://www.cnblogs.com/void/archive/2011/08/26/2153928.h ...

  9. poj 3169 Layout(差分约束)

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

随机推荐

  1. prefix和unprefix

    exports._esModule = true; exports.default = { prefix: function prefix(prop){ if (prop[0] === '-'){ v ...

  2. windows 7 64bit 下apache php mysql 环境配置

    在64位环境下安装apache,php和配置过程 准备好安装包.(64位版本) Apache 下载地址:http://www.apachelounge.com/download/ Php 下载地址:h ...

  3. MySQL数据库远程访问的权限

    GRANT ALL PRIVILEGES ON *.* TO 'user'@'%'IDENTIFIED BY 'passwd' WITH GRANT OPTION;

  4. IdTCPServer, idTCPClient

    IdTcpServer uses IdContext //需要引用 属性,方法: IdTCPServer.Active :=True; //开启服务器 IdTCPServer1.Bindings.Ad ...

  5. ubuntu 12 64 桌面版Oracle11g 安装

    1.Creating the Oracle Inventory Group sudo groupadd oinstall sudo groupadd dba sudo groupadd oper su ...

  6. poj 1818 ATP

    ATP 题意:足球锦标赛使用二分的策略,每次淘汰剩下人的一半,并且数据表明:排名相差k(include)之内的运动员,胜负难料,否则排名前的必定战胜排名后的:问给定n(n = 2x, x∈N, n & ...

  7. 记一个菜鸟在Linux上部署Tomcat的随笔

    以前都只是在园子里找各种资料.文档.各种抱大腿,今天是第一次进园子里来添砖加瓦,实话说,都不知道整些啥东西上来,就把自己在Linux上搭建Tomcat的过程记录下来,人笨,请各位大虾们勿喷. 虽然做开 ...

  8. 【转】spring3 MVC实战,手工搭建Spring3项目demo

    更新:这几天对spring3的理解又进了一步,今天抽空把这篇文章中的错误和不当之处做了修改. 最近的项目在用Spring3,涉及到了基于注解的MVC,事务管理,与hibernate的整合开发等内容,我 ...

  9. Ubuntu使用apt-get安装本地deb包

    我们都喜欢使用apt-get,因为它实在是让我们大大的省心.但是,有时候我们会为网速慢,安装源不好而烦恼,所以我们可能会将一些常用软件包的deb文件保存在本地以备不时之需.当然了使用dpkg也可以直接 ...

  10. Python中通过Image的open之后,去show结果打不开bmp图片,无法正常显示图片

    在windows的cmd命令行下,使用Python的PIL库打开并显示一个jpg图片: ? 1 2 3 openedImg = Image.open(saveToFile); print " ...