题意:

  有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. CodeForces 1131G. Most Dangerous Shark

    题目简述:从左到右依次有$n \leq 10^7$个Domino骨牌,高度为$h_i$,手动推倒他的花费为$c_i$.每个骨牌之间的距离为$1$.一个骨牌可以被向左或者向右推倒.当第$i$个骨牌被推倒 ...

  2. TextBox控件TextMode=&quot;Password&quot;時

    TextBox控件TextMode="Password"時,觸發服務器端事件后,會清空掉TextBox的顯示值 2008-07-15 15:06:10|  分类: C#.NET 空 ...

  3. 鼠标右键添加cmd

    给鼠标右键添加 cmd https://jingyan.baidu.com/article/3f16e003c408142591c103b2.html 有一些软件,最好不要装到Program File ...

  4. Spring入门第九课

    使用外部属性文件 在配置文件里面配置Bean时,有时需要在Bean的配置里面混入系统部署的细节信息(例如:文件路径,数据源配置信息等)而这些部署细节实际上需要和Bean配置相分离. Spring提供了 ...

  5. 利用css制作横向和纵向时间轴

    Html代码: <div class="container"> <p>原文地址:http://www.cnblogs.com/xiaofeixiang/&l ...

  6. 一个基于Tp3.2(thinkphp3.2)的工会管理系统

    该系统包括11个模块. 会员管理模块 奖惩管理模块 运动会管理模块 新闻管理模块 文档管理模块 经费管理模块 电子提案管理模块 用户管理模块 权限管理模块 系统管理模块 系统的登录 系统主页 这里只是 ...

  7. 在element-ui的表格组件中为表头添加Tooltip 文字提示

    在使用表格组件的时候经常遇到的问题,列数很多,而表头的文字描述长度很长 <el-table-column v-if="!column.event" v-for="( ...

  8. FZU 2219【贪心】

    思路: 因为工人造完一个房子就死了,所以如果m<n则还需要n-m个工人. 最优的方案应该是耗时长的房子应该尽快建,而且最优的是越多的房子在建越好,也就是如果当前人数不到n,只派一个人去分裂. 解 ...

  9. 洛谷P3572 [POI2014]PTA-Little Bird

    P3572 [POI2014]PTA-Little Bird 题目描述 In the Byteotian Line Forest there are nn trees in a row. On top ...

  10. java.exe is valid, but is for a machine type other than the current machine

    java.exe is valid, but is for a machine type other than the current machine jdk版本不一致问题,在32位机器上使用64位的 ...