题意:

有N头牛, 有以下关系:

(1)A牛与B牛相距不能大于k

(2)A牛与B牛相距不能小于k

(3)第i+1头牛必须在第i头牛前面

给出若干对关系(1),(2)

求出第N头牛与第一头牛的最长可能距离, 若无解输出-1, 若无限长输出-2

分析:

3个关系对应的 <= 式子是:

dis[b] - dis[a] <= d(1)

dis[a] - dis[b] <= -d(2)

dis[i] - dis[i+1] <= -1(2)

目标式:dis[N] - dis[1] <= T

要求这个T就是建好图后跑源点为1的最短路, 有负权环路则无解输出-1, 不连通则输出-2(无约束关系)。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstring>
#include <cmath>
#include <iomanip>
#define rep(i,a,b) for(int i = a; i < b;i++)
#define _rep(i,a,b) for(int i = a; i <= b;i++)
using namespace std;
const int inf = 1e9 + ;
const int maxn = + ;
int n , m;
struct edge{
int to , d;
edge(int _to, int _d): to(_to), d(_d){}
};
vector<edge> G[maxn];
void add_edge(int u, int v, int d){
G[u].push_back(edge(v,d));
}
int N , ML, MD;
int dis[maxn], enter_cnt[maxn];
int spfa(){
fill(dis, dis+maxn, inf);
memset(enter_cnt, , sizeof(enter_cnt));
bool vis[maxn];
memset(vis, , sizeof(vis)); queue<int> q;
vis[] = ;
dis[] = ;
q.push();
++enter_cnt[]; while(!q.empty()){
int u = q.front();
rep(i,,G[u].size()){
int v = G[u][i].to, d = G[u][i].d;
if(dis[v] > dis[u] + d){
dis[v] = dis[u] + d;
if(!vis[v]){
if(++enter_cnt[v] >= N) return -;//入队次数大于等于N代表存在负权环路
vis[v] = ;
q.push(v);
}
}
}
vis[u] = ;
q.pop();
}
return dis[N];
}
int main(){
cin >> N >> ML >> MD;
rep(i,,ML){
int a, b, d;
cin >> a >> b >> d; //dis[b] - dis[a] <= d
add_edge(a,b,d);
}
rep(i,,MD){
int a, b, d;
cin >> a >> b >> d; // dis[b] - dis[a] >= d -> dis[a] - dis[b] <= -d
add_edge(b,a,-d);
}
rep(i,,N){ //dis[i+1] - dis[i] >= 1 -> dis[i] - dis[i+1] <= -1
add_edge(i+,i,-);
}
int ans = spfa();
if(ans == -){
printf("-1\n");
}else if(ans == inf){
printf("-2\n");
}else printf("%d\n", ans);
return ;
}

POJ 3169 Layout(差分约束 线性差分约束)的更多相关文章

  1. poj 3169 Layout (差分约束)

    3169 -- Layout 继续差分约束. 这题要判起点终点是否连通,并且要判负环,所以要用到spfa. 对于ML的边,要求两者之间距离要小于给定值,于是构建(a)->(b)=c的边.同理,对 ...

  2. poj 3169 Layout(线性差分约束,spfa:跑最短路+判断负环)

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15349   Accepted: 7379 Descripti ...

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

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

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

    题目链接:http://poj.org/problem?id=3169 题目大意:n头牛,按编号1~n从左往右排列,可以多头牛站在同一个点,给出ml行条件,每行三个数a b c表示dis[b]-dis ...

  5. POJ 3169 Layout (HDU 3592) 差分约束

    http://poj.org/problem?id=3169 http://acm.hdu.edu.cn/showproblem.php?pid=3592 题目大意: 一些母牛按序号排成一条直线.有两 ...

  6. poj 3169 Layout(差分约束+spfa)

    题目链接:http://poj.org/problem?id=3169 题意:n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0.这些牛的距离存在着一些约束关系:1.有m ...

  7. POJ 3169 Layout (图论-差分约束)

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6574   Accepted: 3177 Descriptio ...

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

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

  9. 图论--差分约束--POJ 3169 Layout(超级源汇建图)

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

随机推荐

  1. 找不到javax.servlet.Filter的类文件

    在这里我是用IDEA来开发的,Tomcat用的maven插件 原因:没有相应在jar包 解决:导入相应在jar的依赖,在pom文件中添加 <dependency> <groupId& ...

  2. JSP | 基础 | 两种方式循环输出

    用循环连续输出三个你好,字体从小变大 第一种: <body> <%! //通过表达式方式调用实现 String HelloJSP1(){ String str = "&qu ...

  3. Codeforces Round #323 (Div. 2)

    被进爷坑了,第二天的比赛改到了12点 水 A - Asphalting Roads /************************************************ * Author ...

  4. 7.2 Collection和Iterator接口

  5. Python: How to iterate list in reverse order

    #1 for index, val in enumerate(reversed(list)): print len(list) - index - 1, val #2 def reverse_enum ...

  6. Myisamchk使用

    Myisam损坏的情况: . 服务器突然断电导致数据文件损坏;强制关机,没有先关闭mysql 服务;mysqld 进程在写表时被杀掉.因为此时mysql可能正在刷新索引. . 磁盘损坏. . 服务器死 ...

  7. Varnish快速安装及测试

    实验环境: slave-147:   192.168.75.147 slave-148:    192.168.75.148 两台机器均已关闭selinux,关闭iptables. varnish部署 ...

  8. Suricata的初始化脚本

    见官网 https://suricata.readthedocs.io/en/latest/initscripts.html

  9. 单页Html及Android App供小孩学习常用汉字

    为了检验及帮助小孩学习常用汉字,简单开发本网页应用: 常用汉字是按使用频率排序的,来源于网上: 该简单应用 有Android APP下载 “学习常用汉字_20150910.apk” 单页Html 示例 ...

  10. 关于HashMap中hash()函数的思考

    关于HashMap中hash()函数的思考 JDK7中hash函数的实现   static int hash(int h) { h ^= (h >>> 20) ^ (h >&g ...