观光奶牛
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 using namespace std; 5 typedef double dd; 6 const int maxn=1007; 7 const int maxm=5007; 8 int n,m,num; 9 dd l,r,mid; 10 dd d[maxn]; 11 queue<int>q; 12 dd val[maxn]; 13 int cnt[maxn],head[maxn]; 14 bool vis[maxn]; 15 struct Edge{ 16 int next;int to;dd dis; 17 }edge[maxm]; 18 void add(int from,int to,dd dis){ 19 edge[++num].next=head[from]; 20 edge[num].to=to; 21 edge[num].dis=dis; 22 head[from]=num; 23 } 24 bool check(dd mid){ 25 for(int i=1;i<=n;i++){ 26 q.push(i);d[i]=0;cnt[i]=1;vis[i]=true; 27 } 28 while(!q.empty()){ 29 int u=q.front();q.pop();vis[u]=false; 30 for(int i=head[u];i;i=edge[i].next){ 31 int v=edge[i].to; 32 if(d[v]>d[u]+(dd)mid*(dd)edge[i].dis-(dd)val[u]){ 33 d[v]=d[u]+(dd)mid*(dd)edge[i].dis-(dd)val[u]; 34 if(!vis[v]){ 35 q.push(v);vis[v]=true; 36 if(++cnt[v]>=n) return 1; 37 } 38 } 39 } 40 } 41 return false; 42 } 43 int main(){ 44 cin>>n>>m; 45 for(int i=1;i<=n;i++) cin>>val[i]; 46 for(int i=1;i<=m;i++){ 47 int u,v,w;cin>>u>>v>>w; 48 add(u,v,w); 49 } 50 r=(dd)maxn*maxm; 51 while(r-l>=1e-4){ 52 mid=(double)(l+r)/2; 53 if(check(mid)) l=mid; 54 else r=mid; 55 } 56 printf("%.2lf",l); 57 return 0; 58 }