当前位置:网站首页>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;
}
边栏推荐
- Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
- What are the differences between Oracle Linux and CentOS?
- 2022 Google CTF SEGFAULT LABYRINTH wp
- Oracle: Practice of CDB restricting PDB resources
- NEON优化:矩阵转置的指令优化案例
- How to prevent overfitting in cross validation
- Add the applet "lazycodeloading": "requiredcomponents" in taro,
- Dark horse notes - create immutable sets and streams
- 云呐|工单管理办法,如何开展工单管理
- NEON优化:性能优化常见问题QA
猜你喜欢

Typical problems of subnet division and super network construction

The MySQL database in Alibaba cloud was attacked, and finally the data was found

字节P7专业级讲解:接口测试常用工具及测试方法,福利文

Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform

LeetCode:1175. 质数排列

JTAG principle of arm bare board debugging

Body mass index program, entry to write dead applet project

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

Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period

移植DAC芯片MCP4725驱动到NUC980
随机推荐
今日问题-2022/7/4 lambda体中修改String引用类型变量
Add the applet "lazycodeloading": "requiredcomponents" in taro,
Google发布安全更新,修复Chrome中已被利用的0 day
从底层结构开始学习FPGA----FIFO IP的定制与测试
Yunna | work order management software, work order management software app
Neon Optimization: performance optimization FAQ QA
[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)
各种语言,软件,系统的国内镜像,收藏这一个仓库就够了: Thanks-Mirror
How to evaluate load balancing performance parameters?
黑马笔记---异常处理
2022 Google CTF SEGFAULT LABYRINTH wp
黑马笔记---创建不可变集合与Stream流
DS-5/RVDS4.0变量初始化错误
Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period
Neon Optimization: an instruction optimization case of matrix transpose
对C语言数组的再认识
Case development of landlord fighting game
How to manage distributed teams?
Amway wave C2 tools
shell脚本快速统计项目代码行数