求助
查看原帖
求助
400783
Nephren_Sakura楼主2022/3/15 17:25
#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
int head[1000005],tot,cnt,n=read(),m=read(),cntx[1000005];
bool vis[1000005],f[1000005];
double dis[1000005];
struct edge{
	int to,next;
	double w;
}a[1000005];
struct node{
	int x,y;
	double w;
}b[1000005];
void add(int x,int y,double w){
	tot++;
	a[tot].next=head[x];
	a[tot].to=y;
	a[tot].w=w;
	head[x]=tot;
	return;
}
bool spfa(double mid){
	tot=0;
	memset(head,0,sizeof(head));
	for(int i=1; i<=m; i++)
		add(b[i].x,b[i].y,b[i].w*mid-f[b[i].x]);
	queue<int> q;
	for(int i=1; i<=n; i++){
		q.push(i);
		vis[i]=true;
		dis[i]=0;
	}
	memset(cntx,0,sizeof(cntx));
	while(q.empty()==false){
		int x=q.front();
		q.pop();
		vis[x]=false;
		for(int i=head[x]; i; i=a[i].next){
			int nxt=a[i].to;
			if(dis[nxt]>dis[x]+a[i].w){
				dis[nxt]=dis[x]+a[i].w;
				if(vis[nxt]==false){
					vis[nxt]=true;
					q.push(nxt);
					cntx[nxt]++;
					if(cntx[nxt]>=n)
						return true;
				}
			}
		}
	}
	return false;
}
signed main(){
	for(int i=1; i<=n; i++)
		f[i]=read();
	for(int i=1; i<=m; i++){
		b[i].x=read();
		b[i].y=read();
		cin>>b[i].w;
	}
	double lt=0,rt=1000;
	while(lt+1e-6<rt){
		double mid=(lt+rt)/2.0;
		if(spfa(mid)==true)
			lt=mid;
		else
			rt=mid;
	}
	printf("%0.2lf",lt);
	return 0;
} 
2022/3/15 17:25
加载中...