If there is a connected induced subgraph containing more than 2 nodes with the maximum density. The density of every connected induced subgraph of it that contains only one edge can be represented as , where u, v are the values of the two nodes linked by the edge. The density of the bigger connected induced subgraph is at most .
If , and for every edge, . Then we'll have u + v < Bc, and , and , it leads to contradiction.
So just check every single node, and every 2 nodes linked by an edge.
The time complexity is O(n + m).
#include<bits/stdc++.h>
using namespace std;
int x[1020];
int main()
{
int n,m,a,b,c;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&x[i]);
double ans=0;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
ans=max(ans,1.0*(x[a]+x[b])/c);
}
printf("%.15lf\n",ans);
return 0;
}