A - Layout

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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 1
1 3 10
2 4 20
2 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.

 
 
算法分析:
      不知道为什么用SPFA 会超时,改成bellman_ford 算法了就行了,并且需要注意 差分约束 建图时 两个点是否眼交换!
   二题目里却说是有大小顺序的,但是不交换顺序就错了!(我用位运算交换顺序,据说会节省时间,但是刘汝佳的树立却说不建议这样写,不知道为什    么?)
 
 
 
Accepted:
 
#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的更多相关文章

  1. poj Layout 差分约束+SPFA

    题目链接:http://poj.org/problem?id=3169 很好的差分约束入门题目,自己刚看时学呢 代码: #include<iostream> #include<cst ...

  2. POJ 3169 Layout(差分约束啊)

    题目链接:http://poj.org/problem? id=3169 Description Like everyone else, cows like to stand close to the ...

  3. POJ 3169.Layout 最短路

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11612   Accepted: 5550 Descripti ...

  4. poj 3169 Layout 差分约束模板题

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6415   Accepted: 3098 Descriptio ...

  5. POJ 3169 Layout (差分约束系统)

    Layout 题目链接: Rhttp://acm.hust.edu.cn/vjudge/contest/122685#problem/S Description Like everyone else, ...

  6. POJ 3169 Layout (spfa+差分约束)

    题目链接:http://poj.org/problem?id=3169 差分约束的解释:http://www.cnblogs.com/void/archive/2011/08/26/2153928.h ...

  7. poj 3169 Layout

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8610   Accepted: 4147 Descriptio ...

  8. POJ 3169 Layout (图论-差分约束)

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6574   Accepted: 3177 Descriptio ...

  9. POJ 3167 Layout(差分约束)

    题面 Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 ...

随机推荐

  1. HTMLTestRunner 异常输出中文乱码

    1.在代码中加入下面的代码并保存: # -.- coding:utf-8 -.- import sys reload(sys) sys.setdefaultencoding('utf-8') 2.找到 ...

  2. the reactor pattern and java nio

    在<java NIO>作者PPT<How to Build a Scalable Multiplexed Server With NIO> 和 Doug Lea <Sca ...

  3. SpringBoot使用MyBatis报错:Error invoking SqlProvider method (tk.mybatis.mapper.provider.base.BaseInsertProvider.dynamicSQL)

    © 版权声明:本文为博主原创文章,转载请注明出处  1. 错误描述 使用SpringBoot集成MyBatis框架,并且使用 mapper-spring-boot-starter 自动生成MyBati ...

  4. Linux 下安装PHPunit

    PHP 档案包 (PHAR)  要获取 PHPUnit,最简单的方法是下载 PHPUnit 的 PHP 档案包 (PHAR),它将 PHPUnit 所需要的所有必要组件(以及某些可选组件)捆绑在单个文 ...

  5. hdu 3172 Virtual Friends(并查集,字典树)

    题意:人与人交友构成关系网,两个人交友,相当于两个朋友圈的合并,问每个出两人,他们目前所在的关系网中的人数. 分析:用并查集,其实就是求每个集合当前的人数.对于人名的处理用到了字典树. 注意:1.题目 ...

  6. oracle中过滤中文字符或者汉字的函数

    CREATE OR REPLACE FUNCTION GET_CHINESE(P_NAME IN VARCHAR2) RETURN VARCHAR2 IS V_CODE        VARCHAR2 ...

  7. 最小生成树——Prim(普利姆)算法

    [0]README 0.1) 本文总结于 数据结构与算法分析, 源代码均为原创, 旨在 理解Prim算法的idea 并用 源代码加以实现: 0.2)最小生成树的基础知识,参见 http://blog. ...

  8. Office365client通过本地方式批量部署(即点即用部署)

    当企业用户拥有Office 365 ProPlus的许可后,可登陆Office 365.自行下载Officeclient安装部署 以上仅仅是理想情况,实际情况是企业用户较多,IT水平參差不齐,企业的带 ...

  9. ios 深入讲解iOS键盘一:控制键盘隐藏显示

    在iOS的开发中,我们一般使用UITextField.UITextView处理文字输入等操作,大部分情况下我们只需要一两行代码去手动管理键盘的显示隐藏:让UITextField或UITextView成 ...

  10. PHP-Manual的学习----【语言参考】----【类型】-----【Resource 资源类型】

    2017年8月24日11:29:361.资源 resource 是一种特殊变量,保存了到外部资源的一个引用.资源是通过专门的函数来建立和使用的.2.由于资源类型变量保存有为打开文件.数据库连接.图形画 ...