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. Ubuntu下编译Hello World驱动并运行全过程

    一般内核驱动都是在实体机上跑的,那有没有方法在ubuntu直接编译并运行呢?带着这个问题在网上查了一些资料,之后就实现了. 运行 hello.c   #include<linux/init.h& ...

  2. Java中HashTable和HashMap的区别

    在Java中,HashTable和HashMap都是哈希表,那么它们有什么区别呢?   1.它们所继承的类不一样. HashTable和HashMap都实现了Map接口,但是它们所继承的类时不同的.H ...

  3. bootstrap input 加了 disabled 后台竟然接受不到值

    下午做了一个input  值需要不可改变.在input属性中加入了  disabled .在后台接受  var_dump($_POST); 竟然看不到值. <input type="t ...

  4. PHP -- 问题

    @.源码安装,最开始,自己通过 ./config  make  make install三步,啥参数也没加,安装好之后,发现/usr/local/php下就一个man文件夹,死都没找到php-fpm之 ...

  5. UFLDL深度学习笔记 (五)自编码线性解码器

    UFLDL深度学习笔记 (五)自编码线性解码器 1. 基本问题 在第一篇 UFLDL深度学习笔记 (一)基本知识与稀疏自编码中讨论了激活函数为\(sigmoid\)函数的系数自编码网络,本文要讨论&q ...

  6. 表转List泛型数组

    转换那块怕忘记,留存一下 using System; using System.Collections.Generic; using System.Data; using System.Linq; u ...

  7. 转载 iOS全局检测网络变化的实时状态

      昨天浏览了cocoaChina,发现了一遍文章是优化Reachablity框架的出来的检测网络类,大家都知道这个Reachablity框架是用来检测网络变化的!但是也是有一点bug,事实上,基于此 ...

  8. 九度OJ 1195:最长&最短文本 (搜索)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3144 解决:1156 题目描述: 输入多行字符串,请按照原文本中的顺序输出其中最短和最长的字符串,如果最短和最长的字符串不止一个,请全部输 ...

  9. every row of W is a classifier for one of the classes

    every row of W is a classifier for one of the classes As we saw above, every row of W is a classifie ...

  10. Python菜鸟之路:Python基础——函数

    一.函数 1. 简介 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段.函数能提高应用的模块性,和代码的重复利用率. 2. 组成 函数代码块以 def 关键词开头,后接函数名和圆括号( ...