题意:有一串数字1~n,按顺序排序,给两种要求,一是给定u,v保证pos[v] - pos[u] <= w;二是给定u,v保证pos[v] - pos[u] >= w。求pos[n] - pos[1]最大,若无解输出-1,无穷多解输出-2。

思路:光看题目好像和最短路无关,其实这里用到了spfa的松弛操作来保证所给出的两种要求。若pos[v] - pos[u] >= w,则pos[v] +(- w) >=  pos[u],也就是pos[v] +(- w) < pos[u]时进行松弛,建一条边v->u,权值-w,这就和spfa中的那一步对应上了,于是转化为了最短路。另一种条件也是如此操作。无解的情况应为出现了负环;无穷多解的情况为1和n没有条件约束,也就是1没有路通向n。

参考:

夜深人静写算法(四) - 差分约束

代码:

#include<cstdio>
#include<set>
#include<cmath>
#include<stack>
#include<vector>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn = +;
const int INF = 0x3f3f3f3f;
struct Edge{
int v,cost;
Edge(int _v = ,int _cost = ):v(_v),cost(_cost){}
};
vector<Edge> G[maxn];
bool vis[maxn];
int cnt[maxn];
int dist[maxn];
void addEdge(int u,int v,int cost){
G[u].push_back(Edge(v,cost));
}
bool spfa(int st,int n){
memset(vis,false,sizeof(vis));
memset(dist,INF,sizeof(dist));
vis[st] = true;
dist[st] = ;
queue<int> q;
while(!q.empty()) q.pop();
q.push(st);
memset(cnt,,sizeof(cnt));
cnt[st] = ;
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int i = ;i < G[u].size();i++){
int v = G[u][i].v;
if(dist[v] > dist[u] + G[u][i].cost){
dist[v] = dist[u] + G[u][i].cost;
if(!vis[v]){
vis[v] = true;
q.push(v);
if(++cnt[v] > n) return false;
}
}
}
}
return true;
}
int main(){
int n,ml,md;
scanf("%d%d%d",&n,&ml,&md);
for(int i = ;i <= n;i++) G[i].clear();
for(int i = ;i <= ml;i++){ //at most -> v - u <= w -> v <= w + u
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addEdge(u,v,w);
}
for(int i = ;i <= md;i++){ //at least -> v - u >= w -> u <= -w + v
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addEdge(v,u,-w);
}
bool cannot = spfa(,n);
if(!cannot){
printf("-1\n");
}
else{
if(dist[n] == INF){
printf("-2\n");
}
else{
printf("%d\n",dist[n]);
}
}
return ;
}

POJ 3169 Layout(差分约束+最短路)题解的更多相关文章

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

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

  2. POJ 3169 Layout (差分约束)

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

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

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

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

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

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

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

  6. ShortestPath:Layout(POJ 3169)(差分约束的应用)

                布局 题目大意:有N头牛,编号1-N,按编号排成一排准备吃东西,有些牛的关系比较好,所以希望他们不超过一定的距离,也有一些牛的关系很不好,所以希望彼此之间要满足某个关系,牛可以 ...

  7. poj 3169&hdu3592(差分约束)

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9687   Accepted: 4647 Descriptio ...

  8. Bellman-Ford算法:POJ No.3169 Layout 差分约束

    #define _CRT_SECURE_NO_WARNINGS /* 4 2 1 1 3 10 2 4 20 2 3 3 */ #include <iostream> #include & ...

  9. POJ 3169 Layout 差分约束系统

    介绍下差分约束系统:就是多个2未知数不等式形如(a-b<=k)的形式 问你有没有解,或者求两个未知数的最大差或者最小差 转化为最短路(或最长路) 1:求最小差的时候,不等式转化为b-a>= ...

随机推荐

  1. 简明 Vim 教程

    学习 vim 并且其会成为你最后一个使用的文本编辑器.没有比这个更好的文本编辑器了,非常地难学,但是却不可思议地好用. 我建议下面这四个步骤: 存活 感觉良好 觉得更好,更强,更快 使用VIM的超能力 ...

  2. C# DateTime.Now详细用法

    //2008年4月24日 System.DateTime.Now.ToString("D"); //2008-4-24 System.DateTime.Now.ToString(& ...

  3. 【Android】eclipse打不开的解决办法和“Jar mismatch! Fix your dependencies”的解决

    JDK1.7能用,cmd下输入java,javac,java -version,javaw配置和环境都没问题的话,有可能是工作空间的问题,就是一般在D盘下的workspace..那个文件夹,删除了,再 ...

  4. struts2的占位符*在action中的配置方法

    转自:https://blog.csdn.net/u012546338/article/details/68946633 在配置<action> 时,可以在 name,class,meth ...

  5. KMP的next数组性质运用

    hdu2594 Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  6. 从零搭建 vue-cli 脚手架

    前言: 用了几次 vue-cli 做 vue 项目,感觉没什么大问题,虽然也没有用 vue-router 和 vuex .但是心里一直有个梗,就是最初的目录生成和配置文件,一直没动过,也不知道具体原理 ...

  7. yii2 的 Url::to() 和 Url::toRoute()

    关于Url类的操作在这个页面http://www.yiichina.com/doc/guide/2.0/helper-url: Url::to() 和 toRoute() 非常类似.这两个方法的唯一区 ...

  8. 02Del.ashx(删除班级)

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using WebHelper ...

  9. python3安装后无法使用退格键的问题

    # 安装readline模块 yum -y install readline-devel # 进入Python安装目录 cd /usr/python/Python- # 重新安装 ./configur ...

  10. Feed系统架构资料收集(转)

    add by zhj:有些链接已经失效,后续会修改. 原文:http://blog.csdn.net/zhangzhaokun/article/details/7834797 完全用nosql轻松打造 ...