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 < ...
 
随机推荐
- 51Nod 1174 区间中最大的数(RMQ)
			
#include <iostream> #include <algorithm> #include <cstring> using namespace std; + ...
 - Hexo - 修改永久链接的默认格式
			
Hexo的永久链接的默认格式是 :year/:month/:day/:title/,比如访问站点下某一篇文章时,其路径是 2018/04/12/xxxx/,如果我们的文章标题是中文的,那么该路径就会出 ...
 - Python入门小练习 002 批量下载网页链接中的图片
			
我们常常需要下载网页上很多喜欢的图片,但是面对几十甚至上百张的图片,一个一个去另存为肯定是个很差的体验. 我们可以用urllib包获取html的源码,再以正则表达式把匹配的图片链接放入一个list中, ...
 - python之文件的读写
			
读 r 读写模式 r+ 如果打开文件时没有指定模式,默认只读,如果使用r或r+,文件不存在时会报错 写 w 写读模式 w+ w模式会清空原有的文件内容 追加 a 追加读 ...
 - 洛谷 P3312 [SDOI2014]数表
			
式子化出来是$\sum_{T=1}^m{\lfloor}\frac{n}{T}{\rfloor}{\lfloor}\frac{m}{T}{\rfloor}\sum_{k|T}\mu(\frac{T}{ ...
 - linux下实现多台服务器同步文件(inotify-tools+rsync实时同步文件安装和配置)
			
inotify-tools+rsync实时同步文件安装和配置 注:转载https://www.linuxidc.com/Linux/2012-06/63624.htm
 - 145 Binary Tree Postorder Traversal 二叉树的后序遍历
			
给定一棵二叉树,返回其节点值的后序遍历.例如:给定二叉树 [1,null,2,3], 1 \ 2 / 3返回 [3,2,1].注意: 递归方法很简单,你可以使用迭代方法来解 ...
 - 解决spring boot websocket
			
在网上找的demo写了一个小例子,本地开发测试都很正常,但是部署在tomcat就各种坑 1.MyWebSocket不要用spring 注解标注 2.main方法对应的类继承SpringBootServ ...
 - 学习Python的day1
			
自己以前从来没有写博客的想法,但是学Python,里面的老师也说了,写博客可以加深自己的记忆,也能回顾内容.还能给别人参考.挺值的.2017-09-16 一. Python介绍 python的创始人为 ...
 - ES6学习笔记(9)----Symbol
			
参考书<ECMAScript 6入门>http://es6.ruanyifeng.com/ Symbol1.symbol:Symbol是javascript的第七种原始数据类型,代表独一无 ...