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. 清除number输入框的上下箭头

    <input type="number"/> 在chrome,firefox,safari浏览器上输入框右侧会有上下箭头 方法1: <input type=&qu ...

  2. JS IIFE写法

    IIFE 博客分类: 前端开发   介绍IIFE IIFE的性能 使用IIFE的好处 IIFE最佳实践 jQuery优化 在Bootstrap源码(具体请看<Bootstrap源码解析>) ...

  3. Android Matrix类以及ColorMatri

    引自:http://www.chinabaike.com/t/37396/2014/0624/2556217.html Android Matrix类以及ColorMatrix类详解 最近在系统学习了 ...

  4. Android OpenGL ES(十一)绘制一个20面体 .

    前面介绍了OpenGL ES所有能够绘制的基本图形,点,线段和三角形.其它所有复杂的2D或3D图形都是由这些基本图形构成. 本例介绍如何使用三角形构造一个正20面体.一个正20面体,有12个顶点,20 ...

  5. CGRect相关工具函数

    NSStringFromCGRect(aCGRect): CGRectFromString(aString):如果把视图的框架以字符串的形式放在NSUserDefaults里面,那么该方法可以将其转回 ...

  6. HDU ACM 1495 非常可乐(广搜BFS)

    非常可乐 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissi ...

  7. IDL 实现求算 DEM 坡度坡向

    关于坡度坡向的定义,请Google之. 源码: IDL 源码PRO ASPECT_SLOPE,DEM,ASPECT = ASPECT,SLOPE=SLOPE,PIXELSIZE = PIXELSIZE ...

  8. 错误: error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. 的处理方法

  9. jq实现点击按钮后倒计时,多用于手机验证

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. postfix疯狂外发垃圾邮件

    分析 一.查找main.cf配置文件 localhost# find / -name main.cf /etc/postfix/main.cf 二.打开/etc/postfix/main.cf来看看. ...