Hongjun Likes Physics
Time limit2sMemory limit512 MB
Find the connected induced subgraph maximizing (sum of vertex weights)/(sum of edge weights) and print its density. A fractional programming problem solved by binary searching the ratio with a maximum closure computation.
- Level
Hard8 of 10
- Topics
- Graph, Binary search, Greedy
- Solved
- No attempts yet
Problem
Hongjun likes physics, and computing densities is his hobby.
After learning graph theory at school, Hongjun decided to define density on graphs as well. Take an undirected graph whose vertices and edges carry weights, let be the sum of the vertex weights and let be the sum of the edge weights. The density of that graph is .
For his birthday, Myungwoo gave Hongjun an undirected graph with weights on the vertices and on the edges. Hongjun wants the induced subgraph of largest density.
An induced subgraph of a graph satisfies the following conditions.
- The edge joining and belongs to if and only if , , and that edge belongs to .
- The weights of the vertices and edges of are the same as in .
- is connected.
An induced subgraph with no edge has , so its density is undefined and it is not a candidate.
Help Hongjun and compute the density of the induced subgraph of maximum density.
Input
The first line has the number of vertices and the number of edges . (, )
The second line has integers separated by spaces, the weight of the -th vertex. Every vertex weight is between and .
Each of the next lines has three integers , , , meaning that an edge of weight joins vertex and vertex . Every edge weight is between and , and no edge is given twice. The vertices are numbered from to .
Output
Print the density of the induced subgraph of maximum density, rounded to six digits after the decimal point.
No input places the exact answer exactly halfway between two six-digit decimals, so the rounding direction is always determined.