DZY loves Physics, and he enjoys calculating density.
Almost everything has density, even a graph. We define the density of a non-directed graph (nodes and edges of the graph have some values) as follows:
Once DZY got a graph G, now he wants to find a connected induced subgraph G" of the graph, such that the density of G' is as large as possible.
An induced subgraph G'(V',?E') of a graph G(V,?E) is a graph that satisfies:
-
; - edge
if and only if
, and edge
; - the value of an edge in G" is the same as the value of the corresponding edge in G, so as the value of a node.
Help DZY to find the induced subgraph with maximum density. Note that the induced subgraph you choose must be connected.
Input
The first line contains two space-separated integers n (1?≤?n?≤?500),
Output
. Integer n represents the number of nodes of the graph G, m represents the number of edges.<??http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+ClRoZSBzZWNvbmQgbGluZSBjb250YWlucyA8ZW0+bjwvZW0+IHNwYWNlLXNlcGFyYXRlZCBpbnRlZ2VycyA8ZW0+eDwvZW0+PGVtPmk8L2VtPiAoMT+h3D88ZW0+eDwvZW0+PGVtPmk8L2VtPj+h3D8xMDYpLAogd2hlcmUgPGVtPng8L2VtPjxlbT5pPC9lbT4gcmVwcmVzZW50cwogdGhlIHZhbHVlIG9mIHRoZSA8ZW0+aTwvZW0+LXRoIG5vZGUuIENvbnNpZGVyIHRoZSBncmFwaCBub2RlcyBhcmUgbnVtYmVyZWQgZnJvbSAxIHRvIDxlbT5uPC9lbT4uPC9wPgo8cD4KRWFjaCBvZiB0aGUgbmV4dCA8ZW0+bTwvZW0+IGxpbmVzIGNvbnRhaW5zIHRocmVlIHNwYWNlLXNlcGFyYXRlZCBpbnRlZ2VycyA8ZW0+YTwvZW0+PGVtPmk8L2VtPiw/PGVtPmI8L2VtPjxlbT5pPC9lbT4sPzxlbT5jPC9lbT48ZW0+aTwvZW0+ICgxP6HcPzxlbT5hPC9lbT48ZW0+aTwvZW0+Pzw/PGVtPmI8L2VtPjxlbT5pPC9lbT4/odw/PGVtPm48L2VtPjsgMT+h3D88ZW0+YzwvZW0+PGVtPmk8L2VtPj+h3D8xMDMpLAogZGVub3RpbmcgYW4gZWRnZSBiZXR3ZWVuIG5vZGUgPGVtPmE8L2VtPjxlbT5pPC9lbT4gYW5kIDxlbT5iPC9lbT48ZW0+aTwvZW0+IHdpdGgKIHZhbHVlIDxlbT5jPC9lbT48ZW0+aTwvZW0+LiBUaGUKIGdyYXBoIHdvbg=="t contain multiple edges.Output a real number denoting the answer, with an absolute or relative error of at most 10?-?9.
Sample test(s) input1 0 1
output0.000000000000000
input2 1 1 2 1 2 1
output3.000000000000000
input5 6 13 56 73 98 17 1 2 56 1 3 29 1 4 42 2 3 95 2 4 88 3 4 63
output2.965517241379311
NoteIn the first sample, you can only choose an empty subgraph, or the subgraph containing only node 1.
In the second sample, choosing the whole graph is optimal.
证明:必然存在一条边数≤1的最优解
假设存在最优解(G)ans最小边数>1,则点数>2
ans=∑vi/∑c
由假设知对G的子图,(u+v)/c
∴∑u+∑v
∴(∑u+∑v)<∑vi 矛盾
结论成立
所以只要判断所有的只取1条边,和不取的情况 O(m)
#include#include #include #include #include #include #include #include #include #include using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define Rep(i,n) for(int i=0;i =0;i--) #define Forp(x) for(int p=pre[x];p;p=next[p]) #define Lson (x<<1) #define Rson ((x<<1)+1) #define MEM(a) memset(a,0,sizeof(a)); #define MEMI(a) memset(a,127,sizeof(a)); #define MEMi(a) memset(a,128,sizeof(a)); #define INF (2139062143) #define F (100000007) #define MAXN (500+10) #define MAXM (MAXN*MAXN) #define MAXAi (1e6) #define MAXCi (1e3) long long mul(long long a,long long b){return (a*b)%F;} long long add(long long a,long long b){return (a+b)%F;} long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;} typedef long long ll; typedef long double ld; int n,m,a[MAXN]; ld ans=0.0; int main() { // freopen("Physics.in","r",stdin); scanf("%d%d",&n,&m); For(i,n) scanf("%d",&a[i]); For(i,m) { int u,v; double c; scanf("%d%d%lf",&u,&v,&c); ans=max(ans,(ld)(a[u]+a[v])/c); } cout<