当前位置:网站首页>HDU 3592 World Exhibition (differential constraint)
HDU 3592 World Exhibition (differential constraint)
2022-07-28 04:53:00 【gongyuandaye】
The question :n Individuals in order 1-n line up , Can be arranged side by side , give x To the relationship , The distance between two is no more than D; give y To the relationship , The distance between two is not less than D, seek 1 To n The longest distance of .
Answer key : Difference constraint
Seek the farthest , run spfa shortest path .
The difference constraint equation can be established according to the meaning of the question .
Because of side by side , So the distance limit between two adjacent people is ≤0.
Be careful inf Output -2.
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<fstream>
#include<set>
#include<map>
#include<sstream>
#include<iomanip>
#define ll long long
#define pii pair<int, int>
using namespace std;
const int maxn = 1e3 + 5;
const int maxm = 3e4 + 5;
int t, n, x, y, a, b, D;
struct node {
int v, nxt, w;
}edge[maxm];
int vis[maxn], d[maxn], head[maxn], mark[maxn], k, s;
void add(int u, int v, int w) {
edge[++k].nxt = head[u];
edge[k].v = v;
edge[k].w = w;
head[u] = k;
}
bool spfa() {
for (int i = 1; i <= n; i++) {
vis[i] = mark[i] = 0;
d[i] = 0x3f3f3f3f;
}
queue<int>q;
q.push(s);
mark[s] = vis[s] = 1;
d[s] = 0;
while (!q.empty()) {
int u = q.front(); q.pop();
vis[u] = 0;
for (int i = head[u]; i; i = edge[i].nxt) {
int v = edge[i].v, w = edge[i].w;
if (d[v] > d[u] + w) {
d[v] = d[u] + w;
if (vis[v]) continue;
vis[v] = 1;
if(++mark[v] > n) return false; // Negative loop n+1 Only above can there be a negative ring
q.push(v);
}
}
}
return true;
}
int main() {
scanf("%d", &t);
while (t--) {
k = 0;
memset(head, 0, sizeof(head));
scanf("%d%d%d", &n, &x, &y);
for (int i = 2; i <= n; i++) add(i, i - 1, 0);
for (int i = 1; i <= x; i++) {
scanf("%d%d%d", &a, &b, &D);
add(a, b, D);
}
for (int i = 1; i <= y; i++) {
scanf("%d%d%d", &a, &b, &D);
add(b, a, -D);
}
s = 1;
if (spfa()) {
printf("%d\n", d[n] == 0x3f3f3f3f ? -2 : d[n]);
}
else puts("-1");
}
return 0;
}
边栏推荐
- C语言ATM自动取款机系统项目的设计与开发
- What tools do software testers need to know?
- [Sylar] practical part - redis based parameter query service
- 驾驭EVM和XCM的强大功能,SubWallet如何赋能波卡和Moonbeam
- Summary and review of puppeter
- 【sylar】框架篇-Chapter23-模块篇总结
- [Sylar] framework -chapter12 bytearray module
- Performance comparison between set and list
- 王爽汇编语言详细学习笔记三:寄存器(内存访问)
- Cloudcompare & PCL point cloud least square fitting plane
猜你喜欢

解析智能扫地机器人中蕴含的情感元素

Strlen introduction, and the difference between sizeof

Is low code the future of development? On low code platform

Mac installs mysql5.7 through brew

Jupyter notebook installation code prompt function
![[每日一氵]上古年代的 Visual Studio2015 安装](/img/b1/066ed0b9e93b8f378c89ee974163e5.png)
[每日一氵]上古年代的 Visual Studio2015 安装

C语言ATM自动取款机系统项目的设计与开发

Web渗透之域名(子域名)收集方法

How to upgrade a pair of 12.2 RAC(primary) and a pair of 12.2 RAC(dataguard) to 19c

王爽汇编语言详细学习笔记三:寄存器(内存访问)
随机推荐
set与list性能对比
Testcafe provides automatic waiting mechanism and live operation mode
[Sylar] framework -chapter14 tcpserver module
Gerrit operation - rollback a patch_ set
Introduction to testcafe
pytorch打包exe出现WARNING: file already exists but should not: C:\Users\workAI\AppData\Local\Temp\_MEI13
Depth traversal and breadth traversal of tree structure in JS
Take out system file upload
Mysql database -- first knowledge database
How to quickly locate bugs? How to write test cases?
Explain initialization list
Comprehensively analyze the differences between steam and maker Education
Rendering process, how the code becomes a page (I)
外卖系统 文件上传
Constructor of member function
Youxuan database participated in the compilation of the Research Report on database development (2022) of the China Academy of communications and communications
Wang Shuang assembly language detailed learning notes 3: registers (memory access)
Warning: file already exists but should not: c:\users\workmai\appdata\local\temp appears when Python packages exe\_ MEI13
Domain name (subdomain name) collection method of Web penetration
Nat fundamentals and private IP