当前位置:网站首页>(2022 Niuke multi school) D-Link with game glitch (SPFA)
(2022 Niuke multi school) D-Link with game glitch (SPFA)
2022-07-25 05:45:00 【AC__ dream】
subject :
The sample input :
3 3
1 1 2 2
1 2 2 1
1 3 1 1Sample output :
0.5000000000The question : Given m A way of synthesizing objects , Find a maximum synthetic loss parameter w, So that all items cannot be obtained infinitely through infinite synthesis .
analysis : It should be noted that the greater the loss parameter value, the less the loss when the article is synthesized , So that is to say, this topic w Monotonicity , When w When reaching a boundary value ,w No matter how big, there will be some items that can be obtained indefinitely , and w No matter how small, all items can't be obtained indefinitely , So we can solve it directly by dichotomy .
For one w We judge whether it is feasible , Is equivalent to this topic :Arbitrage(spfa)_AC__dream The blog of -CSDN Blog w
Only need to run once spfa Judge whether there is a positive ring in the figure below to know whether there is an item that can be synthesized infinitely , But there is another very important point of this topic that needs special attention , That is to say a,c The boundary of the scope is 1e3, All in all 1000 Items , In extreme cases 1 After exchanging items, you will get 1e3000, This number is relatively large , The loss of accuracy will be serious , So we can't directly update the original data , We can Take the logarithm of the original data and then update it . To put it a little bit : If opened long double You can also put this topic ac, But I still hope you will pay attention to this skill .
W Is the loss parameter a[i] individual t Items can be exchanged c[i] individual j goods , be
Update method of original data :
if(d[t]*c[i]/a[i]*W>=d[j]) d[j]=d[t]*c[i]/a[i]*W
Update method after taking logarithm :
if(log(d[t])+log(c[i]/a[i])+log(W)>=log[d[j]]) log[d[j]=log(d[t])+log(c[i]/a[i])+log(W)
The basis of this transformation is log A function is monotonically increasing in its domain , So if d[t]*c[i]/a[i]*W>=d[j], Then there are log(d[t])+log(c[i]/a[i])+log(W)>=log[d[j]]. We directly order d[i]=log(d[i]), Then make w[i]=log(c[i]/a[i]),W=log(W) that will do , Then the update condition becomes :
if(d[t]+w[i]+W>d[j]) d[j]=d[t]+w[i]+W
Then just use one cnt The array can determine the positive ring , Be careful spfa Judge the ring , It is possible that the graph is not connected , So we need to add all points to the queue at the beginning . See code for details :
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
using namespace std;
const int N=1e4+10;
int h[N],e[N],ne[N],cnt[N],idx;
double w[N],d[N];
bool vis[N];
int n,m;
void add(int x,int y,double z)
{
e[idx]=y;
w[idx]=z;
ne[idx]=h[x];
h[x]=idx++;
}
bool spfa(double W)
{
W=log(W);
queue<int>q;
for(int i=1;i<=n;i++)
{
cnt[i]=0;
vis[i]=false;
q.push(i);
}
while(!q.empty())
{
int t=q.front();
q.pop();
vis[t]=false;
for(int i=h[t];i!=-1;i=ne[i])
{
int j=e[i];
if(d[t]+w[i]+W>d[j])
{
d[j]=d[t]+w[i]+W;
cnt[j]=cnt[t]+1;
if(cnt[j]>=n) return true;
if(!vis[j])
{
q.push(j);
vis[j]=true;
}
}
}
}
return false;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++) h[i]=-1;
for(int i=1;i<=m;i++)
{
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
add(b,d,log(1.0*c/a));
}
double l=0.0,r=1.0;
while(r-l>1e-8)
{
double mid=(l+r)/2;
if(spfa(mid)) r=mid;// If there is a positive ring, you need to increase the loss rate
else l=mid;
}
printf("%.7lf",l);
return 0;
}边栏推荐
- HTB-Arctic
- Mechanism and principle of multihead attention and masked attention
- Automatic usage in SystemVerilog
- HTB-Beep
- C编程 --“最大子数组的和” 的动态规划的解法
- background
- Switch NPM source to private source library
- HTB-Granpa
- Zhou Chen, vice president of zhanrui market, responded to everything about 5g chip chunteng 510!
- 50:第五章:开发admin管理服务:3:开发【查询admin用户名是否已存在,接口】;(这个接口需要登录时才能调用;所以我们编写了拦截器,让其拦截请求,判断用户是否是登录状态;)
猜你喜欢
![Atof(), atoi(), atol() functions [detailed]](/img/5a/a421eab897061c61467c272f122202.jpg)
Atof(), atoi(), atol() functions [detailed]

The difference between function and task in SystemVerilog

C Programming -- the solution of dynamic programming of "the sum of the largest subarray"

基于ISO13209(OTX)实现EOL下线序列

HTB-Arctic

50:第五章:开发admin管理服务:3:开发【查询admin用户名是否已存在,接口】;(这个接口需要登录时才能调用;所以我们编写了拦截器,让其拦截请求,判断用户是否是登录状态;)

Unity接入ChartAndGraph图表插件

Leetcode 202. 快乐数(一点都不快乐)

10、渲染基础

Microservice - hystrix fuse
随机推荐
HTB-Arctic
What are the ways to realize web digital visualization?
C100: smallest hevc visual IOT MCU
剑指 Offer 32 - I. 从上到下打印二叉树
Codeforces Round #809 (Div. 2)
Leetcode 237. delete nodes in the linked list
Programming hodgepodge (I)
求求你别再用 System.currentTimeMillis() 统计代码耗时了,真的太 Low 了!
Switch NPM source to private source library
Y76. Chapter IV Prometheus large factory monitoring system and practice -- Prometheus advanced (VII)
easyrecovery免费数据恢复工具操作简单一键恢复数据
idea常用10个快捷键
HTB-Granpa
Run length test of R language: use the runs.test function to perform run length test on binary sequence data (check whether the sequence is random)
Singing "Seven Mile fragrance" askew -- pay tribute to Jay
2020ICPC 江西省赛热身赛 E.Robot Sends Red Packets(dfs)
npx和npm区别
50: Chapter 5: develop admin management service: 3: develop [query whether the admin user name already exists, interface]; (this interface can only be called when logging in; so we have written an int
G1 garbage collector
Leetcode 237. 删除链表中的节点