当前位置:网站首页>AcWing 361. 观光奶牛 题解(spfa求正环)
AcWing 361. 观光奶牛 题解(spfa求正环)
2022-07-06 18:00:00 【乔大先生】
AcWing 361. 观光奶牛
由图中公式可知,这道题让求的是有一个正环即可,所以要寻找的是最长路径,所以if中的判断句判断的是能否找到更长边值
#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]; //记录边数
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]){
//要求的是正环,所以是最长路
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; //这个值可以,可以试图找更大值
else r = mid; //这个值不行,要找更小值
}
printf("%.2lf\n", r);
return 0;
}
边栏推荐
- golang 基础 —— 数据类型
- 交叉验证如何防止过拟合
- [chip scheme design] pulse oximeter
- 1123. The nearest common ancestor of the deepest leaf node
- 接收用户输入,身高BMI体重指数检测小业务入门案例
- 力扣1037. 有效的回旋镖
- Yunna | work order management measures, how to carry out work order management
- Make Jar, Not War
- c语言—数组
- Can the system hibernation file be deleted? How to delete the system hibernation file
猜你喜欢
随机推荐
Dark horse notes - create immutable sets and streams
golang中的WaitGroup实现原理
Neon Optimization: performance optimization FAQ QA
2022 Google CTF segfault Labyrinth WP
C language instance_ five
Oracle: Practice of CDB restricting PDB resources
1123. The nearest common ancestor of the deepest leaf node
Boot - Prometheus push gateway use
7.6 simulation summary
AI automatically generates annotation documents from code
Dark horse notes - exception handling
【案例分享】网络环路检测基本功能配置
身体质量指数程序,入门写死的小程序项目
树莓派/arm设备上安装火狐Firefox浏览器
ClickHouse字段分组聚合、按照任意时间段粒度查询SQL
Make Jar, Not War
对C语言数组的再认识
C # method of calculating lunar calendar date 2022
AI 从代码中自动生成注释文档
Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP









