HDU 2489 Minimal Ratio Tree DFS enumeration point + minimum spanning tree belongs to the middle of the problem, double compare the size of time attention to accuracy

Source: Internet
Author: User

Minimal Ratio TreeTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2835 Accepted Submission (s): 841


Problem descriptionfor a tree, which nodes and edges are all weighted, the ratio of it is calculated according to the foll Owing equation.




Given a complete graph of n nodes with all nodes and edges weighted, your task was to find a tree, which is a sub-graph of The original graph, with m nodes and whose ratio are the smallest among all the trees of M nodes in the graph.

Inputinput contains multiple test cases. The first line of all test case contains-integers n (2<=n<=15) and M (2<=m<=n), which stands for the numb Er of nodes in the graph and the number of nodes in the minimal ratio tree. Zeros end the input. The next line contains n numbers which stand for the weight of each node. The following n lines contain a diagonally symmetrical NXN connectivity matrix with each element shows the weight of the E Dge connecting one node with another. Of course, the diagonal would be all 0, since there are no edge connecting a node with itself.



All the weights of both nodes and edges (except for the ones on the diagonal of the matrix) is integers and in the range of [1, 100].

The figure below illustrates the first test case in sample input. Node 1 and Node 3 form the minimal ratio tree.


Outputfor each test case output one line contains a sequence of the M nodes which constructs the minimal ratio tree. Nodes should is arranged in ascending order. If There is several such sequences, 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 is numbered from 1.
Sample Input
3 230 20 100 6 26 0 32 3 02 21 10 22 00 0

Sample Output
1 31 2

Reprint Please declare: http://blog.csdn.net/lionel_d
When writing code, always make SB's mistake!!! Cause wrong for a long time!! Always thought it was the wrong precision, O (╯-╰) O first enumerate m points, ask this m-point of the smallest spanning tree,, can,, at first I was the first to seek n points of the smallest spanning tree, and then enumerate m points from the tree,, the results are always wrong, and then look at other people's solution, only to understand their thinking wrong is very serious AH!!!
Look at the code:
#include <cstdio> #include <cstring> #include <algorithm> #define MAX 20#define INF 100000000using namespace std; int Graph[max][max], Lowcost[max], Node[max], Path[max], Temp[max];int N, m; bool CMP (const int A, con St int b) {return a<b;} int prim (int s) {bool Visited[max]; memset (visited,false,sizeof (visited)); int sum = 0; for (int i = 0; i < m; ++i) {Low Cost[temp[i]] = Graph[s][temp[i]];} Visited[s] = true; for (int i = 0; i < m-1; ++i) {int min = INF, index =-1; for (int j = 0; j < m; ++j) {if (!visit ED[TEMP[J]] && lowcost[temp[j]]<min) {min = lowcost[temp[j]]; index = Temp[j];}} if (index = =-1) {break;} Visited[index] = true; sum + = min; for (int j = 0; j < m; ++j) {if (!visited[temp[j]] && Lowcost[temp[j]]>gra PH[INDEX][TEMP[J]]) {Lowcost[temp[j]] = Graph[index][temp[j];}}} return sum;} Double ans = inf*1.0; void DFS (int num, int count) {if (count = = m) {int sumofedge = Prim (temp[0]), Sumofnode = 0;for (int i = 0; I < m; ++i){Sumofnode + = Node[temp[i]];} Double T = sumofedge*1.0/sumofnode, if (ans-t>0.00001) {ans = t; for (int i = 0; i < m; ++i) {path[i] = Temp[i];}} return;} for (int i = num+1; I <= n; ++i) {Temp[count] = i;D FS (i,count+1);}} int main () {while (~scanf ("%d%d", &n,&m) && (m| | N) {for (int i = 1; I <= n; ++i) {scanf ("%d", &node[i]);} for (int i = 1; I <= n; ++i) {for (int j = 1; j <= N; ++j) {scanf ("%d", &graph[i][j]);} Graph[i][i] = INF;} ans = inf*1.0; for (int i = 1; I <= n; ++i) {temp[0] = i;D FS (i,1);} Sort (path,path+m,cmp); for (int i = 0; i < m; ++i) {printf ("%d", path[i]), if (i! = m-1) {printf ("");}} Puts ("");} return 0;}

with June

HDU 2489 Minimal Ratio Tree DFS enumeration point + minimum spanning tree belongs to the middle of the problem, double compare the size of time attention to accuracy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.