HDU-3592 World Exhibition
差分约束。
很容易看出两种约束方式,然后建图。而且题目要求排序不能乱,于是加上第三种约束。
求最长就跑一遍最短路啊就行了。
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <deque> #define rep(i, l, r) for(int i = l; i <= r; i++)
#define down(i, l, r) for(int i = l; i >= r; i--)
#define N 1234
#define M 56789
#define ll long long
#define MAX 1<<30 using namespace std;
int read()
{
int x=0, f=1; char ch=getchar();
while (ch<'0' || ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
while (ch>='0' && ch<='9') { x=x*10+ch-'0'; ch=getchar(); }
return f*x;
} struct edge{int y, n, v;} e[M]; int fir[N], en;
int n, x, y, c[N], d[N], ans, o;
bool b[N]; void Add(int x, int y, int v)
{
en++, e[en].y=y, e[en].v=v, e[en].n=fir[x], fir[x]=en;
} int main()
{
int t=read();
while (t--)
{
en=ans=0; rep(i, 1, n) fir[i]=0;
scanf("%d%d%d", &n, &x, &y);
rep(i, 1, x)
{ int a=read(), b=read(), c=read(); Add(a, b, c); }
rep(i, 1, y)
{ int a=read(), b=read(), c=read(); Add(b, a, -c); }
rep(i, 2, n) Add(i, i-1, 0);
deque <int> q;
rep(i, 1, n) b[i]=1, c[i]=1, d[i]=MAX, q.push_back(i); d[1]=0;
while (!q.empty())
{
x=q.front(); o=fir[x]; y=e[o].y; q.pop_front(); b[x]=0;
if (c[x] > n) { ans=-1; break; }
while (o)
{
if (d[y]>d[x]+e[o].v)
{
d[y]=d[x]+e[o].v;
if (!b[y]) b[y]=1, c[y]++, !q.empty()&&d[y]<=d[q.front()] ? q.push_front(y) : q.push_back(y);
}
o=e[o].n, y=e[o].y;
}
}
if (ans==-1) printf("-1\n");
else if (d[n]==MAX) printf("-2\n");
else printf("%d\n", d[n]-d[1]);
}
return 0;
}
HDU-3592 World Exhibition的更多相关文章
- HDU 3592 World Exhibition(线性差分约束,spfa跑最短路+判断负环)
World Exhibition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3592 World Exhibition (差分约束,spfa,水)
题意: 有n个人在排队,按照前后顺序编号为1~n,现在对其中某两人的距离进行约束,有上限和下限,表示dis[a,b]<=c或者dis[a,b]>=c,问第1个人与第n个人的距离最多可能为多 ...
- 差分约束系统+spfa(B - World Exhibition HDU - 3592 )
题目链接:https://cn.vjudge.net/contest/276233#problem/B 思路和上一个一样,不过注意点有两个,第一,对dis数组进行初始化的时候,应该初始化成ox3f3f ...
- POJ 3169 Layout (HDU 3592) 差分约束
http://poj.org/problem?id=3169 http://acm.hdu.edu.cn/showproblem.php?pid=3592 题目大意: 一些母牛按序号排成一条直线.有两 ...
- 【HDOJ】3592 World Exhibition
基础差分约束. /* 3592 */ #include <iostream> #include <algorithm> #include <queue> #incl ...
- 差分约束 HDU - 1384 HDU - 3592 HDU - 1531 HDU - 3666
Intervals Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- hdu图论题目分类
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
- HDU图论题单
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
- 【转】最短路&差分约束题集
转自:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★254 ...
随机推荐
- mvc的help和functions语法
@helper show(int num ) { ) { @:存在 } else { @:不存在 } } @functions { /// <summary> /// 方法必须要求为静态 ...
- 2002-2003 ACM-ICPC Northeastern European Regional Contest (NEERC 02) H Heroes Of Might And Magic (隐含dp)
问题是求一个方案,实际隐含一个dp.法力是递减的,所以状态是DAG,对于一个确定的状态,我们贪心地希望英雄的血量尽量大. 分析:定义状态dp[i][p][h]表示是已经用了i的法力值,怪兽的位置在p, ...
- UVA 1451 Average平均值 (数形结合,斜率优化)
摘要:数形结合,斜率优化,单调队列. 题意:求一个长度为n的01串的子串,子串长度至少为L,平均值应该尽量大,多个满足条件取长度最短,还有多个的话,取起点最靠左. 求出前缀和S[i],令点Pi表示(i ...
- UVA 12898 - And Or 与和或 (思路题)
思路就是有零一变化的位Or以后一定是1,And以后一定是0:那么如果b的二进制更长那么就把包含a的部分全部置为1或0,如果一样长那么就把不同的部分置为1或0. 今天被这题坑的地方:1默认是int,如果 ...
- UVA208 Firetruck 消防车(并查集,dfs)
要输出所有路径,又要字典序,dfs最适合了,用并查集判断1和目的地是否连通即可 #include<bits/stdc++.h> using namespace std; ; int p[m ...
- Spark源码分析之-Storage模块
原文链接:http://jerryshao.me/architecture/2013/10/08/spark-storage-module-analysis/ Background 前段时间琐事颇多, ...
- spring5之SAXParseException:cvc-elt.1: 找不到元素 “beans” 的声明
之前SSM项目一直报错,就是找不到错误 气啊 后来在网上找到了答案:燕来spring5之后就不再需要写版本号了
- C++ NULL与nullptr的区别
C与C++中空指针的区别 在C里面,由于处处都要使用指针,所以导致NULL遍布各地.我们先来看C99是怎么定义NULL的: NULL can be defined as any null pointe ...
- git使用stash存储相关操作
git stash 将当前修改存储起来 git stash apply 恢复最近一次存储 git stash apply stash@{2} 恢复某一次存储 git stash list 查看存储列 ...
- CE软件修改器
下载地址: 链接:https://pan.baidu.com/s/1WQa5epfmLW92xk0XY10pqw 提取码:jt3k 喜欢请点赞