当前位置:网站首页>HDU 3666 the matrix problemdifferential constraint + stack optimization SPFA negative ring
HDU 3666 the matrix problemdifferential constraint + stack optimization SPFA negative ring
2022-07-28 04:54:00 【gongyuandaye】
The question : Give a N∗M Matrix C, It is required to construct two sequences a1,a2,…,an and b1,b2,…,bm, The order of the matrix i Multiply the row element by ai, The order of the matrix j Column element divided by bj, Each element of the final matrix Cij∈[L,U]. If it can be constructed successfully , The output YES, Otherwise output NO.
Answer key : Difference constraint + Stack optimization spfa
The constraint equation can be obtained : L < = c ∗ a i / b j < = U L <= c * ai / bj <= U L<=c∗ai/bj<=U
Habit is the longest way , So it can be converted into :
a i / b j > = L / c ai/bj>=L/c ai/bj>=L/c and − a i / b j > = − U / c -ai/bj>=-U/c −ai/bj>=−U/c
Take... On both sides log It can be converted to normal addition and subtraction .
Then the question goes directly bfs Negative ring, i.e >n+m Meeting t, See the root sign , But I don't know how to prove , You can actually use dfs Negative link , That is, keep running this ring , This will be very efficient , We use stack simulation dfs It's just the process of .
Be careful double.
#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 = 2e5 + 5;
int n, m, l, u, a;
struct node {
int v, nxt;
double w;
}edge[maxn << 2];
int vis[maxn], head[maxn], mark[maxn], k, s;
double d[maxn];
void add(int u, int v, double 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 + m; i++) {
// vis[i] = mark[i] = 0;
// d[i] = -0x3f3f3f3f; // Longest route version
// }
// 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;
// double 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] > sqrt(n + m)) return false; // Negative loop n+1 Only above can there be a negative ring
// q.push(v);
// }
// }
// }
// return true;
//}
int q[maxn];
bool spfa() {
for (int i = 1; i <= n + m; i++) {
d[i] = -0x3f3f3f3f;
vis[i] = mark[i] = 0;
}
mark[s] = vis[s] = 1;
d[s] = 0;
int ed = 0;
//q[++ed] = s;
stack<int> q;
q.push(s);
while (q.size()) {
//int u = q[ed--];
int u = q.top();
q.pop();
vis[u] = 0;
for (int i = head[u]; i; i = edge[i].nxt) {
int v = edge[i].v;
double 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 + m) return false;
//q[++ed] = v;
q.push(v);
}
}
}
return true;
}
int main() {
while (~scanf("%d%d%d%d", &n, &m, &l, &u)) {
k = 0;
memset(head, 0, sizeof(head));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
scanf("%d", &a);
add(n + j, i, log(1.0 * l / a));
add(i, j + n, -log(1.0 * u / a));
}
}
s = 1;
if (spfa()) puts("YES");
else puts("NO");
}
return 0;
}
边栏推荐
- Mysql database -- first knowledge database
- [每日一氵]上古年代的 Visual Studio2015 安装
- [Hongke technology] Application of network Multimeter in data center
- [Sylar] framework -chapter15 stream module
- alter和confirm,prompt的区别
- [Sylar] framework -chapter7-io coordination scheduling module
- 塑料可以执行GB/T 2408 -燃烧性能的测定吗
- Domain name (subdomain name) collection method of Web penetration
- Inspire domestic students to learn robot programming education for children
- linux下安装mysql
猜你喜欢

Machine learning and deep learning -- normalization processing

Leetcode 454. Adding four numbers II

excel实战应用案例100讲(十一)-Excel插入图片小技巧

With a monthly salary of 15.5K, he failed to start a business and was heavily in debt. How did he reverse the trend through software testing?

解析智能扫地机器人中蕴含的情感元素
![[Hongke technology] Application of network Multimeter in data center](/img/28/2ecc5a7a766454968819c7748fe48e.png)
[Hongke technology] Application of network Multimeter in data center

Service object creation and use

Redis type

RT_ Use of thread mailbox

Automated test tool playwright (quick start)
随机推荐
Design and development of C language ATM system project
[函数文档] torch.histc 与 paddle.histogram 与 numpy.histogram
[Sylar] framework Chapter 22 auxiliary module
吉利AI面试题【杭州多测师】【杭州多测师_王sir】
[Sylar] framework -chapter12 bytearray module
If mongoose exists, it will be updated; if it does not exist, it will be added
启发国内学子学习少儿机器人编程教育
go-zero单体服务使用泛型简化注册Handler路由
全方位分析STEAM和创客教育的差异化
[Sylar] framework -chapter9-hook module
The go zero singleton service uses generics to simplify the registration of handler routes
Artificial intelligence and RPA technology application (I) -rpa Hongji product introduction, designer interface function explanation
Mac installs mysql5.7 through brew
[Sylar] framework -chapter7-io coordination scheduling module
FPGA: use PWM wave to control LED brightness
Observable time series data downsampling practice in Prometheus
[Sylar] framework -chapter11 socket module
你必需要了解的saas架构设计?
(clone virtual machine steps)
CPU and memory usage are too high. How to modify RTSP round robin detection parameters to reduce server consumption?