poj_3925 Minimal Ratio Tree
来源:互联网 发布:淘宝订单如何申请退款 编辑:程序博客网 时间:2024/05/16 07:30
Minimal Ratio Tree
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 836
Accepted: 304
题目链接:http://poj.org/problem?id=3925
Description
For a tree, which nodes and edges are all weighted, the ratio of it iscalculated according to the following equation.
Given a complete graph of n nodes with all nodes and edges weighted, your taskis to find a tree, which is a sub-graph of the original graph, with m nodes andwhose ratio is the smallest among all the trees of m nodes in the graph.
Input
Input contains multiple test cases. The first line of each test casecontains two integers n (2<=n<=15) and m (2<=m<=n), which standsfor the number of nodes in the graph and the number of nodes in the minimalratio tree. Two zeros end the input. The next line contains n numbers whichstand for the weight of each node. The following n lines contain a diagonallysymmetrical n×n connectivity matrix with each element shows the weight of theedge connecting one node with another. Of course, the diagonal will be all 0,since there is no edge connecting a node with itself.
All the weights of both nodes and edges (except for the ones on the diagonal ofthe matrix) are integers and in the range of [1, 100].
The figure below illustrates the first test case in sample input. Node 1 andNode 3 form the minimal ratio tree.
Output
For each test case output oneline contains a sequence of the m nodes which constructs the minimal ratiotree. Nodes should be arranged in ascending order. If there are several suchsequences, pick the one which has the smallest node number; if there's a tie,look at the second smallest node number, etc. Please note that the nodes arenumbered from 1.
Sample Input
3 2
30 20 10
0 6 2
6 0 3
2 3 0
2 2
1 1
0 2
2 0
0 0
Sample Output
1 3
1 2
Source
Beijing 2008
题意:
给你一个完全图,求其中m个点中的radio最小radio=(每条边的权值之和)/(每个点的权值之和)
解题思路:
我是先求出n个点中m个点的每一种组合,在每一种组合中的点,在用prim算法求这些点的最小生成树的值,再计算该组合的radio值,并保存最小的radio对应的点,最后输出每个点
代码:
#include <iostream>#include<cstdio>#include<cstring>#define maxn 100#define VALUE 0xffffffusing namespace std;struct edgeRadio{int member[maxn];int t;};int noteweight[maxn];//点权int edgeweight[maxn][maxn];//边权edgeRadio edgeradio;int n,m;int visited[maxn];int minCost[maxn];double minValue;edgeRadio minEdge;//prim算法求最小值int prim(){int i;int res=0;for(i=0;i<=n;i++){visited[i]=0;minCost[i]=VALUE;}minCost[edgeradio.member[0]]=0;while(true){int t=-1;for(i=1;i<=m;i++){if(visited[edgeradio.member[i-1]]==0 && (t==-1 || minCost[edgeradio.member[i-1]]<minCost[t]))t=edgeradio.member[i-1];}if(t==-1)break;visited[t]=1;res+=minCost[t];for(i=1;i<=m;i++){if(minCost[edgeradio.member[i-1]]>edgeweight[edgeradio.member[i-1]][t] && edgeweight[edgeradio.member[i-1]][t]!=0){minCost[edgeradio.member[i-1]]=edgeweight[edgeradio.member[i-1]][t];}}}return res;}//求radiodouble getRadio(){int num=0;int res=prim();for(int i=1;i<=m;i++){num+=noteweight[edgeradio.member[i-1]];}return res*1.0/num;}//递归求每一种组合void getNum(int t,int p){if(t==0){double res = getRadio();if(minValue>res){minValue=res;minEdge=edgeradio;}return ;}else{for(int i=p;i<=n;i++){edgeradio.member[edgeradio.t]=i;edgeradio.t++;getNum(t-1,p+1);edgeradio.t--;p++;}}}int main(){ int i,j; while(true) { scanf("%d%d",&n,&m); minValue=VALUE; if(n==0 && m==0) break;memset(edgeweight,0,sizeof(edgeweight)); for(i=1;i<=n;i++) { scanf("%d",¬eweight[i]); } for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { scanf("%d",&edgeweight[i][j]); } }getNum(m,1);for(i=0;i<minEdge.t-1;i++){printf("%d ",minEdge.member[i]);}printf("%d\n",minEdge.member[i]); } return 0;}
- poj_3925 Minimal Ratio Tree
- POJ3925 Minimal Ratio Tree
- HDU Minimal Ratio Tree
- hdu2489 Minimal Ratio Tree
- HDU2489 Minimal Ratio Tree
- E - Minimal Ratio Tree
- HDU2489-Minimal Ratio Tree
- Minimal Ratio Tree
- pku hdu Minimal Ratio Tree
- hdoj 2489-Minimal Ratio Tree
- HDU 2489 Minimal Ratio Tree
- POJ 3925 Minimal Ratio Tree
- POJ 3925 Minimal Ratio Tree
- hdu 2489 Minimal Ratio Tree
- HDU 2489 Minimal Ratio Tree
- HDU-2489-Minimal Ratio Tree
- HDU 2489 Minimal Ratio Tree
- HDU 2489:Minimal Ratio Tree
- POJ3625—最小生成树+heap的实现
- xocde4.3 如何手动安装iPhone simulator 4.3/5.0
- Java_各种Exception解析
- 详细介绍Java的内存管理与内存泄露
- 常用软件介绍
- poj_3925 Minimal Ratio Tree
- Bugzilla各种状态详细解释
- HDU 1010 Tempter of the Bone (DFS)
- easerver如何发布web程序
- 软件工程师在职业生涯中说的32句话
- 数据库三范式(上)
- 校门外的树 数据加强版
- Nagle算法, TCP_CORK,TCP_NODELAY,TCP_DEFER_ACCEPT,TCP_QUICKACK
- redis收集整理