POJ Layout
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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
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 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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#define INF 9999999 int dis[1010];
int cnt;
int n, ml, md; struct N
{
int u;
int v;
int w;
}s[200005]; void add(int u, int v, int w )
{
s[cnt].u=u;
s[cnt].v=v;
s[cnt++].w=w;
} void bellman_ford()
{
int i, j;
for(i=1; i<=n; i++)
dis[i]=INF;
dis[1]=0; for(i=2; i<=n; i++ )
{
int flag=0;
for(j=0; j<cnt; j++ ) //检查每条边
{
if( dis[s[j].v] > dis[s[j].u] + s[j].w )
{
dis[s[j].v] = dis[s[j].u]+s[j].w ;
flag=1;
}
}
if(flag==0)
break;
}
for(i=0; i<cnt; i++)
{
if(dis[s[i].v] > dis[s[i].u]+s[i].w )
break;
}
if(i<cnt)
printf("-1\n");
else
{
if( dis[n]==INF )
printf("-2\n");
else
printf("%d\n", dis[n] );
}
} int main()
{
int i, j;
int u, v, w;
while(scanf("%d %d %d", &n, &ml, &md)!=EOF)
{
cnt=0;
for(i=0; i<ml; i++)
{
scanf("%d %d %d", &u, &v, &w ); //亲密的牛 最大距离
if(u>v)
{
u=u^v; v=v^u; u=u^v; //还可以这样写: u^=v^=u^=v ;
}
add(u, v, w);
}
for(j=0; j<md; j++)
{
scanf("%d %d %d", &u, &v, &w ); //排斥的牛 最小距离
if(u<v)
{
u=u^v; v=v^u; u=u^v; // u^=v^=u^=v ;
}
add(u, v, -w);
}
bellman_ford();
}
return 0;
}
POJ Layout的更多相关文章
- poj Layout 差分约束+SPFA
题目链接:http://poj.org/problem?id=3169 很好的差分约束入门题目,自己刚看时学呢 代码: #include<iostream> #include<cst ...
- 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: 11612 Accepted: 5550 Descripti ...
- poj 3169 Layout 差分约束模板题
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6415 Accepted: 3098 Descriptio ...
- POJ 3169 Layout (差分约束系统)
Layout 题目链接: Rhttp://acm.hust.edu.cn/vjudge/contest/122685#problem/S Description Like everyone else, ...
- POJ 3169 Layout (spfa+差分约束)
题目链接:http://poj.org/problem?id=3169 差分约束的解释:http://www.cnblogs.com/void/archive/2011/08/26/2153928.h ...
- poj 3169 Layout
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8610 Accepted: 4147 Descriptio ...
- POJ 3169 Layout (图论-差分约束)
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6574 Accepted: 3177 Descriptio ...
- POJ 3167 Layout(差分约束)
题面 Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 ...
随机推荐
- C程序设计Week12晚上练习
本周仅仅进行一个程序,曾经的一个程序. 自己定义例如以下函数,输入n(n<46)个学生的姓名和成绩,顺序输出这n个学生的姓名和成绩,并输出最高成绩的姓名和成绩.预习struct结构体,思考怎样改 ...
- 配置YUM源更新源
在虚拟机上更新yum,总是告诉我找不懂镜像服务器地址,配置失败,连 #yum install yum 它自己都无法更新. 于是决定先配置一下yum的更新源. 首先找到yum的主要配置文件, 一般在/e ...
- 细说Redirect重定向请求(情节分享)
前些日子在开发公司项目接口的时候,由于需要与第三方平台对接,由于接口之前的层层封装,不断的需要转发,把人差点搞糊涂了.本来以为之前对Redirect的认识足够清楚,可是到实际开发之前我还是没 ...
- 还在用系统自带的?那你那就OUT了!
相信如今的APP10个里面有九个是有Tabbar的,可是非常多人甚是非常多公司都在用系统自带的tabbar.当然这也不是不能够,并且项目中就那几行代码.效果又一样. 可是,别忘了另一个可是.然并卵.这 ...
- MySQL中in(常量列表)的执行计划
我们在写sql的时候,经常用到in,in后面跟一堆常量列表,如id.有人说in的效率很高,而有人说很低:有人说in能使用索引,还有人说in不能使用索引... 到底是一个怎样的情况呢?我们分析以下几种情 ...
- quartus2 13.0+modelsim联合开发环境搭建(win10)
quartus2用于硬件设计代码的综合,检查是否有语法错误:modelsim用于对硬件设计代码进行仿真,观察波形是否与需求一致,需要编写xxx_tb.v才能仿真 一.quartus2安装见这篇文章ht ...
- springmvc上传方法
/** * * @param file 上传的文件 * @param filePath 上传到那个目录 * @return 上传后的文件名字 * @throws IOException */ publ ...
- UFLDL深度学习笔记 (五)自编码线性解码器
UFLDL深度学习笔记 (五)自编码线性解码器 1. 基本问题 在第一篇 UFLDL深度学习笔记 (一)基本知识与稀疏自编码中讨论了激活函数为\(sigmoid\)函数的系数自编码网络,本文要讨论&q ...
- 谷歌宣称web组件才是web开发的未来
谷歌宣称web组件才是web开发的未来 虽然今年的谷歌I/O大会没有出现像去年谷歌眼镜发布时直播疯狂跳伞这样的活动,但是上周仍然有不少产品推出.谷歌宣布对谷歌地图.搜索.安卓,以及其他 很多产品做出更 ...
- 一定要搜藏的20个非常有用的PHP类库
一定要搜藏的20个非常有用的PHP类库 本文提供了20个非常有用的PHP类库的名称和下载地址.这20个PHP类库包含了图标库,RSS解析,缩略图生成,支付,OpenID,数据库抽象,PDF生成器等一系 ...