POJ 3169 Layout (差分约束)
题意:给定一些母牛,要求一个排列,有的母牛距离不能超过w,有的距离不能小于w,问你第一个和第n个最远距离是多少。
析:以前只是听说过个算法,从来没用过,差分约束。
对于第 i 个母牛和第 i+1 个,D[i] - D[i+1] <= 0, D[j] -D[i ]<= k, D[i] - D[j] <= - k,那么这个题就可以用差分约束来求这个不等式组了。
1.对于差分不等式,a - b <= c ,建一条 b 到 a 的权值为 c 的边,求的是最短路,得到的是最大值(本题求的就是最大值),对于不等式 a - b >= c ,建一条 b 到 a 的权值为 c 的边,求的是最长路,得到的是最小值。
2.如果检测到负环,那么无解。
3.如果d[n]没有更新,那么可以是任意解。
代码如下:
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 10000 + 5;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int n, m;
struct node{
int to, w, next, from;
};
int cnt = 0;
node G[maxn<<1];
int d[maxn]; void add(int u, int v, int w){
G[cnt].w = w;
G[cnt].to = v;
G[cnt++].from = u;
} bool Bell_Ford(){
for(int i = 1; i <= n; ++i) d[i] = INF; d[1] = 0;
for(int i = 0; i < n; ++i){
for(int j = 0; j < cnt; ++j){
int u = G[j].from;
int v = G[j].to;
int w = G[j].w;
if(d[u] < INF && d[v] > d[u] + w){
d[v] = d[u] + w;
}
}
} for(int j = 0; j < cnt; ++j){
int u = G[j].from;
int v = G[j].to;
int w = G[j].w;
if(d[u] < INF && d[v] > d[u] + w)
return true;
} return false;
} int main(){
// freopen("in.txt", "r", stdin);
int ml, md, u, v, w;
cin >> n >> ml >> md;
cnt = 0; for(int i = 0; i < ml; ++i){
scanf("%d %d %d", &u, &v, &w);
if(u < v) swap(u, v);
add(v, u, w);
} for(int i = 0; i < md; ++i){
scanf("%d %d %d", &u, &v, &w);
if(u < v) swap(u, v);
add(u, v, -w);
} if(Bell_Ford()) printf("-1\n");
else if(d[n] == INF) printf("-2\n");
else printf("%d\n", d[n]);
return 0;
}
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(差分约束啊)
题目链接: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 ...
随机推荐
- codeforces 450 B Jzzhu and Sequences
题意:给出f1=x,f2=y,f(i)=f(i-1)+f(i+1),求f(n)模上10e9+7 因为 可以求出通项公式:f(i)=f(i-1)-f(i-2) 然后 f1=x; f2=y; f3=y-x ...
- xxx_cast类型转换
xxx_cast是一个统称,它指的是static_cast(静态转换),const_cast(常量转换),reinterpert_cast(重解释转换),dynamic_cast(动态转换).本次我们 ...
- I.MX6 ov5640 camera
/************************************************************************ * I.MX6 ov5640 camera * 说明 ...
- JDK,JRE,JVM区别与联系-理解与概括
我们利用JDK(调用JAVA API)开发了属于我们自己的JAVA程序后,通过JDK中的编译程序(javac)将我们的文本java文件编译成JAVA字节码,在JRE上运行这些JAVA字节码,JVM解析 ...
- HDU 5038 Grade
解题思路:这题最关键的是要读懂题意,If not all the value are the same but the frequencies of them are the same, there ...
- Android Studio 小技巧合集
本文翻译自 Android Studio Tips by Philippe Breault,一共收集了62个 Android Studio 使用小技巧和快捷键. 根据这些小技巧的使用场景,本文将这62 ...
- php的header()大全
<?php /*** Function: PHP header() examples (PHP) ** Desc: Some examples on how to use the header( ...
- android学习——MeasureSpec介绍及使用
一.MeasureSpc类说明 SDK的介绍:MeasureSpc类封装了父View传递给子View的布局(layout)要求.每个MeasureSpc实例代表宽度或者高度 它有三种模式:①.UNSP ...
- HP-Socket
HP-Socket 是一套通用的高性能 TCP/UDP 通信框架,包含服务端组件.客户端组件和Agent组件,广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C++.C#.D ...
- 13、NFC技术:读写非NDEF格式的数据
MifareUltralight数据格式 将NFC标签的存储区域分为16个页,每一个页可以存储4个字节,一个可存储64个字节(512位).页码从0开始(0至15).前4页(0至3)存储了NFC标签相关 ...