POJ3169--Layout(SPFA+差分系统)
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
Hint
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.
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std; struct edge{
int from,to,cost;
};
int visit[];
int cnt[];
int d[];
queue<int> que;
int n,size,sum;
vector<edge> es; int main(){
int ml,md;
cin>>n>>ml>>md;
sum=ml+md;
es.resize(sum);
for(int i=;i<ml;i++){
int a,b,c;
cin>>a>>b>>c;
edge e={a,b,c};
es[i]=e;
}
for(int i=ml;i<ml+md;i++){
int a,b,c;
cin>>a>>b>>c;
edge e={b,a,-c};
es[i]=e;
}
for(int i=;i<n;i++){
edge e={i+,i,};
es.push_back(e);
}
size=es.size(); for(int i=;i<=n;i++)
d[i]=INT_MAX;
d[]=;
que.push();
visit[]=;
cnt[]++;
while(!que.empty()){
int p=que.front();
que.pop();
visit[p]=;
for(int i=;i<size;i++){
edge e=es[i];
if(e.from==p&&d[e.from]+e.cost<d[e.to]){
d[e.to]=d[e.from]+e.cost;
if(visit[e.to]==){
cnt[e.to]++;
if(cnt[e.to]>=n){
cout<<-;
return ;
}
que.push(e.to);
visit[e.to]=;
}
}
}
}
if(d[n]==INT_MAX)
cout<<-;
else
cout<<d[n];
return ;
}
POJ3169--Layout(SPFA+差分系统)的更多相关文章
- POJ3169:Layout(差分约束)
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15705 Accepted: 7551 题目链接:http ...
- POJ 3169 Layout (spfa+差分约束)
题目链接:http://poj.org/problem?id=3169 差分约束的解释:http://www.cnblogs.com/void/archive/2011/08/26/2153928.h ...
- [USACO2005][POJ3169]Layout(差分约束)
题目:http://poj.org/problem?id=3169 题意:给你一组不等式了,求满足的最小解 分析: 裸裸的差分约束. 总结一下差分约束: 1.“求最大值”:写成"<=& ...
- POJ-3169 Layout (差分约束+SPFA)
POJ-3169 Layout:http://poj.org/problem?id=3169 参考:https://blog.csdn.net/islittlehappy/article/detail ...
- POJ3169 Layout(差分约束系统)
POJ3169 Layout 题意: n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0.这些牛的距离存在着一些约束关系:1.有ml组(u, v, w)的约束关系,表示牛 ...
- O - Layout(差分约束 + spfa)
O - Layout(差分约束 + spfa) Like everyone else, cows like to stand close to their friends when queuing f ...
- Did Pong Lie? (差分系统 判负环)
Did Pong Lie? 时间限制: 5 Sec 内存限制: 128 MB提交: 68 解决: 15[提交][状态][讨论版] 题目描述 Doctor Pong has two arrays o ...
- (简单) POJ 3169 Layout,差分约束+SPFA。
Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...
- POJ 3169 Layout 【差分约束】+【spfa】
<题目链接> 题目大意: 一些母牛按序号排成一条直线.有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离.如果没有最大距离输出-1,如果1.n之间距离任意就 ...
随机推荐
- jQuery 实例
选择器 $(this).hide() 隐藏当前的 HTML 元素. $("p").hide() 隐藏所有 <p> 元素. $(".test").hi ...
- Java并发-ConcurrentModificationException原因源码分析与解决办法
一.异常原因与异常源码分析 对集合(List.Set.Map)迭代时对其进行修改就会出现java.util.ConcurrentModificationException异常.这里以ArrayList ...
- PAT 1008 数组元素循环右移问题 (20)(代码)
1008 数组元素循环右移问题 (20)(20 分) 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A ...
- String 练习
package com.hanqi; import java.util.Random; public class Text { public static void main(String[] arg ...
- WebDriverException:Message:'geckodriver'executable needs to be in Path
geckodriver是一原生态的第三方浏览器,对于selenium3.x版本都会使用geckodriver来驱动firefox,所以需要下载geckodriver.exe,下载地址:https:// ...
- Java 中转换为String类型的四种方法
1. 使用 String 的构造方法,用于 byte[], char[], StringBuffer, StringBuilder 类型 2. 使用 String 的静态方法 valueOf() 推荐 ...
- flask部署
https://blog.csdn.net/zhuod/article/details/77850783
- Linq去重 不用实现IEqualityComparer接口的方法超级简单
RskFactorRelation.Instance.GetCache<RskFactorRelation>(true).Where(x => !string.IsNullOrEmp ...
- HttpURLConnection 返回汉字乱码(全是问号)
public static String doPost(String urlStr, Map<String, Object> paramMap) throws Exception { UR ...
- 【UI测试】--合理性