布局

  题目大意:有N头牛,编号1-N,按编号排成一排准备吃东西,有些牛的关系比较好,所以希望他们不超过一定的距离,也有一些牛的关系很不好,所以希望彼此之间要满足某个关系,牛可以挤在同一个位置上,现在给出N个牛的信息,问你能否实现一种排列方案,使得d[1]到d[N]最大?如果不存在输出-1,无限大输出-2

  这一题看上去挺难的,但是如果你知道差分约束原理,这一题似乎还是挺简单的。

  差分约束的原理是:存在任意线性方程,满足d[A]+c>=d[B],就可以表示为图的最短路形式,方程可以表示为A->B,权值为c的边,最后任意点之间距离之差的最大值,即为两点之间的最短距离。

  在最短路的算法中,恒有d[u]+w>=d[s](s是源点,w是权值,u是除了s的任意点),则d[u]-d[s]的最大值即为s-u的最短路经长。

  回到题目上来,那么这一题事实上蕴含了三个线性方程:

    1.d[i+1]>=d[i](按照编号排序)

    2.d[BL]-d[AL]<=DL->->->d[AL]+DL>=d[BL]

    3.d[BD]-D[AD]>=DD->->->d[BD]-DD>=d[AD]

    则我们只用把这些边表示出来算最短路径就可以了,这一题有负值边,用Bellman_Ford或者SPFA就可以了

  

Bellman_Ford:

 #include <iostream>
#include <functional>
#include <algorithm>
#define MAX 0x7f7f7f7f using namespace std; typedef int Positon;
typedef struct least_
{
Positon A;
Positon B;
int cost;
}Relation; static Relation L_Set[],M_Set[];
static int dist[]; void Bellman_Ford(const int, const int, const int); int main(void)
{
int cows_sum, ML, MD;
while (~scanf("%d%d%d", &cows_sum, &ML, &MD))
{
for (int i = ; i < ML; i++)
scanf("%d%d%d", &L_Set[i].A, &L_Set[i].B, &L_Set[i].cost);
for (int i = ; i < MD; i++)
scanf("%d%d%d", &M_Set[i].A, &M_Set[i].B, &M_Set[i].cost);
Bellman_Ford(cows_sum, ML, MD);
}
return ;
} void Bellman_Ford(const int cows_sum, const int ML, const int MD)
{
memset(dist, 0x7f, sizeof(dist));
dist[] = ;//到自己肯定是最短的 for (int i = ; i <= cows_sum; i++)
{
for (int i = ; i + <= cows_sum; i++)
if (dist[i + ] < MAX)//差分约束方程d[i+1]>=d[i]
dist[i] = min(dist[i], dist[i + ]);
for (int i = ; i < ML; i++)//差分约束方程d[AL]+DL>=d[BL]
if (dist[L_Set[i].A] < MAX)
dist[L_Set[i].B] = min(dist[L_Set[i].B], dist[L_Set[i].A] + L_Set[i].cost);
for (int i = ; i < MD; i++)//差分约束方程d[BD]-DD>=d[AD]
if (dist[M_Set[i].B] < MAX)
dist[M_Set[i].A] = min(dist[M_Set[i].A], dist[M_Set[i].B] - M_Set[i].cost);
} int ans = dist[cows_sum];
if (dist[] < )
printf("-1\n");
else if (dist[cows_sum] == MAX)
printf("-2\n");
else printf("%d\n", ans);
}

SPFA:

 #include <iostream>
#include <functional>
#include <algorithm>
#include <queue>
#define MAX 0x7f7f7f7f using namespace std; typedef int Position;
typedef struct least_
{
int next;
Position to;
int cost;
}Edge_Set; static Edge_Set edge[];//存边
static Position Head[];
static int dist[];
static int out[];//记录出去多少次
static bool used[];//记录是否在队内 void SPFA(const int, const int); int main(void)
{
int cows_sum, ML, MD, i, cost, edge_sum;
Position from, to;
while (~scanf("%d%d%d", &cows_sum, &ML, &MD))
{
memset(Head, -, sizeof(Head)); memset(dist, 0x7f, sizeof(dist)); memset(used, , sizeof(used)); memset(out, , sizeof(out));
edge_sum = ;
//读入邻接表
for (i = ; i < ML; i++)//d[BL]-d[AL]<=DL->->->d[AL]+DL>=d[BL]
{
scanf("%d%d%d", &from, &to, &cost);//因为编号是有序的,所以只用储存单向边就可以了
edge[edge_sum].next = Head[from]; edge[edge_sum].to = to; edge[edge_sum].cost = cost;
Head[from] = edge_sum++;
}
for (i = ; i < MD; i++)//d[BL]-d[AL]>=DL->->->d[BD]-DD>=d[AD]
{
scanf("%d%d%d", &from, &to, &cost);
edge[edge_sum].next = Head[to]; edge[edge_sum].to = from; edge[edge_sum].cost = -cost;
Head[to] = edge_sum++;
}
for (i = ; i + <= cows_sum; i++)//d[i+1]+0>=d[i]
{
edge[edge_sum].next = Head[i + ]; edge[edge_sum].to = i; edge[edge_sum].cost = ;
Head[i + ] = edge_sum++;
}
SPFA(cows_sum, edge_sum);
}
return ;
} void SPFA(const int cows_sum, const int edge_sum)//这次用STL玩玩
{
Position out_pos, to;
queue<Position>que;
que.push(); dist[] = ; used[] = ; while (!que.empty())
{
out_pos = que.front(); que.pop();
used[out_pos] = ;//出队了就标记为0
out[out_pos]++;
if (out[out_pos] > cows_sum)
{
printf("-1\n");
return;
}
for (int k = Head[out_pos]; k != -; k = edge[k].next)
{
to = edge[k].to;
if (dist[to] > dist[out_pos] + edge[k].cost)
{
dist[to] = dist[out_pos] + edge[k].cost;
if (!used[to])
{
used[to] = ;
que.push(to);
}
}
}
}
if (dist[cows_sum] == MAX)
printf("-2\n");
else
printf("%d\n", dist[cows_sum]);
}

