CF 444C DZY Loves Physics(图论结论题)

题目链接: 传送门

DZY Loves Chemistry

time limit per test1 second     memory limit per test256 megabytes

Description

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:CF 444C DZY Loves Physics(图论结论题)
where v is the sum of the values of the nodes, e is the sum of the values of the edges.
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:

  • CF 444C DZY Loves Physics(图论结论题)
  • edge CF 444C DZY Loves Physics(图论结论题)if and only if CF 444C DZY Loves Physics(图论结论题)and edge CF 444C DZY Loves Physics(图论结论题)
  • 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), CF 444C DZY Loves Physics(图论结论题). Integer n represents the number of nodes of the graph G, m represents the number of edges.
The second line contains n space-separated integers xi (1 ≤ xi ≤ 10^6), where xi represents the value of the i-th node. Consider the graph nodes are numbered from 1 to n.
Each of the next m lines contains three space-separated integers ai, bi, ci (1 ≤ ai < bi ≤ n; 1 ≤ ci ≤ 10^3), denoting an edge between node ai and bi with value ci. The graph won't contain multiple edges.

Output

Output a real number denoting the answer, with an absolute or relative error of at most 10^ - 9.

Sample Input

1 0
1

2 1
1 2
1 2 1

5 6
13 56 73 98 17
1 2 56
1 3 29
1 4 42
2 3 95
2 4 88
3 4 63

Sample Output

0.000000000000000

3.000000000000000

2.965517241379311

思路:

题目大意:给出一张图,图中的每个节点,每条边都有一个权值,现在有从中挑出一张子图,要求子图联通,并且被选中的任意两点,如果存在边,则一定要被选中。问说点的权值和/边的权值和最大是多少。
可以证明,密度最大的子图一定只有两个点,再往里面加任何边,密度都会被拉低。
假设一个图现在有两个点点权为v1,v2,他们之间相连的边的边权为m1,该图的密度为(v1+v2)/m1。如果增加一个点v3要让该图的密度增加,若v3与v2相连的边的边权为m2。那么只有与v3/m2>(v1+v2)/m1,该图的密度才会增加。但是此时,v2与v3两个点构成的子图的密度为(v2+v3)/m2>(v1+v2+v3)/(m1+m2)。所以密度最大的子图一定只有两个点。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef __int64 LL;

int main()
{
    int N,M;
    while (~scanf("%d%d",&N,&M))
    {
        double ans[505] = {0};
        int u,v,val;
        double res = 0;
        for (int i = 1;i <= N;i++)
        {
            scanf("%lf",&ans[i]);
        }
        while (M--)
        {
            scanf("%d%d%d",&u,&v,&val);
            res = max (res,(ans[u]+ans[v])/val);
        }
        printf("%.15lf\n",res);
    }
    return 0;
} 
上一篇:Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序


下一篇:Spring基础学习(三)—详解Bean(下)