poj 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 (差分约束)的更多相关文章
- 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 (差分约束)
题意:给定一些母牛,要求一个排列,有的母牛距离不能超过w,有的距离不能小于w,问你第一个和第n个最远距离是多少. 析:以前只是听说过个算法,从来没用过,差分约束. 对于第 i 个母牛和第 i+1 个, ...
- POJ 3169 Layout(差分约束啊)
题目链接:http://poj.org/problem? id=3169 Description Like everyone else, cows like to stand close to the ...
- poj 3169 Layout 差分约束模板题
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6415 Accepted: 3098 Descriptio ...
- POJ 3169 Layout(差分约束 线性差分约束)
题意: 有N头牛, 有以下关系: (1)A牛与B牛相距不能大于k (2)A牛与B牛相距不能小于k (3)第i+1头牛必须在第i头牛前面 给出若干对关系(1),(2) 求出第N头牛与第一头牛的最长可能距 ...
- ShortestPath:Layout(POJ 3169)(差分约束的应用)
布局 题目大意:有N头牛,编号1-N,按编号排成一排准备吃东西,有些牛的关系比较好,所以希望他们不超过一定的距离,也有一些牛的关系很不好,所以希望彼此之间要满足某个关系,牛可以 ...
- poj 3169&hdu3592(差分约束)
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9687 Accepted: 4647 Descriptio ...
- 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 & ...
- POJ 3169 Layout 差分约束系统
介绍下差分约束系统:就是多个2未知数不等式形如(a-b<=k)的形式 问你有没有解,或者求两个未知数的最大差或者最小差 转化为最短路(或最长路) 1:求最小差的时候,不等式转化为b-a>= ...
- POJ 3169 Layout (spfa+差分约束)
题目链接:http://poj.org/problem?id=3169 差分约束的解释:http://www.cnblogs.com/void/archive/2011/08/26/2153928.h ...
随机推荐
- java list转换json格式
/** * 处理返回值(转换json格式和补零) * * @param resultDto5List * @param dateList * @return */ private JSONObject ...
- CSS利用filter/opacity实现背景透明
先看看众所周知的解决方案 如果我们想要一个半透明背景,有两种实现方式: 1.利用CSS和opacity属性 .opacity { filter:alpha(opacity=);/*IE浏览器*/ op ...
- Nginx负载均衡反向代理
http{ upstream test.com { server 118.118.66.88:8080; } server { listen 80; server_name www.test.com; ...
- 2017-09-23-ArchData
layout: post title: ArchData category: Technical tags: [分布式,区块链,AI,大数据] ArchData 技术峰会 神经网络和函数式编程 杨博: ...
- 关于layui部分表单不显示的问题(Select, checkBox)
原因: 没有使用JS进行初始化 官方说明: https://www.layui.com/doc/base/faq.html layui.use('form', function(){ var form ...
- reverse 的用法
直接对数组或是数据结构使用 #include<bits/stdc++.h> using namespace std; ]={,,,,,};//申请6个元素,下标从0开始,最后一个下标是5 ...
- 转:CentOS上安装LAMP之第三步:MySQL环境及安装过程报错解决方案(纯净系统环境)
这是AMP运行环境中最后配置的环境: 惯例传送门: 1.编译安装MySQL cd /home/zhangatle/tar tar zxvf mysql-.tar.gz cd mysql- cmake ...
- chrome 浏览器 添加访问助手来访问网上应用商店
chrome浏览器的强大之处,在于可以chrome浏览器的扩展程序来实现很多功能.然而不能下载扩展程序.可以借助chrome访问助手来实现: 下载chrome访问助手:https://pan.baid ...
- 2019-1-29-Sublime-Text-安装中文、英文字体
title author date CreateTime categories Sublime Text 安装中文.英文字体 lindexi 2019-01-29 16:35:25 +0800 201 ...
- git gc干了啥
前几天在写升级项目的时候发现./git/objects/pack/下的idx和pack文件是只读的,用java在windows下删除会抛异常,然后把只读属性改掉就好了. 于是就想弄清楚这两个文件的作用 ...