HDU 3592 World Exhibition (差分约束,spfa,水)
题意:
有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,水)的更多相关文章
- HDU 1384 Intervals【差分约束-SPFA】
类型:给出一些形如a−b<=k的不等式(或a−b>=k或a−b<k或a−b>k等),问是否有解[是否有负环]或求差的极值[最短/长路径].例子:b−a<=k1,c−b&l ...
- 【poj3169】【差分约束+spfa】
题目链接http://poj.org/problem?id=3169 题目大意: 一些牛按序号排成一条直线. 有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离.如果没 ...
- O - Layout(差分约束 + spfa)
O - Layout(差分约束 + spfa) Like everyone else, cows like to stand close to their friends when queuing f ...
- 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 ...
- 图论分支-差分约束-SPFA系统
据说差分约束有很多种,但是我学过的只有SPFA求差分: 我们知道,例如 A-B<=C,那么这就是一个差分约束. 比如说,著名的三角形差分约束,这个大家都是知道的,什么两边之差小于第三边啦,等等等 ...
- 【HDOJ1529】【差分约束+SPFA+二分】
http://acm.hdu.edu.cn/showproblem.php?pid=1529 Cashier Employment Time Limit: 2000/1000 MS (Java/Oth ...
- 【HDOJ1384】【差分约束+SPFA】
http://acm.hdu.edu.cn/showproblem.php?pid=1384 Intervals Time Limit: 10000/5000 MS (Java/Others) ...
- hdu 1531 king(差分约束)
King Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu 1534 Schedule Problem (差分约束)
Schedule Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- poj3159 差分约束 spfa
//Accepted 2692 KB 1282 ms //差分约束 -->最短路 //TLE到死,加了输入挂,手写queue #include <cstdio> #include & ...
随机推荐
- SO_REUSEADDR的分析
今天协议个socket程序时碰到了这个问题,选自博客http://www.cppblog.com/ace/archive/2006/04/29/6446.html 敲完代码,等下看它.
- 深度解析VC中的消息传递机制
摘要:Windows编程和Dos编程,一个很大的区别就是,Windows编程是事件驱动,消息传递的.所以,要学好Windows编程,必须 对消息机制有一个清楚的认识,本文希望能够对消息的传递做一个全面 ...
- LayUI 子父窗体的交互
---恢复内容开始--- 收到的工作是将一个ERP的窗体程序改为网页实现,所以就肯定需要弹框来选择(如:物料编码.部门.业务员等等) 本文采取的前段框架是LayUI. layUI的官网API网址:ht ...
- Netty入门系列(2) --使用Netty解决粘包和拆包问题
前言 上一篇我们介绍了如果使用Netty来开发一个简单的服务端和客户端,接下来我们来讨论如何使用解码器来解决TCP的粘包和拆包问题 TCP为什么会粘包/拆包 我们知道,TCP是以一种流的方式来进行网络 ...
- IDEA如何找到接口的实现类
如何找到接口的实现类 (IDEA))在ApplicationContext上右击 Diagrams ->show diagram 可以看到继承关系: 在ApplicationContext上右击 ...
- 51nod1205(johnson)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1205 题意:中文题诶- 思路:johnson模板题 流水作业调 ...
- Apache为本地主机配置多个网站根目录详解
Author:KillerLegend Date:2014.5.27 From:http://blog.csdn.net/killerlegend/article/details/27195445 - ...
- 剑指Offer的学习笔记(C#篇)-- 二叉搜索树的后序遍历序列
题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 一 . 解题思想与二叉搜索树概念 (1). 二叉树 ...
- JSP 不同版本(转)
转自 http://blog.csdn.net/sunnyyoona/article/details/51076823
- day3字符串操作作业详解
1.day3题目 1.有变量name = "aleX leNb" 完成如下操作: 1) 移除 name 变量对应的值两边的空格,并输出处理结果 2) 移除name变量左边的&quo ...