POJ 3169 Layout(差分约束+最短路)题解
题意:有一串数字1~n,按顺序排序,给两种要求,一是给定u,v保证pos[v] - pos[u] <= w;二是给定u,v保证pos[v] - pos[u] >= w。求pos[n] - pos[1]最大,若无解输出-1,无穷多解输出-2。
思路:光看题目好像和最短路无关,其实这里用到了spfa的松弛操作来保证所给出的两种要求。若pos[v] - pos[u] >= w,则pos[v] +(- w) >= pos[u],也就是pos[v] +(- w) < pos[u]时进行松弛,建一条边v->u,权值-w,这就和spfa中的那一步对应上了,于是转化为了最短路。另一种条件也是如此操作。无解的情况应为出现了负环;无穷多解的情况为1和n没有条件约束,也就是1没有路通向n。
参考:
代码:
#include<cstdio>
#include<set>
#include<cmath>
#include<stack>
#include<vector>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn = +;
const int INF = 0x3f3f3f3f;
struct Edge{
int v,cost;
Edge(int _v = ,int _cost = ):v(_v),cost(_cost){}
};
vector<Edge> G[maxn];
bool vis[maxn];
int cnt[maxn];
int dist[maxn];
void addEdge(int u,int v,int cost){
G[u].push_back(Edge(v,cost));
}
bool spfa(int st,int n){
memset(vis,false,sizeof(vis));
memset(dist,INF,sizeof(dist));
vis[st] = true;
dist[st] = ;
queue<int> q;
while(!q.empty()) q.pop();
q.push(st);
memset(cnt,,sizeof(cnt));
cnt[st] = ;
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int i = ;i < G[u].size();i++){
int v = G[u][i].v;
if(dist[v] > dist[u] + G[u][i].cost){
dist[v] = dist[u] + G[u][i].cost;
if(!vis[v]){
vis[v] = true;
q.push(v);
if(++cnt[v] > n) return false;
}
}
}
}
return true;
}
int main(){
int n,ml,md;
scanf("%d%d%d",&n,&ml,&md);
for(int i = ;i <= n;i++) G[i].clear();
for(int i = ;i <= ml;i++){ //at most -> v - u <= w -> v <= w + u
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addEdge(u,v,w);
}
for(int i = ;i <= md;i++){ //at least -> v - u >= w -> u <= -w + v
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addEdge(v,u,-w);
}
bool cannot = spfa(,n);
if(!cannot){
printf("-1\n");
}
else{
if(dist[n] == INF){
printf("-2\n");
}
else{
printf("%d\n",dist[n]);
}
}
return ;
}
POJ 3169 Layout(差分约束+最短路)题解的更多相关文章
- 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(差分约束 线性差分约束)
题意: 有N头牛, 有以下关系: (1)A牛与B牛相距不能大于k (2)A牛与B牛相距不能小于k (3)第i+1头牛必须在第i头牛前面 给出若干对关系(1),(2) 求出第N头牛与第一头牛的最长可能距 ...
- poj 3169 Layout 差分约束模板题
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6415 Accepted: 3098 Descriptio ...
- ShortestPath:Layout(POJ 3169)(差分约束的应用)
布局 题目大意:有N头牛,编号1-N,按编号排成一排准备吃东西,有些牛的关系比较好,所以希望他们不超过一定的距离,也有一些牛的关系很不好,所以希望彼此之间要满足某个关系,牛可以 ...
- poj 3169&hdu3592(差分约束)
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9687 Accepted: 4647 Descriptio ...
- Bellman-Ford算法:POJ No.3169 Layout 差分约束
#define _CRT_SECURE_NO_WARNINGS /* 4 2 1 1 3 10 2 4 20 2 3 3 */ #include <iostream> #include & ...
- POJ 3169 Layout 差分约束系统
介绍下差分约束系统:就是多个2未知数不等式形如(a-b<=k)的形式 问你有没有解,或者求两个未知数的最大差或者最小差 转化为最短路(或最长路) 1:求最小差的时候,不等式转化为b-a>= ...
随机推荐
- 页面链接跳转历史URL不记录的兼容处理
1.阻止跳转a标签的链接 2.location.replace(href) 不生成新的历史记录, 但有bug 3.首先通过HTML5 history.replaceState()方法把当前URL地址替 ...
- {sharepoint} More on SharePoint 2010 Application Pools
More on SharePoint 2010 Application Pools Print | posted on Friday, December 04, 2009 3:26 PM Blimey ...
- linux下nproc的作用
文章来源: http://blog.csdn.net/odailidong/article/details/50561257 nproc是操作系统级别对每个用户创建的进程数的限制,在Linux下运行多 ...
- windows系统常用软件及配置介绍
常用工具 ,,,, 开发工具 ,,, 快捷键 ... 等等 vvv 等等
- SMGP关键代码
从网上下载java的API就可以开发了我们需要修改的类是: import java.io.IOException; import cn.com.zjtelecom.smgp.Client; impor ...
- OC开发_代码片段——代码编写自定义的tableViewCell
一.介绍 之前已经实现过通过简单的XIB文件来自定义我们的tableViewCell,包括每一步的步骤和代码:http://www.cnblogs.com/daomul/p/4355999.html ...
- DetailView内匿名函数不可用
DetailView yii\widgets\DetailView 小部件显示的是单一 yii\widgets\DetailView::$model 数据的详情. 它非常适合用常规格式显示一个模型(例 ...
- freemarker 判断写法
1.if条件写法:如果data非空则输出:test<#if data?? >test</#if> 2.为空则输出 <#if !(data??) > test < ...
- toml-lang - Tom's Obvious, Minimal Language
Tom's Obvious, Minimal Languagehttps://github.com/toml-lang/toml
- centos 特殊权限 各种搜索命令 lsattr ,chattr,suid,sgid,sbit,file,type是否是内置命令,stat文件属性 ,whereis,locate,find,ln 内部命令和外部命令 第五节课
centos 特殊权限 各种搜索命令 lsattr ,chattr,suid,sgid,sbit,file,type是否是内置命令,stat文件属性 ,whereis,locate,find,ln ...