当前位置:网站首页>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;
}
边栏推荐
- System clock failure of database fault tolerance
- Improving the readability of UI layer test with puppeter
- [Sylar] framework -chapter14 tcpserver module
- Easycvr Video Square snapshot adding device channel offline reason display
- [Sylar] framework -chapter20- daemon module
- What is the core value of testing?
- 100 lectures on Excel practical application cases (XI) - tips for inserting pictures in Excel
- App test process and test points
- Cmake usage base summary
- Leetcode 18. sum of four numbers
猜你喜欢

Introduction to this pointer

go-zero单体服务使用泛型简化注册Handler路由

物联网工业串口转WiFi模块 无线路由WiFi模块的选型

Automated test tool playwright (quick start)

低代码是开发的未来吗?浅谈低代码平台

Interview fraud: there are companies that make money from interviews

could only be written to 0 of the 1 minReplication nodes. There are 0 datanode(s) running and 0 node

How to upgrade a pair of 12.2 RAC(primary) and a pair of 12.2 RAC(dataguard) to 19c
![[Hongke technology] Application of network Multimeter in data center](/img/28/2ecc5a7a766454968819c7748fe48e.png)
[Hongke technology] Application of network Multimeter in data center

Special topic of APP performance design and Optimization - poor implementation affecting performance
随机推荐
Angr (XI) - official document (Part2)
MySQL数据库————初识数据库
What SaaS architecture design do you need to know?
Rendering process, how the code becomes a page (I)
Machine learning and deep learning -- normalization processing
FPGA: use PWM wave to control LED brightness
FPGA:使用PWM波控制LED亮度
pytorch打包exe出现WARNING: file already exists but should not: C:\Users\workAI\AppData\Local\Temp\_MEI13
你必需要了解的saas架构设计?
【sylar】框架篇-Chapter12-ByteArray 模块
[Sylar] framework -chapter20- daemon module
[Sylar] framework -chapter14 tcpserver module
Alibaba interview question [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]
The first artificial intelligence security competition starts. Three competition questions are waiting for you to fight
Cmake usage base summary
Testcafe's positioning, operation of page elements, and verification of execution results
塑料可以执行GB/T 2408 -燃烧性能的测定吗
欧拉路/欧拉回路
01 node express system framework construction (express generator)
Redis配置文件详解/参数详解及淘汰策略