(简单) POJ 3169 Layout,差分约束+SPFA。
Description
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.
代码如下:
#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。的更多相关文章
- POJ 3169 Layout(差分约束+链式前向星+SPFA)
描述 Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 ...
- POJ 3169 Layout (差分约束)
题意:给定一些母牛,要求一个排列,有的母牛距离不能超过w,有的距离不能小于w,问你第一个和第n个最远距离是多少. 析:以前只是听说过个算法,从来没用过,差分约束. 对于第 i 个母牛和第 i+1 个, ...
- POJ 3169 Layout(差分约束啊)
题目链接:http://poj.org/problem? id=3169 Description Like everyone else, cows like to stand close to the ...
- poj 3169 Layout 差分约束模板题
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6415 Accepted: 3098 Descriptio ...
- POJ 3169 Layout(差分约束 线性差分约束)
题意: 有N头牛, 有以下关系: (1)A牛与B牛相距不能大于k (2)A牛与B牛相距不能小于k (3)第i+1头牛必须在第i头牛前面 给出若干对关系(1),(2) 求出第N头牛与第一头牛的最长可能距 ...
- poj Layout 差分约束+SPFA
题目链接:http://poj.org/problem?id=3169 很好的差分约束入门题目,自己刚看时学呢 代码: #include<iostream> #include<cst ...
- ShortestPath:Layout(POJ 3169)(差分约束的应用)
布局 题目大意:有N头牛,编号1-N,按编号排成一排准备吃东西,有些牛的关系比较好,所以希望他们不超过一定的距离,也有一些牛的关系很不好,所以希望彼此之间要满足某个关系,牛可以 ...
- POJ-3169 Layout (差分约束+SPFA)
POJ-3169 Layout:http://poj.org/problem?id=3169 参考:https://blog.csdn.net/islittlehappy/article/detail ...
- poj 3169&hdu3592(差分约束)
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9687 Accepted: 4647 Descriptio ...
随机推荐
- prototype小解
prototype由来 在理解prototype前,首先得理解js面向对象编程的私有变量.私有函数,静态变量.静态函数,以及实例变量,实例函数 私有变量,私有函数 函数内部通过var定义的变量 fun ...
- C++随机崩溃捕捉处理
1. 会引起异常的几个原因(主要记录目前遇到过的几个问题) 程序读取了无效的内存地址 堆栈的溢出,比如无限循环导致那段内存溢出,比如把size为20的缓存拷贝到size为10的缓存块等 无法申请到有效 ...
- 青蛙的约会<数论,extgcd>
青蛙的约会 题意: 在一个圆上有一个零刻度点,公青蛙和母青蛙分别在A点和B点<不同的位 置>,他们每秒行走的距离分别是m和n,圆的周长是L.问题是这两个青 蛙能不能相遇,若能在什么时候相遇 ...
- PullToRefreshScrollView的上拉加载、下拉刷新
eclipse中的项目: //注意:此刷新功能是使用的第三方的PullToRefreshScrollView,因此需要导入第三方library作为依赖 步骤:导入第三方library,依赖:点击你的应 ...
- java 继承与多态
Example5_11.java class 动物 { void cry() { } } class 狗 extends 动物 { void cry() { System.out.println(&q ...
- Java 垃圾回收机制学习
原文链接: http://blog.csdn.net/zsuguangh/article/details/6429592 自己学习总结: 1c++和java的内存使用的区别: 在C++中,对象所占的内 ...
- 如何获取url访问历史记录
在院里的群里,有人问了这么一个问题: A页面提交表单到B页面,然后在B页面点了后退,如果在A页面上判断是直接访问的还是后退进去的呢?我不想改B页面. 于是乎本着热心人的想法,我就帮他搞了搞,首先我想到 ...
- python中uuid来生成机器唯一标识
摘要: 我们可以使用uuid1的后16位来标识一个机器. # use machine specific uuid, last 16 char will be the same if machine ...
- ASP.NET Cache 类
在查找资料的过程中.原来园子里面已经有过分析了.nopCommerce架构分析系列(二)数据Cache. 接下来是一些学习补充. 1.Nop中没有System.Web.Caching.Cache的实现 ...
- 卸载get-apt安装的软件
我们都知道安装软件最简单的方法是apt-get install,但是卸载就不常用了,如何卸载呢? sudo apt-get remove android-tools-adb