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 ...
随机推荐
- 云盾正常扫描云服务器的IP是什么
问题:云盾正常扫描云服务器的IP是什么? 解答:云盾扫描云服务器的的IP段固定为 42.120.145.0/24 110.75.105.0/24 110.75.185.0/24 110.75 ...
- HaProxy+keepalived实现负载均衡
HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持 ...
- 用php生成word文档
一.用windows里面自带的com,然后用php生成word文档 <?php $word= new COM("word.application") or die(" ...
- PHP学习心得(一)——简介
PHP(“PHP: Hypertext Preprocessor”,超文本预处理器的字母缩写)是一种被广泛应用的开放源代码的多用途脚本语言,它可嵌入到 HTML中,尤其适合 web 开发. PHP 脚 ...
- 2016032101 - eclipse3.7+jdk1.6+maven3.0.5
公司使用jdk1.6做开发环境,那么使用的eclipse需要下载3.7版本,因为eclipse4以上必须使用jdk1.7及其以上版本. 1.资源下载 jdk1.6需要去oracle官网去下载,可能需要 ...
- js: get event handler bound to the element
jQuery._data(jQuery(this)[0], "events" ).click[0].handler $._data( $("#myabc")[0 ...
- csuoj 1351: Tree Counting
这是一个动态规划的题: 当初想到要用dp,但是一直想不到状态转移的方程: 题解上的原话: 动态规划,设 g[i]表示总结点数为 i 的方案种数,另设 f[i][j]表示各个孩子的总结点数为i,孩子的个 ...
- android ListView内数据的动态添加与删除
main.xml 文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns ...
- Java Development Kit (JDK) 发展历程 及新特性
SE(J2SE),standard edition,标准版,是我们通常用的一个版本,从JDK 5.0开始,改名为Java SE. EE(J2EE),enterprise edition,企业版,使用这 ...
- linux 性能分析常规逻辑和手段总结
一. 追查cpu占用较高的进程(线程) 1 . 如何查找出当前系统中占用cpu或者内存最高的进程? ps aux |sort -rn -k 3 |head -n3 查找出当前系统中cpu资源占用前三 ...