3169 -- Layout

  继续差分约束。

  这题要判起点终点是否连通,并且要判负环,所以要用到spfa。

  对于ML的边,要求两者之间距离要小于给定值,于是构建(a)->(b)=c的边。同理,对于MD的,构建(b)->(a)=-c的边。然后就是(i+1)->(i)=0,两者距离大于0的限制。

代码如下:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue> using namespace std; const int N = ;
const int M = ;
struct Edge {
int t, nx, c;
Edge() {}
Edge(int t, int nx, int c) : t(t), nx(nx), c(c) {}
} edge[M];
int eh[N], ec; void init() {
memset(eh, -, sizeof(eh));
ec = ;
} void addedge(int s, int t, int c) {
edge[ec] = Edge(t, eh[s], c);
eh[s] = ec++;
} int cnt[N], dis[N], n;
bool inq[N];
queue<int> q;
const int INF = 0x5fffffff; int spfa(int s, int t) {
while (!q.empty()) q.pop();
for (int i = ; i <= n; i++) inq[i] = false, dis[i] = INF, cnt[i] = n + ;
q.push(s);
dis[s] = ;
inq[s] = true;
cnt[s]--;
while (!q.empty()) {
int cur = q.front();
// cout << cur << ' ' << eh[cur] << endl;
q.pop();
inq[cur] = false;
for (int i = eh[cur]; ~i; i = edge[i].nx) {
Edge &e = edge[i];
//cout << dis[e.t] << ' ' << dis[cur] + e.c << endl;
if (dis[e.t] > dis[cur] + e.c) {
dis[e.t] = dis[cur] + e.c;
if (cnt[e.t]) cnt[e.t]--;
else return -;
if (!inq[e.t]) {
q.push(e.t);
inq[e.t] = true;
}
}
}
}
if (dis[t] == INF) return -;
else return dis[t];
} int main() {
int ml, md;
int x, y, c;
while (~scanf("%d%d%d", &n, &ml, &md)) {
init();
for (int i = ; i < ml; i++) {
scanf("%d%d%d", &x, &y, &c);
addedge(x, y, c);
}
for (int i = ; i < md; i++) {
scanf("%d%d%d", &x, &y, &c);
addedge(y, x, -c);
}
for (int i = ; i < n; i++) {
//cout << i + 1 << ' ' << i << endl;
addedge(i + , i, );
}
printf("%d\n", spfa(, n));
}
return ;
}

——written by Lyon

poj 3169 Layout (差分约束)的更多相关文章

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

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

  2. POJ 3169 Layout (差分约束)

    题意:给定一些母牛,要求一个排列,有的母牛距离不能超过w,有的距离不能小于w,问你第一个和第n个最远距离是多少. 析:以前只是听说过个算法,从来没用过,差分约束. 对于第 i 个母牛和第 i+1 个, ...

  3. POJ 3169 Layout(差分约束啊)

    题目链接:http://poj.org/problem? id=3169 Description Like everyone else, cows like to stand close to the ...

  4. poj 3169 Layout 差分约束模板题

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6415   Accepted: 3098 Descriptio ...

  5. POJ 3169 Layout(差分约束 线性差分约束)

    题意: 有N头牛, 有以下关系: (1)A牛与B牛相距不能大于k (2)A牛与B牛相距不能小于k (3)第i+1头牛必须在第i头牛前面 给出若干对关系(1),(2) 求出第N头牛与第一头牛的最长可能距 ...

  6. ShortestPath:Layout(POJ 3169)(差分约束的应用)

                布局 题目大意:有N头牛,编号1-N,按编号排成一排准备吃东西,有些牛的关系比较好,所以希望他们不超过一定的距离,也有一些牛的关系很不好,所以希望彼此之间要满足某个关系,牛可以 ...

  7. poj 3169&hdu3592(差分约束)

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9687   Accepted: 4647 Descriptio ...

  8. Bellman-Ford算法:POJ No.3169 Layout 差分约束

    #define _CRT_SECURE_NO_WARNINGS /* 4 2 1 1 3 10 2 4 20 2 3 3 */ #include <iostream> #include & ...

  9. POJ 3169 Layout 差分约束系统

    介绍下差分约束系统:就是多个2未知数不等式形如(a-b<=k)的形式 问你有没有解,或者求两个未知数的最大差或者最小差 转化为最短路(或最长路) 1:求最小差的时候,不等式转化为b-a>= ...

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

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

随机推荐

  1. 初识zookeeper以及安装和集群部署

    初识zookeeper以及安装和集群部署     一.Zookeeper单体版安装     在安装zookeeper之前要先安装jdk环境,具体在linux环境安装jdk1.8请参照linux笔记. ...

  2. mysql创建数据库指定utf8编码

    CREATE DATABASE IF NOT EXISTS dbname DEFAULT CHARSET utf8;

  3. 序列化form表单

    // 序列化JSON字符串 $.fn.serializeObject = function () { let o = {}; let a = this.serializeArray(); $.each ...

  4. 洛谷P1965 转圈游戏 [2013NOIP提高组 D1T1][2017年6月计划 数论04]

    P1965 转圈游戏 题目描述 n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n-1.最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 ...

  5. 洛谷P1508 Likecloud-吃、吃、吃 [2017年4月计划 动态规划10]

    P1508 Likecloud-吃.吃.吃 题目背景 问世间,青春期为何物? 答曰:“甲亢,甲亢,再甲亢:挨饿,挨饿,再挨饿!” 题目描述 正处在某一特定时期之中的李大水牛由于消化系统比较发达,最近一 ...

  6. JavaScript 中的多线程通信的方法

    在Html 5诞生之后,我们可以使用javascript来实现多线程处理.H5 新增了一个web workers api,使用这个API,用户可以很容易地创建在后台运行的线程,H5 中被称为workd ...

  7. vue自定义指令之拖动页面的元素

    此案例中,用到了鼠标事件onmousedown.onmousemove.onmouseup 源代码如下: <!doctype html><html lang="en&quo ...

  8. webpack学习之—— 模块热替换(Hot Module Replacement)

    模块热替换(HMR - Hot Module Replacement)功能会在应用程序运行过程中替换.添加或删除模块,而无需重新加载整个页面.主要是通过以下几种方式,来显著加快开发速度: 保留在完全重 ...

  9. Ubuntu 安装 setuptools

    Setuptools的官方页面 Easily download, build, install, upgrade, and uninstall Python packages 它是一个对python的 ...

  10. 层次分析法MATLAB

    输入成对比较矩阵,输出权重值和一致性检验结果. disp('请输入判断矩阵A(n阶)'); A=input('A='); [n,n]=size(A); x=ones(n,100); y=ones(n, ...