POJ3169 Layout
Description
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.
Input
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.
Output
Sample Input
4 2 1
1 3 10
2 4 20
2 3 3
Sample Output
27
There are 4 cows. Cows #1 and #3 must be no more than 10 units apart, cows #2 and #4 must be no more than 20 units apart, and cows #2 and #3 dislike each other and must be no fewer than 3 units apart.
The best layout, in terms of coordinates on a number line, is to put cow #1 at 0, cow #2 at 7, cow #3 at 10, and cow #4 at 27.
Source
正解:差分约束系统+SPFA
解题报告:
大概题意是给定排成一列的牛,然后两头牛之间的距离可能要大于等于某个值或者小于等于某个值,问是否存在或者终点是否可以无限远。
以前在codevs上面做过一道差分约束系统的题,然后就学会了这种神奇的思想。其实思想很简单,结合图论的话还是很有用的。
考虑题意,需要求1到n的最大距离。题目中给了很多限制条件,比如说x2-x1<=3,x4-x2>=6这样的条件。我们考虑像x2-x1<=3这样的条件,因为我们想让距离尽可能大,就要使距离最大化,然后建图,1向2连一条权值为3的边。那么像x4-x2>=6这样大于的怎么办呢,我们就可以把它变成x2-x4<=-6,边权为负即可。然后图上跑SPFA。
接着是个很重要的问题,是最短路还是最长路呢?按理说要想距离大应该跑最长路,但是我们想,我们这个图是怎么建的,根据每个条件的最大条件连边,那么说明我们肯定要取所有对这个点的约束中最小的那个(取交),所以只会越来越小。不难想到,最后求出的dis[n]就是我们要求的。
题意中的-1、-2怎么特判呢?如果有负权环就说明不可能,记一下每个点入队n次就说明有负权环。而可以无限大则说明还到不了n,则说明dis[n]仍为初值
轻松AC,代码如下:
//It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#ifdef WIN32
#define OT "%I64d"
#else
#define OT "%lld"
#endif
using namespace std;
typedef long long LL;
const int MAXN = ;
const int MAXM = ;
const int inf = (<<);
int n,m1,m2;
int first[MAXN],to[MAXM],next[MAXM],w[MAXM],ecnt;
int dis[MAXN];
queue<int>Q;
bool pd[MAXN];
int cnt[MAXN]; inline int getint()
{
int w=,q=;
char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar();
if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar();
return q ? -w : w;
} inline void link(int x,int y,int z){
next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y; w[ecnt]=z;
} inline bool spfa(){
Q.push(); pd[]=;
for(int i=;i<=n;i++) dis[i]=inf;
while(!Q.empty()){
int u=Q.front(); Q.pop(); pd[u]=;
for(int i=first[u];i;i=next[i]) {
int v=to[i];
if(dis[v]>dis[u]+w[i]) {
dis[v]=dis[u]+w[i];
if(!pd[v]) {
Q.push(v);
pd[v]=;
cnt[v]++;
if(cnt[v]>=n) return false;
}
}
}
}
if(dis[n]==inf) printf("-2");
else printf("%d",dis[n]);
return true;
} inline void solve(){
n=getint(); m1=getint(); m2=getint();
int x,y,z;
for(int i=;i<=m1;i++) {
x=getint();y=getint();z=getint();
link(x,y,z);
}
for(int i=;i<=m2;i++) {
x=getint(); y=getint(); z=getint();
link(y,x,-z);
}
if(!spfa()) printf("-1");
} int main()
{
solve();
return ;
}
POJ3169 Layout的更多相关文章
- POJ3169 Layout(差分约束系统)
POJ3169 Layout 题意: n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0.这些牛的距离存在着一些约束关系:1.有ml组(u, v, w)的约束关系,表示牛 ...
- POJ-3169 Layout (差分约束+SPFA)
POJ-3169 Layout:http://poj.org/problem?id=3169 参考:https://blog.csdn.net/islittlehappy/article/detail ...
- POJ3169:Layout(差分约束)
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15705 Accepted: 7551 题目链接:http ...
- POJ-3169 Layout 最短路 差分约束
题目链接:https://cn.vjudge.net/problem/POJ-3169 题意 Farmer John手下的一些牛有自己喜欢的牛,和讨厌的牛 喜欢的牛之间希望距离在给定距离D之内 讨厌的 ...
- [USACO2005][POJ3169]Layout(差分约束)
题目:http://poj.org/problem?id=3169 题意:给你一组不等式了,求满足的最小解 分析: 裸裸的差分约束. 总结一下差分约束: 1.“求最大值”:写成"<=& ...
- POJ3169:Layout(差分约束)
http://poj.org/problem?id=3169 题意: 一堆牛在一条直线上按编号站队,在同一位置可以有多头牛并列站在一起,但编号小的牛所占的位置不能超过编号大的牛所占的位置,这里用d[i ...
- 转自作者:phylips@bmy
差分约束系统 2008-11-28 20:53:25| 分类: 算法与acm|举报|字号 订阅 出处:http://duanple.blog.163.com/blog/static/7097 ...
- 【POJ3169 】Layout (认真的做差分约束)
Layout Description Like everyone else, cows like to stand close to their friends when queuing for ...
- 【图论】POJ-3169 差分约束系统
一.题目 Description Like everyone else, cows like to stand close to their friends when queuing for feed ...
随机推荐
- NGUI OnChange Event
那些组件有OnChange? 下面这些组件都有OnChange事件,当你点击,下拉选择时,就会触发它们. NGUI中对应的组件 PopupList (下拉列表) Toggle (复选框) Input ...
- 关于js中onclick字符串传参问题
规则: 外变是“”,里面就是‘’外边是‘’,里边就是“” 示例: var a="111"; var html="<a onclick='selecthoods( ...
- Qt——鼠标拖动调整窗口大小
要求:鼠标移到界面边角时,鼠标样式相应地发生改变. 实现方法一: 重写mouseMoveEvent,如果鼠标没有按下,则根据鼠标在界面上的位置设置鼠标样式,如果鼠标按下,则根据位置判断该怎样调整界面大 ...
- VSS迁移备忘
今天早上服务器down掉了,没办法,只能把vss数据文件目录一并压缩,拷贝到本机.然后准备利用本机做服务端.下面是操作步骤: 1.将拷贝下来的文件夹设置为共享. 2.打开Microsoft Visua ...
- Asp.net Json数据解析的一种思路
在日常的编码中,经常会遇到JSON类型的数据,有简单的,也有复杂的.对于简单的,我们可以用正则等匹配,但是一旦遇到复杂的,就比较难办了. 数据分析 目前手头上需要制作一个天气预报功能,现成的接口已经有 ...
- 构建高转化率的着陆页-PS+HTML+网络营销
课程简介 本课程是全网独家专业的着陆页课程,课程完整的再现了整个着陆页实战案例的开发过程,包括:策划.设计和实现.上线后的推广.优化及提高转化率的技巧等,本套课程能帮助您迅速掌握着陆页的能力,迅速洞察 ...
- Android Studio 2.2 来啦
今年的 I/O 2016 Google 放出了 Android Studio 2.2 的预览版,改进了多项功能,只不过为了保证公司项目不受影响,我一般都不安装预览版的,因为预览版意味着不稳定,可能遇到 ...
- 自己写的一个关于Linq to Entity 动态查询的例子
这两天一直想写一个动态查询的方式,先是晚上查询了一下,发现大家写的差不多都是一样的[如:http://www.cnblogs.com/ASPNET2008/archive/2012/10/28/274 ...
- JqueryEasyUI教程入门篇
什么是jQueryEasyUI? JqueryUI是一组基于jQuery的UI插件集合 学习jQueryEasyUI的条件? 必须掌握Jquery的基本语法知识 jQueryEasyUI的特点? 1. ...
- [bzoj 1064][NOI2008]假面舞会(dfs判断环)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1064 分析: 如果a看到b,则a->b 那么: 1.如果图中有环,则说明这个环的 ...