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.

 
  题意就是 Xi-Xj<c 然后求 Xn-X1 的最大值,差分约束问题。。。
  建图然后SPFA 就好。。。
 

代码如下:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> using namespace std; const int MaxN=;
const int MaxM=;
const int INF=; struct Edge
{
int to,next,cost;
}; Edge E[MaxM];
int head[MaxN],Ecou; bool vis[MaxN];
int couNode[MaxN]; void init(int N)
{
Ecou=;
for(int i=;i<=N;++i)
{
head[i]=-;
couNode[i]=;
vis[i]=;
}
} void addEdge(int u,int v,int w)
{
E[Ecou].to=v;
E[Ecou].cost=w;
E[Ecou].next=head[u];
head[u]=Ecou++;
} bool SPFA(int lowcost[],int N,int start)
{
int t,v;
queue <int> que; for(int i=;i<=N;++i)
lowcost[i]=INF;
lowcost[start]=; que.push(start);
couNode[start]=;
vis[start]=; while(!que.empty())
{
t=que.front();
que.pop(); vis[t]=; for(int i=head[t];i!=-;i=E[i].next)
{
v=E[i].to; if(lowcost[v]>lowcost[t]+E[i].cost)
{
lowcost[v]=lowcost[t]+E[i].cost; if(!vis[v])
{
vis[v]=;
couNode[v]+=;
que.push(v); if(couNode[v]>N)
return ;
}
}
}
} return ;
} int ans[MaxN]; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int N,ML,MD;
int a,b,c; scanf("%d %d %d",&N,&ML,&MD); init(N); for(int i=;i<=ML;++i)
{
scanf("%d %d %d",&a,&b,&c); addEdge(a,b,c);
} for(int i=;i<=MD;++i)
{
scanf("%d %d %d",&a,&b,&c); addEdge(b,a,-c);
} for(int i=;i<=N-;++i)
addEdge(i+,i,); if(!SPFA(ans,N,))
printf("-1\n");
else if(ans[N]!=INF)
printf("%d\n",ans[N]);
else
printf("-2\n"); return ;
}

(简单) POJ 3169 Layout,差分约束+SPFA。的更多相关文章

  1. POJ 3169 Layout(差分约束+链式前向星+SPFA)

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

  2. POJ 3169 Layout (差分约束)

    题意:给定一些母牛,要求一个排列,有的母牛距离不能超过w,有的距离不能小于w,问你第一个和第n个最远距离是多少. 析:以前只是听说过个算法,从来没用过,差分约束. 对于第 i 个母牛和第 i+1 个, ...

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

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

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

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

  5. POJ 3169 Layout(差分约束 线性差分约束)

    题意: 有N头牛, 有以下关系: (1)A牛与B牛相距不能大于k (2)A牛与B牛相距不能小于k (3)第i+1头牛必须在第i头牛前面 给出若干对关系(1),(2) 求出第N头牛与第一头牛的最长可能距 ...

  6. poj Layout 差分约束+SPFA

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

  7. ShortestPath:Layout(POJ 3169)(差分约束的应用)

                布局 题目大意:有N头牛,编号1-N,按编号排成一排准备吃东西,有些牛的关系比较好,所以希望他们不超过一定的距离,也有一些牛的关系很不好,所以希望彼此之间要满足某个关系,牛可以 ...

  8. POJ-3169 Layout (差分约束+SPFA)

    POJ-3169 Layout:http://poj.org/problem?id=3169 参考:https://blog.csdn.net/islittlehappy/article/detail ...

  9. poj 3169&hdu3592(差分约束)

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9687   Accepted: 4647 Descriptio ...

随机推荐

  1. 关于tag,viewWithTag

    iOS SDK内置了一套搜寻机制,可通过tag来查找子视图. **苹果公司很少给子视图设置tag.笔者所知范围的唯一例外出现在UIAlertView中,该类会给按钮分别设置值为1.2的标签 viewW ...

  2. java 网络编程Socket编程

    Server.java import java.io.*; import java.net.*; public class Server { public static void main(Strin ...

  3. Node.js学习 - Web Server

    Client - 客户端,一般指浏览器,浏览器可以通过 HTTP 协议向服务器请求数据. Server - 服务端,一般指 Web 服务器,可以接收客户端请求,并向客户端发送响应数据. Busines ...

  4. linux 命令实现原理

    我们知道有些Linux的命令涉及到一些高效率的算法,在此做出一个积累吧,不是系统的. 1.tail命令打印一个文件的最后num行 2.grep命令从文本中匹配字符串 基于正则表达式的匹配很快. it ...

  5. JSP内置对象--pageContext对象(非常重要!!!)

    pageContext对象是javax.servlet.jsp.PageContext类的实例,只要表示的是一个jsp页面的上下文,而且功能强大,几乎可以操作各种内置对象. >forward(S ...

  6. JSP标准标签库(JSTL)--SQL标签库 sql

    了解即可.SQL标签库 No. 功能分类 标签名称 描述 1 数据源标签 <sql:setDataSource> 设置要使用的数据源名称 2 数据库操作标签 <sql:query&g ...

  7. ERROR: No pool defined. at least one pool section must be specified in config file

    root@ubuntu:/opt/php7# /opt/php7/sbin/php-fpm [22-Sep-2015 14:29:00] WARNING: Nothing matches the in ...

  8. codeforces 369 div2 C dp

    http://codeforces.com/contest/711 C. Coloring Trees time limit per test 2 seconds memory limit per t ...

  9. tps,qps

    http://blog.itpub.net/22664653/viewspace-767265/

  10. hrbust 1721 A + B = 0 map的应用

    13级春季校赛的热身题,但优化后我的代码也超时了,后来看了看学长的解法,觉得最简单的还是map,再一次感受到了map的强大. 题目描述如下 Description There is an intege ...