POJ 3169 Layout(差分约束 线性差分约束)
题意:
有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(差分约束 线性差分约束)的更多相关文章
- poj 3169 Layout (差分约束)
3169 -- Layout 继续差分约束. 这题要判起点终点是否连通,并且要判负环,所以要用到spfa. 对于ML的边,要求两者之间距离要小于给定值,于是构建(a)->(b)=c的边.同理,对 ...
- poj 3169 Layout(线性差分约束,spfa:跑最短路+判断负环)
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15349 Accepted: 7379 Descripti ...
- POJ 3169 Layout (spfa+差分约束)
题目链接:http://poj.org/problem?id=3169 差分约束的解释:http://www.cnblogs.com/void/archive/2011/08/26/2153928.h ...
- POJ 3169 Layout (spfa+差分约束)
题目链接:http://poj.org/problem?id=3169 题目大意:n头牛,按编号1~n从左往右排列,可以多头牛站在同一个点,给出ml行条件,每行三个数a b c表示dis[b]-dis ...
- POJ 3169 Layout (HDU 3592) 差分约束
http://poj.org/problem?id=3169 http://acm.hdu.edu.cn/showproblem.php?pid=3592 题目大意: 一些母牛按序号排成一条直线.有两 ...
- poj 3169 Layout(差分约束+spfa)
题目链接:http://poj.org/problem?id=3169 题意:n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0.这些牛的距离存在着一些约束关系:1.有m ...
- POJ 3169 Layout (图论-差分约束)
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6574 Accepted: 3177 Descriptio ...
- 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(超级源汇建图)
Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 < ...
随机推荐
- excel导入phpmyadmin
1.将excel文件另存为txt文件,再将txt文件保存为.csv文件同时修改编码为UTF8 2.登录phpmyadmin,在phpmyadmin中创建好表格,按excel中的顺序创建每列 3.因为p ...
- 基于node 搭建http2服务
1.准备工作:安装node2.安装http2: npm install http2 -g安装完成后,在安装目录中appData/Roaming>npm>node_modules>ht ...
- 当css样式表遇到层
(附:White-space:pre可以是样式表里卖弄body的属性,作用是保持html源代码的空格与换行,等同<pre>标签.) Css样式表可以通过被封在层里的方式来限制页面所修饰的内 ...
- Keepalived+LVS(DR)+MySQL
实验环境 主机名 IP VIP 服务 主备 KA_LV_MYSQL_01 192.168.30.130 192.168.30.100 keepalived.LVS.MySQL MASTER KA_LV ...
- 【前端】html5获取经纬度,百度api获取街区名,并使用JS保存进cookie
引用js<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak= ...
- 用私有构造器或者枚举类型强化Singleton
参考Effective Java第三版 Joshua J. Bloch 参与编写JDK的大佬,上次看Collections的源码时看见了他的名字,然后翻了翻书,竟然就是他写的! 1.常见的一种: pu ...
- 在docker容器中运行hello world!
在docker容器中运行hello world! docker容器可以理解为在沙盒中运行的进程.这个沙盒包含了该进程运行所必须的资源,包括文件系统.系统类库.shell 环境等等.但这个沙盒默认是不会 ...
- ubuntu下php-fpm多实例运行配置
php-fpm服务一般情况下我们只会配置一个php-fpm了,如果我们碰到要实现多实例php-fpm服务要如何来配置呢,下面一起来看看吧. 这里是在LNMP环境的基础上配置多实例的过程.因为我在使用的 ...
- SEO 第九章
SEO第九章 本次课目标: 1. 外部优化之平台优化 2. 如何撰写SEO诊断方案 一.外部平台优化——百度系列平台 百度系列的平台都是属于百度自己的产品,排名都是比较高的,所以我们在做外部推广的 ...
- exportfs - 管理NFS共享文件系统列表
概述 (SYNOPSIS) /usr/sbin/exportfs [-avi] [-o options,..] [client:/path ..] /usr/sbin/exportfs -r [-v] ...