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 ...
随机推荐
- C++ 11 vlearning
1.新增算术类型 longlong,最小不比long小,一般为64位. 2.列表初始化 int units_sold = {0};或者 int units_sold{0};非11标准 ...
- HDFS小文件处理——Mapper处理
处理小文件的时候,可以通过org.apache.hadoop.io.SequenceFile.Writer类将所有文件写出到一个seq文件中. 大致流程如下: 实现代码: package study. ...
- 自定义View(2)canas绘制基本图形的示例
效果 代码: void drawSample(Canvas canvas) { /* * 方法 说明 drawRect 绘制矩形 drawCircle 绘制圆形 drawOval 绘制椭圆 drawP ...
- BZOJ 3143 游走(高斯消元)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3143 题意:一个无向连通图,顶点从1编号到n,边从1编号到m.小Z在该图上进行随机游走, ...
- Xcode5 编译ffmpeg,arm64版本;H264
编译选项:./configure —-cc=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchai ...
- LeetCode Swap Nodes in Pairs 交换结点对(单链表)
题意:给一个单链表,将其每两个结点交换,只改尾指针,不改元素值. 思路:迭代法和递归法都容易写,就写个递归的了. 4ms /** * Definition for singly-linked list ...
- 《C++ Primer 4th》读书笔记 第6章-语句
原创文章,转载请注明出处: http://www.cnblogs.com/DayByDay/p/3912407.html
- JOB的创建,定时,执行
--建表 create table test_job(para_date date); commit; insert into test_job values(sysdate); commit; ...
- delphi 编译的时候 把Warning去除的方法
delphi 编译的时候 把Warning去除的方法 在 添加 {$WARNINGS OFF}
- 深入浅出 iOS 之生命周期
转:http://blog.csdn.net/kesalin/article/details/6691766 iOS应用程序的生命周期相比 Android 应用程序的生命周期来说,没那么简明易懂,但是 ...