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 (差分约束系统)的更多相关文章

  1. POJ 3169 Layout 差分约束系统

    介绍下差分约束系统:就是多个2未知数不等式形如(a-b<=k)的形式 问你有没有解,或者求两个未知数的最大差或者最小差 转化为最短路(或最长路) 1:求最小差的时候,不等式转化为b-a>= ...

  2. POJ 3169 Layout(差分约束啊)

    题目链接:http://poj.org/problem? id=3169 Description Like everyone else, cows like to stand close to the ...

  3. POJ 3169 Layout(差分约束+链式前向星+SPFA)

    描述 Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 ...

  4. poj 3169 Layout 差分约束模板题

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6415   Accepted: 3098 Descriptio ...

  5. POJ 3169 Layout (差分约束)

    题意:给定一些母牛,要求一个排列,有的母牛距离不能超过w,有的距离不能小于w,问你第一个和第n个最远距离是多少. 析:以前只是听说过个算法,从来没用过,差分约束. 对于第 i 个母牛和第 i+1 个, ...

  6. PKU 3169 Layout(差分约束系统+Bellman Ford)

    题目大意:原题链接 当排队等候喂食时,奶牛喜欢和它们的朋友站得靠近些.FJ有N(2<=N<=1000)头奶牛,编号从1到N,沿一条直线站着等候喂食.奶牛排在队伍中的顺序和它们的编号是相同的 ...

  7. POJ 3169 Layout(差分约束 线性差分约束)

    题意: 有N头牛, 有以下关系: (1)A牛与B牛相距不能大于k (2)A牛与B牛相距不能小于k (3)第i+1头牛必须在第i头牛前面 给出若干对关系(1),(2) 求出第N头牛与第一头牛的最长可能距 ...

  8. POJ 3169 Layout (spfa+差分约束)

    题目链接:http://poj.org/problem?id=3169 差分约束的解释:http://www.cnblogs.com/void/archive/2011/08/26/2153928.h ...

  9. poj 3169 Layout(差分约束)

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6549   Accepted: 3168 Descriptio ...

随机推荐

  1. 服务--web服务

    .面向对象和面向组件 .什么是Web服务 Web Service "Stack" .Web服务的应用分类 Web服务都是对象/组件技术在Internet中的延伸 面向对象和面向组件 ...

  2. 淘宝技术发展(Java时代:脱胎换骨)

    我的师父黄裳@岳旭强曾经说过,“好的架构图充满美感”,一个架构好不好,从审美的角度就能看得出来.后来我看了很多系统的架构,发现这个言论基本成立.那么反观淘宝前面的两个版本的架构,你看哪个比较美? 显然 ...

  3. UVALive 4043 Ants(二分图完美匹配)

    题意:每个蚁群有自己的食物源(苹果树),已知蚂蚁靠气味辨别行进方向,所以蚁群之间的行动轨迹不能重叠.现在给出坐标系中n个蚁群和n棵果树的坐标,两两配对,实现以上要求.输出的第 i 行表示第 i 个蚁群 ...

  4. angular js 实例参数学习

    <!DOCTYPE html> <html> <head> <meta name="description" content=" ...

  5. 利用ffmpeg解码h264流的代码

    这里也直接给出代码: h264dec.h: #pragma once #include "tdll.h" #include "avcodec.h" #inclu ...

  6. hdu 1211 RSA

    // 表示题目意思我是理解了蛮久 英语太水了 //首先这是解密公式 m=c^d mod n// 给你 p q e 然后 n=p*q fn=(p-1)*(q-1)// 给你 e,根据公式 e*d mod ...

  7. 去除 waring Method 'CreateNew' hides virtual method of base type 'TCustomForm'

    最近整理前人的代码,有好多的hint和waring, 其中整理到Method 'CreateNew' hides virtual method of base type 'TCustomForm', ...

  8. OracleHelper(for produce)

    OracleHelper中,有一个用存储过程实现的Insert方法. 然后我把执行存储过程的方法 封装成了可以执行任何存储过程,参数是 存储过程名称 以及存错过程中的传入.传出参数 using Ora ...

  9. linux 下 NetBeans 字体大小设置

    在linux mint 12下安装了 NetBeans7.1.2使用之后,觉得字体不好看,字体普遍特别大,分三个方面改NetBeans的字体. 1. 代码字体大小 点击NetBeans菜单,工具--& ...

  10. 【转】linux线程模型

    一.定义 关于进程.轻量级进程.线程.用户线程.内核线程的定义,这个很容易找到,但是看完之后你可以说你懂了,但实际上你真的明白了么? 在现代操作系统中,进程支持多线程.进程是资源管理的最小单元:而线程 ...