题意:

  有n个人在排队,按照前后顺序编号为1~n,现在对其中某两人的距离进行约束,有上限和下限,表示dis[a,b]<=c或者dis[a,b]>=c,问第1个人与第n个人的距离最多可能为多少?(若INF则输出-2,若冲突则输出-1,否则输出距离)

思路:

  建图时都将约束转成a-b<=c的标准形式,然后建一条b->a的边,权为c。然后求最短路,注意最短路跑出来的结果却是最远的合法距离,而不是最短距离。本题无需添加辅助边,只要到达不了n,则距离为INF,输出-2,若有负环,那肯定是冲突了,为-1。

 //#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <deque>
#include <map>
#include <algorithm>
#include <vector>
#include <iostream>
#define pii pair<int,int>
#define back que[rear-1]
#define INF 0x3f3f3f3f
#define LL long long
#define ULL unsigned long long
using namespace std;
const double PI = acos(-1.0);
const int N=;
struct node
{
int from,to,dis,next;
node(){};
node(int from,int to,int dis,int next):from(from),to(to),dis(dis),next(next){};
}edge[N*N]; int edge_cnt, head[N];
int inq[N], cnt[N], dist[N], n; void add_node(int from,int to,int dis)
{
edge[edge_cnt]=node(from,to,dis,head[from]);
head[from]=edge_cnt++;
} int spfa(int st,int ed)
{
memset(cnt,,sizeof(cnt));//入队次数
memset(inq,,sizeof(inq));//是否在队中
memset(dist,0x3f,sizeof(dist));//距离
deque<int> que(,st);
inq[st]=;
dist[st]=;
while(!que.empty())
{
int t=que.front();que.pop_front();
inq[t]=;node e;
for(int i=head[t]; i!=-; i=e.next)
{
e=edge[i];
if( dist[e.to]>dist[t]+e.dis )
{
dist[e.to]=dist[t]+e.dis;
if(!inq[e.to]) //没有在队列中
{
if(++cnt[e.to]>n) //入队次数过多
return -;
inq[e.to]=;//下面是优化,可删
if(!que.empty() && dist[e.to]<dist[que.front()])
que.push_front(e.to);
else que.push_back(e.to);
}
} }
}
return dist[ed]==INF?-:dist[ed];
} void init()
{
edge_cnt=;
//for(int i=0; i<=n; i++) head[i]=-1;
memset(head,-,sizeof(head));
} int main()
{
freopen("input.txt", "r", stdin);
int x, y, a, b, c, t;cin>>t;
while(t--)
{
init();
scanf("%d%d%d",&n,&x,&y);
for(int i=; i<=x; i++) //最多
{
scanf("%d%d%d",&a,&b,&c);
add_node(a,b,c);
}
for(int i=; i<=y; i++) //最小
{
scanf("%d%d%d",&a,&b,&c);
add_node(b,a,-c);
}
printf("%d\n", spfa(,n));
}
return ;
}

AC代码

HDU 3592 World Exhibition (差分约束,spfa,水)的更多相关文章

  1. HDU 1384 Intervals【差分约束-SPFA】

    类型:给出一些形如a−b<=k的不等式(或a−b>=k或a−b<k或a−b>k等),问是否有解[是否有负环]或求差的极值[最短/长路径].例子:b−a<=k1,c−b&l ...

  2. 【poj3169】【差分约束+spfa】

    题目链接http://poj.org/problem?id=3169 题目大意: 一些牛按序号排成一条直线. 有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离.如果没 ...

  3. O - Layout(差分约束 + spfa)

    O - Layout(差分约束 + spfa) Like everyone else, cows like to stand close to their friends when queuing f ...

  4. POJ 1364 / HDU 3666 【差分约束-SPFA】

    POJ 1364 题解:最短路式子:d[v]<=d[u]+w 式子1:sum[a+b+1]−sum[a]>c      —      sum[a]<=sum[a+b+1]−c−1  ...

  5. 图论分支-差分约束-SPFA系统

    据说差分约束有很多种,但是我学过的只有SPFA求差分: 我们知道,例如 A-B<=C,那么这就是一个差分约束. 比如说,著名的三角形差分约束,这个大家都是知道的,什么两边之差小于第三边啦,等等等 ...

  6. 【HDOJ1529】【差分约束+SPFA+二分】

    http://acm.hdu.edu.cn/showproblem.php?pid=1529 Cashier Employment Time Limit: 2000/1000 MS (Java/Oth ...

  7. 【HDOJ1384】【差分约束+SPFA】

    http://acm.hdu.edu.cn/showproblem.php?pid=1384 Intervals Time Limit: 10000/5000 MS (Java/Others)     ...

  8. hdu 1531 king(差分约束)

    King Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  9. hdu 1534 Schedule Problem (差分约束)

    Schedule Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  10. poj3159 差分约束 spfa

    //Accepted 2692 KB 1282 ms //差分约束 -->最短路 //TLE到死,加了输入挂,手写queue #include <cstdio> #include & ...

随机推荐

  1. linux工具————fish shell

    1.说明 fish is a fully-equipped command line shell (like bash or zsh) that is smart and user-friendly. ...

  2. yzm10的ACM集训小感

    7月30号,ACM集训进行了两周,一切都已on the right way.这时的我适时地从题海中探出头,其实除了刷题,也该写点什么来总结下过去.首先,在第一周里,我学习了数据结构,知道了STL这么一 ...

  3. 【转】 mybatis如何在控制台打印执行的sql语句

    <strong>######################################################################### #Root Logger ...

  4. Fitnesse框架简单介绍

    1.Fitnesse是什么? 官方的说明:FitNesse is a wiki server. It's also a test execution engine. Fitnesse是一个wiki s ...

  5. 无监督学习:Deep Auto-encoder(深度自动编码器)

    一 Auto-encoder NN Encoder & NN Decoder 要一起训练. 二 Starting from PCA 三 Deep Auto-encoder PCA&De ...

  6. python-根据URL地址下载文件

    博主个人网站:https://chenzhen.online 使用Python中提供的urllib.request下载网上的文件 #coding=utf-8 """ 目标 ...

  7. opengl Polygon Offset

    http://www.cnblogs.com/bitzhuwei/archive/2015/06/12/4571539.html#_label2 启用了Polygon Offset,那么到底要把立方体 ...

  8. Pycharm 设置TextStyle

    之前在脚本中选择了一个字符串, PyCharm会"高亮"所有相同的字符串, 但是我不满意这个"高亮"的颜色,因为和背景色太相似了,所以需要做一下操作,修改这个& ...

  9. [Xcode 实际操作]六、媒体与动画-(13)使用UIImageView制作帧动画

    目录:[Swift]Xcode实际操作 本文将演示如何将导入的序列图片,转换为帧动画. 在项目导航区打开资源文件夹[Assets.xcassets] [+]->[Import]->选择图片 ...

  10. C 语言实例 - 输出九九乘法口诀表

    C 语言实例 - 输出九九乘法口诀表 使用嵌套 for 循环输出九九乘法口诀表. 实例 #include<stdio.h> int main(){ //外层循环变量,控制行 ; //内层循 ...