博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3169 Layout (差分约束)
阅读量:6322 次
发布时间:2019-06-22

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

Layout
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5190   Accepted: 2491

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.

Source

 
 
题意:N头牛排队吃饭 排编号顺序排,大的永远在小的前面,但牛之间有的关系好,有的差,所以有的牛想离某些牛的距离最远不超过D 有的必须大于D 给出它们的关系 求第n头牛跟第一头的最远距离
思路:差分问题,求最大值 约束条件转化为 < ; 所以有S大-S小 <= D1,
S大-S小>=D2 把这个条件转化一下==> S小-S大<=-D2
然后就是spfa了
 
 
#include 
#include
#define VM 10005#define EM 200500 //原先数据都是少一个0的,但RE 不知为什么 多加一个就过了#define inf 9999999999struct edge{ int v,w,next;}e[EM];int head[VM],ep;void addedge (int cu,int cv,int cw){ ep ++; e[ep].v = cv; e[ep].w = cw; e[ep].next = head[cu]; head[cu] = ep;}__int64 spfa (int n){ int vis[VM],cnt[VM],queue[VM]; __int64 dis[VM]; for (int i = 1;i <= n;i ++) { vis[i] = 0; cnt[i] = 0; dis[i] = inf; } dis[1] = 0; int front = -1,rear = 0; vis[1] = 1; cnt[1] = 1; queue[rear ++] = 1; while (front != rear) { front = (front+1)%(n+1); int u = queue[front]; vis[u] = 0; for (int i = head[u];i != -1;i = e[i].next) { int v = e[i].v; if (dis[v] > dis[u]+e[i].w) { dis[v] = dis[u] + e[i].w; if (!vis[v]) { vis[v] = 1; cnt[v] ++; if (cnt[v] >= n) return 0; //有负环输出-1 queue[rear] = v; rear = (rear+1)%(n+1); } } } } if (dis[n] == inf) return -1; //不可达点 else return dis[n];}int main (){ int n,ml,md; int v1,v2,cost; scanf ("%d%d%d",&n,&ml,&md); memset (head,-1,sizeof(head)); ep = 0; while (ml --) { scanf ("%d%d%d",&v1,&v2,&cost); if (v1 < v2) //编号大的牛永远在小的牛前面 addedge (v1,v2,cost); else addedge (v2,v1,cost); } while (md --) { scanf ("%d%d%d",&v1,&v2,&cost); if (v2 < v1) addedge (v1,v2,-1*cost); else addedge (v2,v1,-1*cost); } __int64 ans = spfa (n); if (ans == 0) printf ("-1\n"); else if (ans == -1) printf ("-2\n"); else printf ("%I64d\n",ans); return 0;}

 

转载地址:http://cuvaa.baihongyu.com/

你可能感兴趣的文章
将input或textarea设置为disabled的样式问题
查看>>
HDU 2017 多校联合Contest 3
查看>>
Mysqlreport总结
查看>>
Ubuntu14.04忘记root密码的解决方法
查看>>
JS中解决中文乱码的2种方法
查看>>
C语言学习大纲
查看>>
spring boot 官方文档笔记
查看>>
JVM 运行时数据区
查看>>
谈STL的重要应用与实现
查看>>
Android中的关于MDM中的几个方法举例
查看>>
optimize table tablename
查看>>
树五:二叉搜索树的后序遍历序列
查看>>
laravel 表单提交注意数据到达的方法
查看>>
东软HIS切换输入法卡死的解决方法
查看>>
vc 使用SetWindowPos改变窗体的大小,实现折叠,展开
查看>>
关于windows修改远程登录端口的问题
查看>>
安装CocoaPods学习
查看>>
async 常用函数总结
查看>>
过渡函数transition-timing-function
查看>>
http与websocket(基于SignalR)两种协议下的跨域基于ASP.NET MVC--竹子整理
查看>>