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. vue+vant ui+高德地图的选址组件

    首先在index.html引入高德地图的js <script src="https://webapi.amap.com/maps?v=1.4.14&key=你的key" ...

  2. iOS项目转移到自动引用计数

    这里主要参考了Apple官方文档:Transitioning to ARC Release Notes 在支持iOS5的Xcode4中,创建项目会看到这样的选项: 这是iOS5的新特性,自动对象引用计 ...

  3. 消息队列rabbitmq rabbitMQ安装

    消息队列rabbitmq   12.1 rabbitMQ 1. 你了解的消息队列 生活里的消息队列,如同邮局的邮箱, 如果没邮箱的话, 邮件必须找到邮件那个人,递给他,才玩完成,那这个任务会处理的很麻 ...

  4. C#中时间差的计算

    /// <summary> /// 已重载.计算两个日期的时间间隔,返回的是时间间隔的日期差的绝对值. /// </summary> /// <param name=&q ...

  5. Vue.之. 动态设置按钮Disabled

    Vue.之. 动态设置按钮Disabled 按钮代码如下: 添加了一个 属性      :disabled="isAble"  ,控制:更新按钮.重置按钮 <el-form- ...

  6. java 2类与对象[学堂在线]

    java的面向对象方法和特征(略) 累的声明格式 语法:先定义一个引用变量名 穿件对象 new aclock=new CLock() 没有ststaic 就是实例变量 类变量static 类变量 方法 ...

  7. text()和html()区别

    这两天看了一下html和jquery的选择器,并对w3chool上面的在线编辑产生了兴趣,但是在用textarea展示后台纯html的时候发生错误,查阅各种资料发现不行--心态炸了.废话不多说了,上干 ...

  8. Directx11教程(17) D3D11管线(6)

    原文:Directx11教程(17) D3D11管线(6)       VS shader输出clip空间的顶点位置及参数信息(比如颜色)到一个FIFO中,之后PA(primitive assembl ...

  9. day39-Spring 14-Spring的JDBC模板:DBCP连接池配置

    一般常用的连接池是DBCP和C3P0. package cn.itcast.spring3.demo1; import java.sql.DriverManager; import org.junit ...

  10. 原生js分页

    <html> <head> <meta charset='utf-8'> <style type="text/css"> #idDa ...