POJ 3169 Layout (差分约束系统)
Layout
题目链接:
Rhttp://acm.hust.edu.cn/vjudge/contest/122685#problem/S
Description
```
Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 Some cows like each other and want to be within a certain distance of each other in line. Some really dislike each other and want to be separated by at least a certain distance. A list of ML (1 <= ML <= 10,000) constraints describes which cows like each other and the maximum distance by which they may be separated; a subsequent list of MD constraints (1 <= MD <= 10,000) tells which cows dislike each other and the minimum distance by which they must be separated.
Your job is to compute, if possible, the maximum possible distance between cow 1 and cow N that satisfies the distance constraints.
</big>
##Input
<big>
Line 1: Three space-separated integers: N, ML, and MD.
Lines 2..ML+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at most D (1 <= D <= 1,000,000) apart.
Lines ML+2..ML+MD+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at least D (1 <= D <= 1,000,000) apart.
</big>
##Output
<big>
Line 1: A single integer. If no line-up is possible, output -1. If cows 1 and N can be arbitrarily far apart, output -2. Otherwise output the greatest possible distance between cows 1 and N.
</big>
##Sample Input
<big>
4 2 1
1 3 10
2 4 20
2 3 3
</big>
##Sample Output
<big>
27
</big>
##Hint
<big>
</big>
<br/>
##题意:
<big>
以两种形式给出若干组大小关系:
A和B的权值差最多是D.
A和B的权值差最少是D.
求#1和#N之间的最大权值差.
</big>
<br/>
##题解:
<big>
差分约束系统. 还需强化练习. 挖坑待填.
</big>
<br/>
##代码:
``` cpp
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 31000
#define mod 1000000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
int n,m1,m2;
int u[maxn],v[maxn],w[maxn];
int first[maxn], _next[maxn], edges;
int dis[maxn];
void add_edge(int s, int t, int ww) {
u[edges] = s; v[edges] = t; w[edges] = ww;
_next[edges] = first[s];
first[s] = edges++;
}
int bell_man(int s, int t) {
for(int i=1; i<=n; i++) dis[i]=inf; dis[s] = 0;
for(int i=1; i<=n; i++) {
for(int e=0; e<edges; e++) if(dis[v[e]] > dis[u[e]]+w[e]){
dis[v[e]] = dis[u[e]]+w[e];
if(i == n) return -1;
}
}
if(dis[t] == inf) return -2;
return dis[t];
}
int main(void)
{
//IN;
while(scanf("%d %d %d", &n, &m1, &m2) != EOF)
{
memset(first, -1, sizeof(first)); edges = 0;
while(m1--) {
int u,v,w; scanf("%d %d %d", &u, &v, &w);
add_edge(u, v, w);
}
while(m2--) {
int u,v,w; scanf("%d %d %d", &u, &v, &w);
add_edge(v, u, -w);
}
for(int i=1; i<n; i++)
add_edge(i+1, i, 0);
int ans = bell_man(1, n);
printf("%d\n", ans);
}
return 0;
}
POJ 3169 Layout (差分约束系统)的更多相关文章
- POJ 3169 Layout 差分约束系统
介绍下差分约束系统:就是多个2未知数不等式形如(a-b<=k)的形式 问你有没有解,或者求两个未知数的最大差或者最小差 转化为最短路(或最长路) 1:求最小差的时候,不等式转化为b-a>= ...
- POJ 3169 Layout(差分约束啊)
题目链接:http://poj.org/problem? id=3169 Description Like everyone else, cows like to stand close to the ...
- 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 差分约束模板题
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6415 Accepted: 3098 Descriptio ...
- POJ 3169 Layout (差分约束)
题意:给定一些母牛,要求一个排列,有的母牛距离不能超过w,有的距离不能小于w,问你第一个和第n个最远距离是多少. 析:以前只是听说过个算法,从来没用过,差分约束. 对于第 i 个母牛和第 i+1 个, ...
- PKU 3169 Layout(差分约束系统+Bellman Ford)
题目大意:原题链接 当排队等候喂食时,奶牛喜欢和它们的朋友站得靠近些.FJ有N(2<=N<=1000)头奶牛,编号从1到N,沿一条直线站着等候喂食.奶牛排在队伍中的顺序和它们的编号是相同的 ...
- POJ 3169 Layout(差分约束 线性差分约束)
题意: 有N头牛, 有以下关系: (1)A牛与B牛相距不能大于k (2)A牛与B牛相距不能小于k (3)第i+1头牛必须在第i头牛前面 给出若干对关系(1),(2) 求出第N头牛与第一头牛的最长可能距 ...
- POJ 3169 Layout (spfa+差分约束)
题目链接:http://poj.org/problem?id=3169 差分约束的解释:http://www.cnblogs.com/void/archive/2011/08/26/2153928.h ...
- poj 3169 Layout(差分约束)
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6549 Accepted: 3168 Descriptio ...
随机推荐
- multi-cursor
可以进行多处同时编辑 用C-n选择第一个单词,再次按住选住下一个单词,C-p放弃当前选中的,返回到第上一个,C-x放弃当前选中的,光标到下一处 选中一段文本后用:MultipleCursorsFind ...
- i386 和amd64 的意思
首先可以简化一个概念,i386=Intel 80386.其实i386通常被用来作为对Intel(英特尔)32位微处理器的统称. Windows NT类系统的安装盘上,通常i386是其根上的一个文件夹, ...
- 【笨嘴拙舌WINDOWS】伟大的变革
"改革"."革命"."变革" 这几个词语毫无疑问是每一个时代必须被呼吁的词语,当一个国家没有人求变时,那是一个时代的悲剧.无论是文景之治,贞 ...
- UVa 10817 (状压DP + 记忆化搜索) Headmaster's Headache
题意: 一共有s(s ≤ 8)门课程,有m个在职教师,n个求职教师. 每个教师有各自的工资要求,还有他能教授的课程,可以是一门或者多门. 要求在职教师不能辞退,问如何录用应聘者,才能使得每门课只少有两 ...
- NYOJ 536 开心的mdd【矩阵链乘】
题意:给出n个矩阵组成的序列,问最少的运算量 看的紫书: dp[i][j]表示从第i个矩阵到第j个矩阵最少的乘法次数 dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j] ...
- Android Studio 我常用快捷键
0. Ctrl+Alt+L 格式化代码 Ctrl+Alt+O 优化导入的类 1. 重载方法 Ctrl+O 2.Ctrl+shift+Enter:自动匹配相对应的语法结构,比如if,do-while,t ...
- POJ 2584 T-Shirt Gumbo (二分图多重最大匹配)
题意 现在要将5种型号的衣服分发给n个参赛者,然后给出每个参赛者所需要的衣服的尺码的大小范围,在该尺码范围内的衣服该选手可以接受,再给出这5种型号衣服各自的数量,问是否存在一种分配方案使得每个选手都能 ...
- OK335xS 网络连接打印信息 hacking
/*********************************************************************** * OK335xS 网络连接打印信息 hacking ...
- I.MX6 bq27441 driver porting
/************************************************************************** * I.MX6 bq27441 driver p ...
- MYSQL自动备份策略的选择
目前流行几种备份方式: 1.逻辑备份:使用mysql自带的mysqldump工具进行备份.备份成sql文件形式.优点:最大好处是能够与正在运行的mysql自动协同工作,在运行期间可以确保备份是当时的点 ...