Layout---poj3169(差分约束+最短路spfa)
题目链接:http://poj.org/problem?id=3169
有n头牛站成一排 在他们之间有一些牛的关系比较好,所以彼此之间的距离不超过一定距离;也有一些关系不好的牛,希望彼此之间的距离大于等于一定距离;
关系好的有ml个(A B D)表示A牛和B牛之间的距离<=D 关系不好的有md个(A,B,D)表示A牛和B牛之间的距离>=D,求在满足条件的排列中,求出牛1和牛n的最大距离;
如果距离可以是无限大输出-2,如果不存在这样的排列输出-1;
我们用d[i]表示第i头牛的位置,因为牛是按照编号排列顺序的,
所以 d[i]-d[i-1]>=0; --------- d[i-1]-d[i]<=0;(i到i-1的距离是0)
关系好的 d[B]-d[A] <= C; --------- d[B]-d[A]<=C;(A到B的距离是C)
关系不好的 d[B]-d[a]>=C; ----------- d[A]-d[B]<=-C;(B到A的距离是-C)
我们要求是的d[n]-d[1]<=x中的x;
刚好满足差分约束系统,所以直接建图求1-n的最短路即可;如果存在负环则不存在这样的序列,用spfa判断负环,如果距离为无穷大说明可以是任意值,
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <queue>
#include <stack>
#include <algorithm>
#include <map>
#include <string>
typedef long long LL;
#define INF 0x3f3f3f3f
#define met(a, b) memset(a, b, sizeof(a))
#define N 1500 using namespace std; int n, md, ml, G[N][N], vis[N], dist[N], cnt[N]; int spfa(int s)
{
for(int i=; i<=n; i++)
dist[i] = INF;
met(vis, );
met(cnt, );
queue<int>Q;
Q.push(s);
vis[s] = ;
dist[s] = ;
cnt[s]++;
while(!Q.empty())
{
int p = Q.front();Q.pop();
vis[p] = ;
cnt[p] ++;
for(int i=; i<=n; i++)
{
if(dist[i]>dist[p]+G[p][i])
{
dist[i] = dist[p]+G[p][i];
if(!vis[i])
{
vis[i] = ;
cnt[i]++;
if(cnt[i]>=n)
return -;
Q.push(i);
}
}
}
}
if(dist[n] == INF) return -;
return dist[n];
} int main()
{
while(scanf("%d %d %d", &n, &ml, &md) != EOF)
{
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
G[i][j] = INF;
G[i][i] = G[i][i-] = ;
}
int u, v, w;
for(int i=; i<=ml; i++)
{
scanf("%d %d %d", &u, &v, &w);
G[u][v] = w;
}
for(int i=; i<=md; i++)
{
scanf("%d %d %d", &u, &v, &w);
G[v][u] = -w;
} int ans = spfa();
printf("%d\n", ans);
}
return ;
}
Layout---poj3169(差分约束+最短路spfa)的更多相关文章
- POJ 3169 Layout 【差分约束】+【spfa】
<题目链接> 题目大意: 一些母牛按序号排成一条直线.有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离.如果没有最大距离输出-1,如果1.n之间距离任意就 ...
- POJ 3169 Layout(差分约束+最短路)题解
题意:有一串数字1~n,按顺序排序,给两种要求,一是给定u,v保证pos[v] - pos[u] <= w:二是给定u,v保证pos[v] - pos[u] >= w.求pos[n] - ...
- O - Layout(差分约束 + spfa)
O - Layout(差分约束 + spfa) Like everyone else, cows like to stand close to their friends when queuing f ...
- POJ3169:Layout(差分约束)
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15705 Accepted: 7551 题目链接:http ...
- poj 3169 Layout(差分约束+spfa)
题目链接:http://poj.org/problem?id=3169 题意:n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0.这些牛的距离存在着一些约束关系:1.有m ...
- [USACO2005][POJ3169]Layout(差分约束)
题目:http://poj.org/problem?id=3169 题意:给你一组不等式了,求满足的最小解 分析: 裸裸的差分约束. 总结一下差分约束: 1.“求最大值”:写成"<=& ...
- (简单) POJ 3169 Layout,差分约束+SPFA。
Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...
- POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)
原题地址:http://poj.org/problem?id=3159 题意大概是班长发糖果,班里面有不良风气,A希望B的糖果不比自己多C个.班长要满足小朋友的需求,而且要让自己的糖果比snoopy的 ...
- POJ 3167 Layout(差分约束)
题面 Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 ...
随机推荐
- HDU 1010 (DFS搜索+奇偶剪枝)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给定起点和终点,问刚好在t步时能否到达终点. 解题思路: 4个剪枝. ①dep&g ...
- Linux Mint 没有 language support 语言支持解决方案
打开新立得软件管理器在右边找到有关语言的安装后,language support就会出现
- java 实现数据结构之队列
队列是一种特殊的线性表,它只允许在表的前端(front)进行删除操作,只允许在表的后端(rear)进行插入操作. 1.队列的顺序存储结构及实现 public class SequenceQueue&l ...
- Python 处理理时间超详细转的
# -*- coding: utf-8 -*- import time def timestamp_datetime(value): format = '%Y-%m-%d %H:%M:%S' ...
- [简单]poi word2007表格按模版样式填充行数据
主要实现了按照模版行的样式填充数据,针对的是动态数据,静态数据可以直接替换变量实现,先说下缺点:1)暂未实现特殊样式填充(如列合并(跨行合并)),只能用于普通样式(如段落间距 缩进 字体 对齐)2)数 ...
- LINUX 2.6.18-238 local root exp
/* * * * 1-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=0 * 0 _ __ __ __ 1 * ...
- opengl绘制正弦曲线
利用opengl绘制正弦曲线 ,见代码: #include <windows.h> //#include <GLUT/glut.h> #include <GL/glut. ...
- lastLogon和lastLogonTimestamp的区别
如何得到用户最近一次登陆域的时间?在Windows2003域中有2个属性:lastLogon和lastLogonTimestamp,那么这2个属性到底有什么作用呢? lastLogon属性实时更新用户 ...
- NOJ 1072 The longest same color grid(尺取)
Problem 1072: The longest same color grid Time Limits: 1000 MS Memory Limits: 65536 KB 64-bit in ...
- NVlink
Nvidia's Pascal to use stacked memory, proprietary NVLink interconnect by Scott Wasson — 6:50 PM on ...