ShortestPath:Layout(POJ 3169)(差分约束的应用)

题目大意:有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)(差分约束的应用)的更多相关文章
- POJ - 3169 差分约束
题意:n头牛,按照编号从左到右排列,两头牛可能在一起,接着有一些关系表示第a头牛与第b头牛相隔最多与最少的距离,最后求出第一头牛与最后一头牛的最大距离是多少,如 果最大距离无限大则输出 ...
- POJ 3169 Layout (spfa+差分约束)
题目链接:http://poj.org/problem?id=3169 题目大意:n头牛,按编号1~n从左往右排列,可以多头牛站在同一个点,给出ml行条件,每行三个数a b c表示dis[b]-dis ...
- POJ 3169 Layout (图论-差分约束)
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6574 Accepted: 3177 Descriptio ...
- poj 3159(差分约束经典题)
题目链接:http://poj.org/problem?id=3159思路:题目意思很简单,都与给定的条件dist[b]-dist[a]<=c,求dist[n]-dist[1]的最大值,显然这是 ...
- [Usaco2005 dec]Layout 排队布局 差分约束
填坑- 差分约束一般是搞一个不等式组,求xn-x1的最大最小值什么的,求最大值就转化成xa<=xb+w这样的,然后建图跑最短路(这才是最终约束的),举个例子 x1<=x0+2x2<= ...
- poj 1201 差分约束
http://www.cnblogs.com/wangfang20/p/3196858.html 题意: 求集合Z中至少要包含多少个元素才能是每个区间[ai,bi]中的元素与Z中的元素重合个数为ci. ...
- BZOJ1731:[USACO]Layout 排队布局(差分约束)
Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...
- bzoj 1731: [Usaco2005 dec]Layout 排队布局 ——差分约束
Description 当排队等候喂食时,奶牛喜欢和它们的朋友站得靠近些.FJ有N(2<=N<=1000)头奶牛,编号从1到N,沿一条直线站着等候喂食.奶牛排在队伍中的顺序和它们的编号是相 ...
- bzoj 1731 [Usaco2005 dec]Layout 排队布局——差分约束
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1731 对差分约束理解更深.还发现美妙博客:http://www.cppblog.com/me ...
随机推荐
- mysql中的having
from子句后面可以用where选择行,group by子句后面可以用having子句选择行, having中条件的定义和where中很相似,但是having中可以直接用聚合函数,但是where中不能 ...
- jstl是自动就有的吗,不是的Unknown tag (c:if).
这个错误的原因就是没有导包 http://www.runoob.com/jsp/jsp-jstl.html 这个网站有方法
- USACO 3.3 fence 欧拉回路
题意:求给定图的欧拉回路(每条边只走一次) 若欧拉回路存在,图中只可能有0个or2个奇数度的点. 求解时,若有奇数度的点,则必须从该点开始.否则可以从任一点开始 求解过程:dfs //主程序部分 # ...
- mybatis-spring从1.1升级到1.2所带来的dao层级的编写问题
我们公司的项目使用spring+mybatis组合.所以就必须得使用mybatis-spring了.所以此处就昨日mybatis-spring从1.1升级到1.2所带来的dao层级的编写问题,做了一个 ...
- c++ 函数调用在进入下一个循环的时候会再次初始化参数,将函数体直接写进去就正常
#include"stdafx.h" #include"string" #include<iostream> #include<vector& ...
- 千万不要误用 java 中的 HashCode 方法
刚才debug追堆栈的时候发现一个很奇怪的问题 我用IE8和Google的浏览器访问同一个地址 Action的 scope="session" 也设置了 而且两个浏览器提交的参数m ...
- Struts2拦截器Interceptor执行顺序理解
invocation.invoke()方法是拦截器框架的实现核心,通过确定invocation.invoke()方法执行位置,来实现Action执行前后处理操作,在invocation.invoke( ...
- java中request,application,session三个域及参数简单示例
直接上代码: java代码: public class HelloAction implements Action { @Override public String execute() throws ...
- mono 3.4.0 make install的时候出现"找不到 Microsoft.Portable.Common.targets 文件”的错误提示解决方法
如果在这时就进行配置安装Mono的话,会在make阶段得到一个“找不到 Microsoft.Portable.Common.targets 文件”的错误提示, 所以需要先进行如下处理: #> c ...
- 新浪微博客户端(31)-显示相册图片上的GIF标记
DJStatusPhotoView.h #import <UIKit/UIKit.h> @class DJPhoto; @interface DJStatusPhotoView : UII ...