博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3169 Layout
阅读量:4562 次
发布时间:2019-06-08

本文共 4120 字,大约阅读时间需要 13 分钟。

Layout
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13261   Accepted: 6361

Description

Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbered, and since they 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

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.

Output

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.

Sample Input

4 2 11 3 102 4 202 3 3

Sample Output

27

Hint

Explanation of the sample: 
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.
 
题意:
•       N个人编号为1-N,并且按照编号顺序排成一条直线,两个人的位置可能重合,然后给定一些约束条件。
•       X(X <= 100000)组约束Ax Bx Cx(1 <= Ax < Bx <= N),表示Ax和Bx的距离不能大于Cx。
•       Y(X <= 100000)组约束Ay By Cy(1 <= Ay < By <= N),表示Ay和By的距离不能小于Cy。
•       如果这样的排列存在,输出1-N这两个人的最长可能距离,如果不存在,输出-1,如果无限长输出-2。
题解:
求最大解转换成最短路 形如
ai -
bi <=
ki 的方程组
注意是按顺序排的,所以有
ai -
ai-1 >= 0 即
ai-1 -
ai <= 0
#include
#include
#include
#include
using namespace std;const int maxn = 1005,maxm = 40005, inf = 1000000008;int head[maxn],dis[maxn],vis[maxn],N,Ml,Nl,tot;bool inq[maxn];struct edge{ int to, nxt, w;}G[maxm];void add(int u,int v,int w){ G[++tot].nxt = head[u]; G[tot].to = v; G[tot].w = w; head[u] = tot;}int SPFA(){ queue
Q; for(int i = 1; i <= N; i++)dis[i] = inf; Q.push(1); inq[1] = 1; vis[1]++; dis[1] = 0; while(!Q.empty()){ int u = Q.front(); Q.pop(); inq[u] = 0; for(int i = head[u]; i; i = G[i].nxt){ int v = G[i].to; if(dis[v] > dis[u] + G[i].w){ dis[v] = dis[u] + G[i].w; if(!inq[v]){ if(++vis[v] > N)return 0;//入队时判负环 Q.push(v); inq[v] = 1; } } } } if(dis[N] == inf)return 2; return 1; }int main(){ scanf("%d%d%d",&N,&Ml,&Nl); for(int i = 1; i <= N; i++) add(i, i-1, 0); for(int i = 1; i <= Ml; i++){ int u, v, w; scanf("%d%d%d",&u,&v,&w); add(u, v, w); } for(int j = 1; j <= Nl; j++){ int u, v, w; scanf("%d%d%d",&u,&v,&w); add(v, u, -w); } int p = SPFA(); if(!p)cout<<"-1"<

差束约分求最大解跑最小路,求最小解跑最长路,是反的,一定不要搞反了啊

 
 

转载于:https://www.cnblogs.com/EdSheeran/p/8467354.html

你可能感兴趣的文章
java处理excel-xlsx格式大文件的解决方案
查看>>
collections工具类
查看>>
jQuery EasyUI 表单插件 - Combobox 组合框
查看>>
高质量程序设计指南c++/c语言(18)--使用断言
查看>>
Spring的自动扫描与管理
查看>>
C++数据成员的内存布局
查看>>
英国体系环境下项目有什么特征(一)
查看>>
获取点击元素的绝对位置
查看>>
hdu1495 bfs搜索、模拟
查看>>
adb启动和关闭
查看>>
Linux磁盘分区的实用管理命令
查看>>
MFC控件编程之鼠标跟键盘消息
查看>>
【ASP.NET Core】给路由规则命名有何用处
查看>>
linux(4) vi编辑/删除、复制、粘贴 /bash shell 环境变量设置/数据流重定向 | 的用法...
查看>>
Ubuntu 下面安装 JDK1.7
查看>>
ECharts的x轴和y轴均使用数值类型
查看>>
Windows Phone 7Silverlight控件之--Panorama
查看>>
Liunx下的有关于tomcat的相关操作 && Liunx 常用指令
查看>>
PMP考试通过
查看>>
js声明 对象,数组 的方法
查看>>