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. TagHighlight

    :UpdateTypesFile 命令会生成相应的上色高亮文件(生成的着色文件可以叫types_c.taghl)还有相应的tags文件,寻找tags或taghl_config.txt确定project ...

  2. HDU 4717 The Moving Points(三分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4717 题意:给出n个点的坐标和运动速度(包括方向).求一个时刻t使得该时刻时任意两点距离最大值最小. ...

  3. eclipse导入javax.servlet.*的方法

    1.下载web应用服务器tomact,网址http://tomcat.apache.org/download-80.cgi 这个根据自己系统进行选择. 2.将其加压到电脑中 3.在eclipse中添加 ...

  4. hdu 1513 && 1159 poj Palindrome (dp, 滚动数组, LCS)

    题目 以前做过的一道题, 今天又加了一种方法 整理了一下..... 题意:给出一个字符串,问要将这个字符串变成回文串要添加最少几个字符. 方法一: 将该字符串与其反转求一次LCS,然后所求就是n减去 ...

  5. 简单实现WPF界面控件换肤效果

    效果如下如图:选择皮肤颜色 1.首先新建一个如图界面: 选择匹夫下拉框Xaml代码如下:三种颜色选项,并触发SelectionChanged事件 <ComboBox Height="2 ...

  6. IIS修改队列长度(IIS6+IIS7)

    Internet Information Services (IIS) 限制了在任何给定时间可在队列中等待的应用程序池请求的最大数量.如果达到此限制,则所有新请求都将被拒绝,而且用户将收到错误消息“5 ...

  7. 注解框架ButterKnife

    将插件升级到1.3后支持Android Studio1.3 + ButterKnife7 如何使用 有所使用的布局 ID 上点击右键 (例如上图中的 R.layout.activity_setting ...

  8. 【C#学习笔记】List容器使用

    using System; using System.Collections.Generic; namespace ConsoleApplication { class Program { stati ...

  9. ECshop 二次开发模板教程1

    本教程适用于了解 ECshop 和 ECshop模板DIY 以及它们的日常使用,在查看前阁下需要至少会使用一种编辑器(exp:Dreamweaver, editplus, emacs, vi, ee  ...

  10. SQL Server 2012安装时如何不安装自带的Visual Studio

    不安装以下两个: