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. MySql-时间格式转换之转换为时分秒格式的日期

    select date_format(create_datetime,'%Y-%m-%d %k:%i:%s') from busi_repairitem_category MySQL毫秒值和日期的指定 ...

  2. cmd实用指令

    具体可参考:http://www.jb51.net/os/windows/36986.html 以下是本人总结的一些比较实用的指令,仅仅只是自己的实战笔记 f: 进入F盘 同理 c: 进入C盘 cd ...

  3. Linux下安装php开发框架yaf

    yaf框架中文手册:http://yaf.laruence.com/manual/index.html yaf手册:http://www.php.net/manual/en/book.yaf.php ...

  4. java应用测试报告生成(二):利用ant的build.xml生成测试报告

    1.将写好的项目导出 在工程下会生成一个build.xml的蚂蚁图标的文件. 2.右击该文件,选择run as Ant build 其中的测试目录是可以选择的,如果涉及到顺序也可以调整顺序 3.执行后 ...

  5. Co-Debugging JNI with Android Studio and Visual Studio

    Tutorials > Android > Integration with other tools > Co-Debugging JNI with Android Studio a ...

  6. building system busy, pls wait !!

    编译ca是可能会报这个错误,是189服务器上的/home/pub-work/.android_build_lock这个文件的问题,删除即可.

  7. 2--OC -- 类的创建与实例化

    2.OC -- 类的创建与实例化   一.OC类的简述 1.OC类分为2个文件:.h文件用于类的声明,.m文件用于实现.h的函数: 2.类是声明使用关键字:@interface.@end : 3.类是 ...

  8. DHCP详细工作过程(转)

    DHCP客户端通过和DHCP服务器的交互通讯以获得IP地址租约.为了从DHCP服务器获得一个IP地址,在标准情况下DHCP客户端和DHCP服务器之间会进行四次通讯.DHCP协议通讯使用端口UDP 67 ...

  9. ZOJ 2866 Overstaffed Company

    树状数组 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...

  10. Magento首页显示产品

    Magento首页显示产品     经常用的比较琐碎,上官网发现一个稍微全一点的不过没有针对 具体使用过程中遇到的情况进行修正  这边只做一个备忘吧   (细节问题 按个别情况进行修改即可) New  ...