当前位置:网站首页>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;
}
边栏推荐
- Go zero micro service practical series (IX. ultimate optimization of seckill performance)
- AI automatically generates annotation documents from code
- 分享一个通用的so动态库的编译方法
- NEON优化:矩阵转置的指令优化案例
- 图片打水印 缩放 和一个输入流的转换
- 今日问题-2022/7/4 lambda体中修改String引用类型变量
- 一起看看matlab工具箱内部是如何实现BP神经网络的
- Metauniverse urban legend 02: metaphor of the number one player
- Add the applet "lazycodeloading": "requiredcomponents" in taro,
- 让我们,从头到尾,通透网络I/O模型
猜你喜欢
字节P7专业级讲解:接口测试常用工具及测试方法,福利文
Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP
shell脚本快速统计项目代码行数
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
【案例分享】网络环路检测基本功能配置
Analysis of mutex principle in golang
Body mass index program, entry to write dead applet project
Typical problems of subnet division and super network construction
Can the system hibernation file be deleted? How to delete the system hibernation file
Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
随机推荐
搭建【Redis in CentOS7.x】
C # method of calculating lunar calendar date 2022
The difference between spin and sleep
NEON优化:性能优化常见问题QA
go-zero微服务实战系列(九、极致优化秒杀性能)
How to evaluate load balancing performance parameters?
C language instance_ five
Table table setting fillet
C language instance_ three
Google发布安全更新,修复Chrome中已被利用的0 day
Js逆向——捅了【马蜂窝】的ob混淆与加速乐
Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period
各种语言,软件,系统的国内镜像,收藏这一个仓库就够了: Thanks-Mirror
Oracle: Practice of CDB restricting PDB resources
LeetCode:1175. 质数排列
POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
What does security capability mean? What are the protection capabilities of different levels of ISO?
DS-5/RVDS4.0变量初始化错误
Taro2.* applet configuration sharing wechat circle of friends
C language instance_ two