Undirected graph density

Topic link: http://www.bjfuacm.com/contest/115/problem/568/

                                      Undirected graph of small A

               Published: April 24, 2018 15:24 Last update: April 24, 2018 17:23 Time limit: 1000ms Memory limit: 128M

Xiao A has recently been working on graph problems, such as the density of an undirected graph. He defines the density of an undirected graph with weights for both points and edges as:

image.png

v represents the sum of the weights of the points, and e represents the sum of the weights of the edges.

One day, little A gets an undirected graph G, and he wants to find a subgraph G' in this graph G, so that the density of the subgraph G' is the largest.

Graph G'(V',E') is a subgraph of graph G(V,E) that satisfies the following conditions:

1. V' is contained in V;

2. If a, b ∈ V', and side (a, b) ∈ E, then side (a, b) ∈ E';

3. The edge weights and point weights of the graph G' will not change.

Help A to find the subgraph G' with the highest density.

image.png

The input contains multiple groups. The first line contains two two integers n, m (1<=n<=500, 0<=m<=n×(n-1)/2). n represents the number of points in the graph G, and m represents the edge quantity.
The next line contains n integers xi (1<=xi<=10^6), representing the weight of the i-th point. The points of the graph are numbered from 1 to n.
Next contains m lines, including ai, bi, ci (1<=ai<bi<=n; 1<=ci<=10^3), ai and bi represent the edge (ai,bi), and ci represents this edge weight value. The graph will not have duplicate edges.

Output the density of the densest subgraph. The result is rounded to two decimal places.

1 0
1
0.00
2 1
1 2
1 2 1
3.00
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
2.97

 

#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int val[510],i;
        for(i=1;i<=n;i++)
            scanf("%d",&val[i]);
        float res=0;
        for(i=0;i<m;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            res=max(res,(float)((val[a]+val[b])*1.0/c));
        }
        printf("%.2f\n",res);
    }
    return 0;
}

 

2018-04-25

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324936061&siteId=291194637