poj 3169 Layout(差分约束)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6549 | Accepted: 3168 |
Description
can be rather pushy, it is possible that two or more cows can line up at exactly the same location (that is, if we think of each cow as being located at some coordinate on a number line, then it is possible for two or more cows to share the same coordinate).
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
解题思路:
1、构造差分约束系统:
A。B距离不超过D则B-A<=D,
A,B距离至少为D则A-B<=-D.
2、若有解则求1和N之间的最短路径:
比如:A-B<=D1 , B-C<=D2, A-C<=D3 不等式相加得:A-C<=min(D3,D1+D2)。而当A-B<=D时。我们建的边是B->A的。所以我们仅仅要求出1到N的最短距离就可以,假设没有最短距离。则输出-2.
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std; const int maxne = 1000000;
const int maxnn = 1010;
const int INF = 0x3f3f3f3f;
struct edge{
int u , v , d;
edge(int a = 0 , int b = 0 , int c = 0){
u = a , v = b , d = c;
}
}e[maxne];
int head[maxnn] , next[maxne] , cnt , dis[maxnn] , vis[maxnn] , vt[maxnn];
int N , ML , MD; void add(int u , int v , int d){
e[cnt] = edge(u , v , d);
next[cnt] = head[u];
head[u] = cnt++;
} void initial(){
for(int i = 0; i < maxnn; i++) head[i] = -1 , dis[i] = INF , vis[i] = 0 , vt[i] = 0;
for(int i = 0; i < maxne; i++) next[i] = -1;
cnt = 0;
for(int i = 1; i < N; i++){
add(0 , i , 0);
add(i+1 , i , 0);
}
add(0 , N , 0);
dis[0] = 0;
} void readcase(){
int u , v , d;
while(ML--){
scanf("%d%d%d" , &u , &v , &d);
add(u , v , d);
}
while(MD--){
scanf("%d%d%d" , &u , &v , &d);
add(v , u , -1*d);
}
} bool SPFA(int start){
queue<int> q;
q.push(start);
vt[start]++;
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = 0;
int n = head[u];
if(vt[u] > N+1){
return false;
}
while(n != -1){
int v = e[n].v;
if(dis[v] > dis[u]+e[n].d){
dis[v] = dis[u]+e[n].d;
if(vis[v] == 0){
vt[v]++;
q.push(v);
vis[v] = 1;
}
}
n = next[n];
}
}
return true;
} void computing(){
if(!SPFA(0)){
printf("-1\n");
}else{
for(int i = 0; i < maxnn; i++) dis[i] = INF;
dis[1] = 0;
SPFA(1);
if(dis[N] == INF){
printf("-2\n");
}else{
printf("%d\n" , dis[N]);
}
}
} int main(){
while(scanf("%d%d%d" , &N , &ML , &MD) != EOF){
initial();
readcase();
computing();
}
return 0;
}
poj 3169 Layout(差分约束)的更多相关文章
- 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 (差分约束)
题意:给定一些母牛,要求一个排列,有的母牛距离不能超过w,有的距离不能小于w,问你第一个和第n个最远距离是多少. 析:以前只是听说过个算法,从来没用过,差分约束. 对于第 i 个母牛和第 i+1 个, ...
- POJ 3169 Layout(差分约束啊)
题目链接:http://poj.org/problem? id=3169 Description Like everyone else, cows like to stand close to the ...
- poj 3169 Layout 差分约束模板题
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6415 Accepted: 3098 Descriptio ...
- POJ 3169 Layout(差分约束 线性差分约束)
题意: 有N头牛, 有以下关系: (1)A牛与B牛相距不能大于k (2)A牛与B牛相距不能小于k (3)第i+1头牛必须在第i头牛前面 给出若干对关系(1),(2) 求出第N头牛与第一头牛的最长可能距 ...
- ShortestPath:Layout(POJ 3169)(差分约束的应用)
布局 题目大意:有N头牛,编号1-N,按编号排成一排准备吃东西,有些牛的关系比较好,所以希望他们不超过一定的距离,也有一些牛的关系很不好,所以希望彼此之间要满足某个关系,牛可以 ...
- poj 3169&hdu3592(差分约束)
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9687 Accepted: 4647 Descriptio ...
- Bellman-Ford算法:POJ No.3169 Layout 差分约束
#define _CRT_SECURE_NO_WARNINGS /* 4 2 1 1 3 10 2 4 20 2 3 3 */ #include <iostream> #include & ...
- POJ 3169 Layout 差分约束系统
介绍下差分约束系统:就是多个2未知数不等式形如(a-b<=k)的形式 问你有没有解,或者求两个未知数的最大差或者最小差 转化为最短路(或最长路) 1:求最小差的时候,不等式转化为b-a>= ...
- POJ 3169 Layout (spfa+差分约束)
题目链接:http://poj.org/problem?id=3169 差分约束的解释:http://www.cnblogs.com/void/archive/2011/08/26/2153928.h ...
随机推荐
- python红包随机生成(隔板法)
#红包生成思路#200 块钱 10个红包#0-200 的一个轴,随机取9个点,分成10段, 每一段的值表示一个红包的大小 #把输入的 money值 * 100 拿到的数值就是分, 不用再考虑单位是元的 ...
- 安装altium designer的破解问题
无法启动此程序,因为计算机中丢失MFC71.DLL.尝试重装安装该程序 百度下搜索下载MFC71.DLL,解压后复制到c:windows/system32/下,在开始-运行下输入regsvr32 MF ...
- performSelectorOnMainThread
在多线程操作中,有一个著名的错误,叫做"Tried to obtain the web lock from a thread other than the main thread or th ...
- .NET重构(九):机房重构验收总结
导读:机房收费系统个人重构版,在寒假前,已经结束了.嗯,这一路的过程,也挺心酸的.结合师傅验收时的指导.建议,对这一段时间的学习,进行一个总结. 一.学习过程 这一阶段的学习,按照师傅给的建议是:由浅 ...
- fdisk分区自动挂载
理解/etc/fstab文件配置 首先打开这个文件我们查看下本身内容 vi /etc/fstab 或者 vim /etc/fstab 2 介绍下fstab配置 文件配置每一行属于一个配置,每个 ...
- BZOJ 2301 [HAOI2011]Problem b ——莫比乌斯反演
分成四块进行计算,这是显而易见的.(雾) 然后考虑计算$\sum_{i=1}^n|sum_{j=1}^m gcd(i,j)=k$ 首先可以把n,m/=k,就变成统计&i<=n,j< ...
- BZOJ 1087 [SCOI2005]互不侵犯King ——状压DP
[题目分析] 沉迷水题,吃枣药丸. [代码] #include <cstdio> #include <cstring> #include <iostream> #i ...
- spring之生命周期
1.容器中对的生命周期 spring可以管理 singleton作用域的bean的生命周期,spring可以精确地知道该bean何时被创建,何时被初始化完成,容器合适准备销毁该bean实例. spri ...
- spring之scope作用域
spring中,bean的作用域有五种类型:默认是单例模式, singleton prototype request session ...
- 【2018.11.8】小迟的比赛 / Yuno like cake / 格子填数
题目 $noip$ 欢乐赛真是欢乐,除了不欢乐的方面以外我都很欢乐. T1 鸡汤题目,故意输对后面的胜率又没有影响,为什么要故意输呢? 所以第二个决策是凑字用的,这题就是朴素递推概率,最后乘结果权值计 ...