ShortestPath:Layout(POJ 3169)(差分约束的应用)的更多相关文章

  1. POJ - 3169 差分约束

    题意:n头牛,按照编号从左到右排列,两头牛可能在一起,接着有一些关系表示第a头牛与第b头牛相隔最多与最少的距离,最后求出第一头牛与最后一头牛的最大距离是多少,如         果最大距离无限大则输出 ...

  2. POJ 3169 Layout (spfa+差分约束)

    题目链接:http://poj.org/problem?id=3169 题目大意:n头牛,按编号1~n从左往右排列,可以多头牛站在同一个点,给出ml行条件,每行三个数a b c表示dis[b]-dis ...

  3. POJ 3169 Layout (图论-差分约束)

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6574   Accepted: 3177 Descriptio ...

  4. poj 3159(差分约束经典题)

    题目链接:http://poj.org/problem?id=3159思路:题目意思很简单,都与给定的条件dist[b]-dist[a]<=c,求dist[n]-dist[1]的最大值,显然这是 ...

  5. [Usaco2005 dec]Layout 排队布局 差分约束

    填坑- 差分约束一般是搞一个不等式组,求xn-x1的最大最小值什么的,求最大值就转化成xa<=xb+w这样的,然后建图跑最短路(这才是最终约束的),举个例子 x1<=x0+2x2<= ...

  6. poj 1201 差分约束

    http://www.cnblogs.com/wangfang20/p/3196858.html 题意: 求集合Z中至少要包含多少个元素才能是每个区间[ai,bi]中的元素与Z中的元素重合个数为ci. ...

  7. BZOJ1731:[USACO]Layout 排队布局(差分约束)

    Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...

  8. bzoj 1731: [Usaco2005 dec]Layout 排队布局 ——差分约束

    Description 当排队等候喂食时,奶牛喜欢和它们的朋友站得靠近些.FJ有N(2<=N<=1000)头奶牛,编号从1到N,沿一条直线站着等候喂食.奶牛排在队伍中的顺序和它们的编号是相 ...

  9. bzoj 1731 [Usaco2005 dec]Layout 排队布局——差分约束

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1731 对差分约束理解更深.还发现美妙博客:http://www.cppblog.com/me ...

随机推荐

  1. 解决Oracle忘记密码问题

    在使用ORACLE的过程中,会出现各种各样的问题,各种各样的错误,其中ORA-12899就是前段时间我在将数据导入到我本地机器上的时候一直出现的问题.不过还好已经解决了这个问题,现在分享一下,解决方案 ...

  2. firefox与chrome中对select下拉框中的option支持问题

    firefox可以直接修改option的字体样式,但是chrome中option的字体样式是继承select的,这个是在项目中遇到的,具体的可以看一下 http://www.cnblogs.com/r ...

  3. JSTL标签库简介

    核心标签库 http://java.sun.com/jsp/jstl/core <c:catch>,<c:url>的使用 <!-- 捕获异常 --> <c:c ...

  4. 安装coreseek找不到mysql

    1.安装 coreseek-3.2.14 遇到问题:“ERROR: cannot find MySQL include files,随即在网上搜索各种答案说是要找到mysql.h的正确路径加入./co ...

  5. Deep Learning in a Nutshell: Core Concepts

    Deep Learning in a Nutshell: Core Concepts This post is the first in a series I’ll be writing for Pa ...

  6. python学习笔记之module && package

    个人总结: import module,module就是文件名,导入那个python文件 import package,package就是一个文件夹,导入的文件夹下有一个__init__.py的文件, ...

  7. 汇文Libsys图书管理系统全版本权限绕过+getshell

    由于一个很低级的代码错误,导致可以登录Libsys任意图书系统后台,并且由于代码未做过滤可直接getshell. 该图书管理系统的用户量很大,全国很大一部分院校都在使用此系统.经测试3.5-5.0版本 ...

  8. MySQL主从分离读写复制

    参考教程:http://www.yii-china.com/post/detail/283.html MySQL是开源的关系型数据库系统.复制(Replication)是从一台MySQL数据库服务器( ...

  9. WPF 数据绑定基础

    纯理论,可能会枯燥. .net 技术群: 199281001 ,欢迎加入. 1.目标对象一定是派生自DependencyObject的对象,并且目标属性必须是依赖属性,否则数据绑定操作将会失   败. ...

  10. 10GE---超长距离的万兆以太网

    万兆以太网 锁定 本词条由“科普中国”百科科学词条编写与应用工作项目 审核 . 以太网标准是一个古老而又充满活力的标准.自从1982年以太网协议被IEEE采纳成为标准以后,已经历了20年的风风雨雨.在 ...