当前位置:网站首页>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;
}
边栏推荐
- 黑马笔记---异常处理
- 2022 Google CTF SEGFAULT LABYRINTH wp
- 2022 Google CTF SEGFAULT LABYRINTH wp
- POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
- Oracle:CDB限制PDB资源实战
- C language - array
- JTAG principle of arm bare board debugging
- golang中的atomic,以及CAS操作
- Neon Optimization: an optimization case of log10 function
- Make Jar, Not War
猜你喜欢

Analysis of mutex principle in golang

Make Jar, Not War

如何管理分布式团队?

2022 Google CTF SEGFAULT LABYRINTH wp

Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which

c语言—数组
![[advanced C language] 8 written questions of pointer](/img/d4/c9bb2c8c9fd8f54a36e463e3eb2fe0.png)
[advanced C language] 8 written questions of pointer

Instructions for using the domain analysis tool bloodhound

【C语言进阶篇】指针的8道笔试题

JTAG debugging experience of arm bare board debugging
随机推荐
一起看看matlab工具箱内部是如何实现BP神经网络的
Taro中添加小程序 “lazyCodeLoading“: “requiredComponents“,
2022 Google CTF segfault Labyrinth WP
Body mass index program, entry to write dead applet project
【案例分享】网络环路检测基本功能配置
Vocabulary in Data Book
Can the system hibernation file be deleted? How to delete the system hibernation file
Sword finger offer II 035 Minimum time difference - quick sort plus data conversion
How to prevent overfitting in cross validation
Google released a security update to fix 0 days that have been used in chrome
云呐|工单管理软件,工单管理软件APP
golang中的WaitGroup实现原理
Atomic in golang, and cas Operations
2022 Google CTF SEGFAULT LABYRINTH wp
身体质量指数程序,入门写死的小程序项目
Send template message via wechat official account
golang中的atomic,以及CAS操作
各种语言,软件,系统的国内镜像,收藏这一个仓库就够了: Thanks-Mirror
curl 命令
Taro2.* applet configuration sharing wechat circle of friends