hdu 2489 Minimal Ratio Tree (DFS枚举+MST)

参考链接:http://blog.csdn.net/xingyeyongheng/article/details/9373271

     http://www.cnblogs.com/chenxiwenruo/p/3294668.html

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<string>
 6 #include<queue>
 7 #include<algorithm>
 8 #include<map>
 9 #include<iomanip>
10 #define INF 99999999
11 using namespace std;
12
13 const int MAX = 15 + 5;
14 int edge[MAX][MAX], dist[MAX], node[MAX];//node记录最终选的点
15 int vale[MAX], temp[MAX], n, m;//temp记录选的m个点
16 double minratio;
17 bool mark[MAX];
18
19 int Prim(int s){
20     int sum = 0;
21     for (int i = 1; i <= m; ++i)mark[temp[i]] = false, dist[temp[i]] = edge[s][temp[i]];
22     mark[s] = true;
23     dist[s] = 0;
24     for (int i = 1; i<m; ++i){
25         int point = s;
26         for (int j = 1; j <= m; ++j){
27             if (point == s && !mark[temp[j]])point = temp[j];
28             if (!mark[temp[j]] && dist[point]>dist[temp[j]])point = temp[j];
29         }
30         mark[point] = true;
31         sum += dist[point];
32         for (int j = 1; j <= m; ++j){
33             if (!mark[temp[j]] && edge[point][temp[j]]<dist[temp[j]])dist[temp[j]] = edge[point][temp[j]];
34         }
35     }
36     return sum;
37 }
38
39 void dfs(int u, int num){
40     if (num == m){
41         int ans = 0;
42         for (int i = 1; i <= m; ++i)ans += vale[temp[i]];
43         double sum = Prim(u)*1.0 / ans;
44         if (sum - minratio < -(1e-9)){
45             minratio = sum;
46             for (int i = 1; i <= m; ++i) node[i] = temp[i];
47         }
48         return;
49     }
50     if (n - u + num<m)return;
51     for (int i = u + 1; i <= n; ++i){
52         temp[num + 1] = i;
53         dfs(i, num + 1);
54     }
55 }
56
57 int main(){
58     while (cin >> n >> m, n + m){
59         minratio = INF*1.0;
60         for (int i = 1; i <= n; ++i)cin >> vale[i];
61         for (int i = 1; i <= n; ++i){
62             for (int j = 1; j <= n; ++j){
63                 cin >> edge[i][j];
64             }
65         }
66         for (int i = 1; i <= n; ++i){
67             temp[1] = i;
68             dfs(i, 1);
69         }
70         for (int i = 1; i<m; ++i)cout << node[i] << ‘ ‘;
71         cout << node[m] << endl;
72     }
73     return 0;
74 }

别的大牛DFS写法

 1 //调用时:dfs(1,0,0);
 2
 3 //dep表示点的编号,cnt表示选取的点的个数,sum_pw表示目前选取了cnt个点后总的点权值
 4 void dfs(int dep, int cnt, int sum_pw) {
 5     if(cnt == m) {
 6         ...;
 7         return ;
 8     }
 9     if(dep == n + 1) return ;
10     use[dep] = true;  //选取点dep,这里use[i]=true表示选取点i,在用prim求最小生成树的时候有用
11     dfs(dep + 1, cnt + 1, sum_pw + weight[dep]);
12     use[dep] = false; //不选取点dep
13     dfs(dep + 1, cnt, sum_pw);
14 }
时间: 2024-08-11 01:22:29

hdu 2489 Minimal Ratio Tree (DFS枚举+MST)的相关文章

hdu 2489 Minimal Ratio Tree(dfs枚举 + 最小生成树)~~~

题目: 链接:点击打开链接 题意: 输入n个点,要求选m个点满足连接m个点的m-1条边权值和sum与点的权值和ans使得sum/ans最小,并输出所选的m个点,如果有多种情况就选第一个点最小的,如果第一个点也相同就选第二个点最小的........ 思路: 求一个图中的一颗子树,使得Sum(edge weight)/Sum(point weight)最小~ 数据量小,暴力枚举~~~~~dfs暴力枚举C(M,N)种情况. 枚举出这M个点之后,Sum(point weight)固定,进行prim或者K

hdu 2489 Minimal Ratio Tree DFS枚举点+最小生成树 属于中等偏上题 ,Double比较大小的时候注意精度问题

Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2835    Accepted Submission(s): 841 Problem Description For a tree, which nodes and edges are all weighted, the ratio of it is

HDU 2489 Minimal Ratio Tree (DFS枚举+最小生成树Prim)

Problem Description For a tree, which nodes and edges are all weighted, the ratio of it is calculated according to the following equation. Given a complete graph of n nodes with all nodes and edges weighted, your task is to find a tree, which is a su

hdu - 2489 - Minimal Ratio Tree(枚举 + MST)

题意:给出一个图 n x n (2<=n<=15)的图,每个点,每条边都有权值,求其中的 m (2<=m<=n)个点,使得这m个点生成的树的边点权比例最小. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2489 -->>数量小,于是,可以枚举取 m 个点的所有情况,对每种情况最一次MST,更新最小值.. 时间复杂度:O(n ^ n * log(n) * 2 ^ n) #include <cstdio> #in

HDU 2489 Minimal Ratio Tree (dfs+Prim最小生成树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2489 Problem Description For a tree, which nodes and edges are all weighted, the ratio of it is calculated according to the following equation. Given a complete graph of n nodes with all nodes and edges

hdu 2489 Minimal Ratio Tree 枚举+最小生成树

点的总数很小,直接枚举就好. #include <stdio.h> #include <string.h> #define N 20 #define inf 1000000 int mk[N],n,k,ans[N]; double low[N],val[N]; double map[N][N],MIN; double prim() { int i,j; double sum=0; double tot=0; for(i=1;i<=n;i++) low[i]=inf; int

HDU 2489 Minimal Ratio Tree(数据结构-最小生成树)

Minimal Ratio Tree Problem Description For a tree, which nodes and edges are all weighted, the ratio of it is calculated according to the following equation. Given a complete graph of n nodes with all nodes and edges weighted, your task is to find a

HDU 2489 Minimal Ratio Tree(prim+DFS)

Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3345    Accepted Submission(s): 1019 Problem Description For a tree, which nodes and edges are all weighted, the ratio of it i

HDU 2489 Minimal Ratio Tree

Minimal Ratio Tree Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 248964-bit integer IO format: %I64d      Java class name: Maina For a tree, which nodes and edges are all weighted, the ratio of it is calcul