洛谷P2868

未A

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<queue>
 5 #include<cstdlib>
 6 #include<cmath>
 7 
 8 using namespace std;
 9 const int N=1001;
10 
11 struct node{
12     int u,v,w,nxt;
13 };
14 node e[N];
15 
16 int now=1;
17 int head[N];
18 int dis[N];
19 int vis[N];
20 double hap[N];
21 double ans=0.0;
22 int n,m;
23 queue<int>q;
24 
25 void add(int u,int v,int w)
26 {
27     e[now].u=u;
28     e[now].v=v;
29     e[now].w=w;
30     e[now].nxt=head[u];
31     head[u]=now++;
32 }
33 
34 void spfa(int a)
35 {
36     q.push(a);
37     vis[a]=1;
38     for(int i=2;i<=n;i++)
39     {
40         dis[i]=999999;
41     }
42     int ss=q.empty();
43     while(!q.empty())
44     {
45         int top=q.front();
46         q.pop();
47         vis[top]=0;
48         for(int i=head[top];i!=-1;i=e[i].nxt)
49         {
50             int v=e[i].v;
51             int w=e[i].w;
52             double happ=(hap[top]+hap[v])/(double)w;
53             if(happ>ans)
54             {
55                 ans=happ;
56                 if(vis[v]==0)
57                 {
58                     vis[v]=1;
59                     q.push(v);
60                 }
61             }
62             else 
63             {
64                 printf("%.2lf",(ans));
65                 exit(0);
66             }
67         }
68     }
69 }
70 
71 int main()
72 {
73     cin>>n>>m;
74     for(int i=1;i<=n;i++)
75     {
76         head[i]=-1;
77     }
78     for(int i=1;i<=n;i++)
79     {
80         cin>>hap[i];
81     }
82     for(int i=1;i<=m;i++)
83     {
84         int u,v,w;
85         cin>>u>>v>>w;
86         add(u,v,2*w);
87     }
88     spfa(1);
89 }
原文地址:https://www.cnblogs.com/lyqlyq/p/6798587.html