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 < ...
随机推荐
- iOS UITableView ExpandableHeader(可形变的Header)
最常见的header就是在tableView下拉时header里的图片会放大的那种, 最近研究了一下,自己实现了这种header. 1.设置TableView的contentInset(为header ...
- oracle 表空间及查看所有用户的表空间
用户有默认表空间,但是只能指定一个,但是你有其它表空间的限额的话,可以将表建到其它表空间中. 语法 create table xxx(xxxx xx) tablespace xxxxx 1.查看当前用 ...
- Selenium | 基础入门 | 利用Xpath寻找用户框
以微信公众号登陆界面为例, 找到相对应的Xpath的方法, 核心代码: System.setProperty(“webdriver.chrome.driver”,“ C:\\Program Files ...
- HDU2586(tarjanLCA板子)
; int T, n, m; int f[maxn], vis[maxn], dis[maxn], ans[maxn]; vector<P> vc[maxn]; vector<int ...
- The Specials Menu LightOJ - 1025
The Specials Menu LightOJ - 1025 题意:在给定的字符串中删去一些字符,使其成为回文串(不能全部都删).求方案数. 方法:常规的区间dp.ans[i][j]表示在i到j的 ...
- 523 Continuous Subarray Sum 非负数组中找到和为K的倍数的连续子数组
非负数组中找到和为K的倍数的连续子数组 详见:https://leetcode.com/problems/continuous-subarray-sum/description/ Java实现: 方法 ...
- [已读]响应式web设计
去年冲着响应式这三个字买的,很快就读完了,因为说实话都挺浅显的内容.真正涉及到响应式的是第二和第三章(媒体查询 em 百分比图片),其他的h5与css3关系不大.
- Git之提交项目到远程github
1.在分支dev下,默认本地工作区有项目project 2. git add project [添加项目到暂存区] 3. git commit project -m "提交项目" ...
- 【学习笔记】彻底理解JS中的this
首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上this的最终指向的是那个调用它的对象(这句话有些问题,后面会解释为什么会有问题,虽然 ...
- vue中引入字体图标报错,找不到字体文件
在用vue + webpack进行开发的时候,在引用字体图标遇到字体无法加载的问题: 报以下错误 搞了好久没搞定,最后才找到解决方法(还是没有找到原因) 修改字体图标的css中引入字体文件的路径 以前 ...