当前位置:网站首页>AcWing 361. Sightseeing cow problem solution (SPFA seeking positive ring)
AcWing 361. Sightseeing cow problem solution (SPFA seeking positive ring)
2022-07-07 01:37:00 【Mr. Qiao Da】
AcWing 361. Sightseeing cows 
According to the formula in the figure , What this problem asks is to have a positive ring , So we are looking for the longest path , therefore if The judgement sentence in determines whether a longer boundary value can be found
#include<bits/stdc++.h>
using namespace std;
const int N = 1010, M = 5210;
int h[N], ne[M], e[M], w[M], idx;
int n, m;
double dist[N];
int wf[N];
bool st[N];
int cnt[N]; // Record edges
void add(int a, int b, int c){
e[idx] = b;
ne[idx] = h[a];
w[idx] = c;
h[a] = idx ++ ;
}
bool check(double mid){
memset(dist, 0, sizeof dist);
memset(st, 0, sizeof st);
memset(cnt, 0, sizeof cnt);
queue<int>q;
for(int i = 1; i <= n; i ++ ){
q.push(i);
st[i] = true;
}
while(q.size()){
int t = q.front();
q.pop();
st[t] = false;
for(int i = h[t]; ~i; i = ne[i]){
int j = e[i];
if(dist[j] < dist[t] + wf[t] - mid * w[i]){
// What is required is a positive ring , So it's the longest way
dist[j] = dist[t] + wf[t] - mid * w[i];
cnt[j] = cnt[t] + 1;
if(cnt[j] >= n) return true;
if(!st[j]){
q.push(j);
st[j] = true;
}
}
}
}
return false;
}
int main()
{
cin>>n>>m;
for(int i = 1; i <= n; i ++ ) cin>>wf[i];
memset(h, -1, sizeof h);
for(int i = 0; i < m; i ++ ){
int a, b, c;
cin>>a>>b>>c;
add(a, b, c);
}
double l = 0, r = 1e6;
while(r - l > 1e-4){
double mid = (l + r) / 2;
if(check(mid)) l = mid; // This value can , You can try to find a larger value
else r = mid; // This value is not good , Looking for a smaller value
}
printf("%.2lf\n", r);
return 0;
}
边栏推荐
- go-zero微服务实战系列(九、极致优化秒杀性能)
- JS ES5也可以创建常量?
- Neon Optimization: About Cross access and reverse cross access
- 前置机是什么意思?主要作用是什么?与堡垒机有什么区别?
- Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP
- WCF基金会
- MySQL script batch queries all tables containing specified field types in the database
- Gin 入门实战
- AcWing 1142. 繁忙的都市 题解(最小生成树)
- AcWing 1148. 秘密的牛奶运输 题解(最小生成树)
猜你喜欢

AcWing 361. 观光奶牛 题解(spfa求正环)

LeetCode. 剑指offer 62. 圆圈中最后剩下的数

【信号与系统】

Appium自动化测试基础 — uiautomatorviewer定位工具

AcWing 345. 牛站 题解(floyd的性质、倍增)

2022 Google CTF SEGFAULT LABYRINTH wp

js如何快速创建一个长度为 n 的数组

shell脚本快速统计项目代码行数

Let's see through the network i/o model from beginning to end

第三方跳转网站 出现 405 Method Not Allowed
随机推荐
C language instance_ two
Dark horse notes - create immutable sets and streams
7.6 simulation summary
shell脚本快速统计项目代码行数
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
鼠标右键 自定义
Comparison of picture beds of free white whoring
字节P7专业级讲解:接口测试常用工具及测试方法,福利文
Neon Optimization: performance optimization FAQ QA
C language instance_ four
黑马笔记---创建不可变集合与Stream流
Install Firefox browser on raspberry pie /arm device
糊涂工具类(hutool)post请求设置body参数为json数据
How to prevent overfitting in cross validation
dvajs的基础介绍及使用
前置机是什么意思?主要作用是什么?与堡垒机有什么区别?
454-百度面经1
[advanced C language] 8 written questions of pointer
Google released a security update to fix 0 days that have been used in chrome
Use nodejs to determine which projects are packaged + released