POJ3169
题目链接:http://poj.org/problem?id=3169
AC思路:
spfa算法。
设各牛的位置为x[ ]。
对于感情好的牛,即第2到ML+1行:A B D, 有x[B] - x[A] <=D.而对于感情不好的牛,即第ML+2到ML+MD+1行: A B D, 则有x[B] - x[A] >=D,可以转化为x[A] - x[B] <= -D.
这是一个差分约束题,第一类式子x[B] - x[A] <=D可以看成是从点B到点A的边权为D,而第二类式子也可以转换成x[A] - x[B] <= -D,看成是从点A到点B的负权边。
最终目的是要求 x[N] - x[1]的最大值,其实就是从N点出发到 1点的单源最短路问题。理解了差分约束这个概念,这道题就不难了。
AC代码:
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxv=+,maxe=+;
const int inf=0x7ffffff;
int edgecount,N;
int head[maxv],d[maxv],visitcount[maxv];
bool inq[maxv];
queue<pair<int,int> > q;
struct edge{
int from,to,val,next;
edge(){}
edge(int _f,int _t,int _v,int _n){
from=_f,to=_t,val=_v,next=_n;
}
}es[maxe];
void addedge(int from,int to,int val){
es[edgecount]=edge(from,to,val,head[from]);
// printf("es[%d] = (%d,%d,%d,%d)\n",edgecount,from,to,val,head[from]);
head[from]=edgecount++;
}
bool spfa(int s){
for(int i=;i<=N;i++){
d[i]=inf;
inq[i]=(i==s);
visitcount[i]=;
}
d[s]=;
q.push(make_pair(d[s],s));
while(!q.empty()){
int dist=q.front().first,u=q.front().second;
q.pop();
inq[u]=false;
if(visitcount[u]++>N) return true;
for(int e=head[u];e!=-;e=es[e].next){
int t=es[e].to,value=es[e].val;
if(d[t]>d[u]+value){
d[t]=d[u]+value;
if(!inq[t]){
inq[t]=true;
q.push(make_pair(d[t],t));
}
}
}
}
return false;
}
int main()
{
memset(head,-,sizeof(head));
int ML,MD,A,B,D; edgecount=;
scanf("%d%d%d",&N,&ML,&MD);
while(ML--){
scanf("%d%d%d",&A,&B,&D);
addedge(B,A,D);
}
while(MD--){
scanf("%d%d%d",&A,&B,&D);
addedge(A,B,-D);
} if(spfa(N)) printf("-1\n");
else if(d[]==inf) printf("-2\n");
else printf("%d\n",d[]);
// for(int i=1;i<=N;i++) printf("%d\n",d[i]);
return ;
}
POJ3169的更多相关文章
- POJ3169 Layout(差分约束系统)
POJ3169 Layout 题意: n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0.这些牛的距离存在着一些约束关系:1.有ml组(u, v, w)的约束关系,表示牛 ...
- POJ-3169 Layout---差分约束系统+Bellman
题目链接: https://vjudge.net/problem/POJ-3169 题目大意: 一些母牛按序号排成一条直线.有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最 ...
- 【图论】POJ-3169 差分约束系统
一.题目 Description Like everyone else, cows like to stand close to their friends when queuing for feed ...
- POJ-3169 Layout 最短路 差分约束
题目链接:https://cn.vjudge.net/problem/POJ-3169 题意 Farmer John手下的一些牛有自己喜欢的牛,和讨厌的牛 喜欢的牛之间希望距离在给定距离D之内 讨厌的 ...
- POJ-3169 Layout (差分约束+SPFA)
POJ-3169 Layout:http://poj.org/problem?id=3169 参考:https://blog.csdn.net/islittlehappy/article/detail ...
- [USACO2005][POJ3169]Layout(差分约束)
题目:http://poj.org/problem?id=3169 题意:给你一组不等式了,求满足的最小解 分析: 裸裸的差分约束. 总结一下差分约束: 1.“求最大值”:写成"<=& ...
- POJ3169 Layout
Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...
- poj3169 最短路(差分约束)
题意:一个农夫有n头牛,他希望将这些牛按照编号 1-n排成一条直线,允许有几头牛站在同一点,但是必须按照顺序,有一些牛关系比较好,希望站的距离不超过某个值,而有一些牛关系不太好,所以希望站的距离大于等 ...
- 【POJ3169 】Layout (认真的做差分约束)
Layout Description Like everyone else, cows like to stand close to their friends when queuing for ...
- POJ3169差分约束系统
题意:有n头牛,编号为1到n,对于关系好的ml头牛,al和bl之间的距离不大于dl,关系差的md头牛,ad和bd之间的距离不大于dd,求第1头牛和第n头牛之间的距离 分析:这是一道差分约束系统的题目, ...
随机推荐
- 基于Atlas实现mysql读写分离
一.实验环境 主机名IP地址 master192.168.200.111 slave192.168.200.112 atlas192.168.200.113 主从复制不再赘述,链接地址:授权Atlas ...
- Codeforces Round 623(Div. 2,based on VK Cup 2019-2020 - Elimination Round,Engine)D. Recommendations
VK news recommendation system daily selects interesting publications of one of n disjoint categories ...
- Codeforces Round #623 (Div. 2, based on VK Cup 2019-2020 - Elimination Round, Engine) B. Homecoming
After a long party Petya decided to return home, but he turned out to be at the opposite end of the ...
- CodeForces - 262C 贪心
Maxim always goes to the supermarket on Sundays. Today the supermarket has a special offer of discou ...
- 图论--LCA--在线RMQ ST
板子测试POJ1330,一发入魂,作者是KuangBin神犇,感谢? #include <cstdio> #include <cstring> #include <al ...
- 首次使用AWS服务器EC2
AWS有一年的免费套餐,这个便宜我得占. 申请的时候需要填写银行卡,AWS暂不支持储蓄卡,只好绑信用卡了. 创建EC2实例之后,下一个要解决的问题就是远程root访问. 1. 修改安全组设置 2. s ...
- golang 容器的学习与实践
golang 提供了几个简单的容器供我们使用,本文在介绍几种Golang 容器的基础上,实现一个基于Golang 容器的LRU算法. 容器介绍 Golang 容器位于 container 包下,提供了 ...
- ModuleNotFoundError: No module named 'pip'的解决方案
python在通过cmd安装pandas时遇到ModuleNotFoundError: No module named 'pip'的报错. 网上查询后通过如下两行cmd命令解决了 python -m ...
- springboot中json转换LocalDateTime失败的bug解决过程
环境:jdk1.8.maven.springboot 问题:前端通过json传了一个日期:date:2019-03-01(我限制不了前端开发给到后端的日期为固定格式,有些人就是这么不配合), ...
- Face The Right Way 开关(POJ3276)
描述: \( N 头牛排成了一列.每头牛或者向前或者向后.为了让所有的牛都面向前方,农夫约翰买了 一台自动转向的机器. 这个机器在购买时就必须设定一个数值 K,机器每操作一次恰好使 K 头连续的牛转向 ...