CF 444A(DZY Loves Physics-低密度脂蛋白诱导子图)
2015-09-23 20:56
405 查看
A. DZY Loves Physics
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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:
![](http://img.blog.csdn.net/20140709220708672)
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:
![](http://img.blog.csdn.net/20140709220719748)
;
edge
![](http://img.blog.csdn.net/20140709220723523)
if and only if
![](http://img.blog.csdn.net/20140709220727298)
, and edge
![](http://img.blog.csdn.net/20140709220730512)
;
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.
![](http://img.blog.csdn.net/20140709220714148)
Input
The first line contains two space-separated integers n (1 ≤ n ≤ 500),
![](http://img.blog.csdn.net/20140709220735301)
. 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 ≤ 106),
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 ≤ 103),
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 test(s)
input
output
input
output
input
output
Note
In 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<ans ,(u+v)<ans*c
∴∑u+∑v<ans*∑c ,(∑u+∑v)/∑c<ans=∑vi/∑c
∴(∑u+∑v)<∑vi 矛盾
结论成立
所以仅仅要推断全部的仅仅取1条边,和不取的情况 O(m)
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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:
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:
;
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),
. 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 ≤ 106),
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 ≤ 103),
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 test(s)
input
1 0 1
output
0.000000000000000
input
2 1 1 2 1 2 1
output
3.000000000000000
input
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
output
2.965517241379311
Note
In 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<ans ,(u+v)<ans*c
∴∑u+∑v<ans*∑c ,(∑u+∑v)/∑c<ans=∑vi/∑c
∴(∑u+∑v)<∑vi 矛盾
结论成立
所以仅仅要推断全部的仅仅取1条边,和不取的情况 O(m)
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<functional> #include<iostream> #include<cmath> #include<cctype> #include<ctime> #include<iomanip> 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<n;i++) #define ForD(i,n) for(int i=n;i;i--) #define RepD(i,n) for(int i=n;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<<setiosflags(ios::fixed)<<setprecision(100)<<ans; return 0; }
相关文章推荐
- java02
- in App Purchases一个注意事项
- ActiveMQ - message cursor
- 读取Json文件为String字符串
- Java——随机计数器
- 大道至简第二章读后感
- html5 构造网页的新方式
- 安卓几种界面布局的详解
- 交叉线还是直通线?
- UVA 1356 Bridge 自适应辛普森
- rt-thread学习笔记(二)-LED测试程序
- Android基础学习之SQLiteHelper(数据库帮助类)
- 逆袭系列日志说明-20150923
- 自定义TabBarController
- ASP.NET Session的共享
- java01
- PCB元件快速分类排列方法
- 我的Android之旅——学习、项目、心态
- linux网卡做bond的7种模式
- 【6】和作为连续序列s