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之间距离任意就 ...
随机推荐
- u-boot之NAND启动与NOR启动的区别
nand启动与nor启动的区别主要分为以下几部分说明: 1.nand flash与nor flash的最主要区别 2.s3c2440的nand启动与nor启动原理 3.nand启动与nor启动的时候u ...
- SDK Manager 基础下载
双击SDK Manager打开Android SDK Manager. 全选以下几项 创建新项目 更改gradle的地址: 更改app的build.gradle: android { buildToo ...
- (转)android拨打电话崩溃6.0以上实时动态权限申请
文章转自:http://blog.csdn.net/qq_29988575/article/details/54909213 6.0以下手机正常,6.0以上的却崩溃 解决方法: targetSdkVe ...
- Hadoop(一) HADOOP简介
1. HADOOP背景介绍 1.1 什么是HADOOP HADOOP是apache旗下的一套开源软件平台 HADOOP提供的功能:利用服务器集群,根据用户的自定义业务逻辑,对海量数据进行分布式处理 H ...
- [Robot Framework] 调用ExcelLibrary
安装ExcelLibrary for Robot Framework 参考 : http://navinet.github.io/robotframework-excellibrary/ 打开wind ...
- InputMethodManagerService处理输入法——监听APK变动
android\frameworks\base\services\java\com\android\server\InputMethodManagerService.java public Input ...
- 【算法】DP解决旅行路径问题
问题描述 : After coding so many days,Mr Acmer wants to have a good rest.So travelling is the best choice ...
- 【Linux】 Ncures库的介绍与安装
Ncures库的介绍 ncurses(new curses)是一套编程库,它提供了一系列的函数以便使用者调用它们去生成基于文本的用户界面. ncurses名字中的n意味着“new”,因为它是curse ...
- FMS是什么?
- Firefox,chrome,IE上传图片预览
首先判断IE或是Firefox,chrome.本文只测试了IE8中和Firefox,chrome是不一样的. 判断是否IE: if(-[1,]){//判断浏览器不是IE //alert((-[1 ...