题意:

有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. iOS 优雅地隐藏导航栏NavigationBar (Objc)

    @interface FSViewController () <UINavigationControllerDelegate> @end @implementation FSViewCon ...

  2. SPFA/Dijkstra POJ 3159 Candies

    题目传送门 题意:n个人发糖果,B 比 A 多 C的糖果,问最后第n个人比第一个人多多少的糖果 分析:最短路,Dijkstra 优先队列优化可过,SPFA竟然要用栈,队列超时! 代码: /****** ...

  3. Hu矩

    close all; clear all; I1=imread('lena.bmp'); angle=; T=[cos(angle),sin(angle),;-sin(angle),cos(angle ...

  4. EJB Timer Service is not available. Timers for application with id 95795415990861824 will not be deleted

    delete follows:glassfish\domains\domain1\applications\ejb-timer-service-appglassfish\domains\domain1 ...

  5. imagettftext

    ImageTTFText 写 TTF 文字到图中. 语法: array ImageTTFText(int im, int size, int angle, int x, int y, int col, ...

  6. 使用callabestatement接口调用存储过程

  7. Squid启动报:Could not determine this machines public hostname. Please configure one or set 'visible_hostname'.

    在squid.conf中添加 visible_hostname squid.packet-pushers.net 或者编辑/etc/hosts文件, 在该文件中制定主机IP地址与主机名的对应.

  8. ASP.NET MVC Identity 兩個多個連接字符串問題解決一例

    按照ASP.NET MVC Identity建立了一個用戶權限管理模塊,由于還要加自己已有的數據庫,所以建立了一個實體模型,建立了之后,發現登錄不了: 一直顯示“Login in failed for ...

  9. springboot项目启动问题

    在调试项目的时候有遇到这样一个问题: 项目启动后访问不通,编译没有任何问题,启动也没有报错,日志在打,但是访问不通.而且之前一直可以正常访问,在没改任何代码的情况下不能访问了. 尝试很多次偶然发现,点 ...

  10. 命令模式和php实现

    命令模式: 命令模式(Command Pattern):将一个请求封装为一个对象,从而使我们可用不同的请求对客户进行参数化:对请求排队或者记录请求日志,以及支持可撤销的操作.命令模式是一种对象行为型模 ...