#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int inf = 99999999;
const int M = 1000;
int vist[M];
int Map[M][M];
int node[M];
int g[M][M];
int dist[M];
int value[M];
int ans[M];
int n, m;
double MAX;
double Prime(int v) {
int minn, index, sum = 0;
for(int i = 1; i <= m; i++) {
dist[i] = g[v][i];
vist[i] = 0;
}
dist[v] = 0;
vist[v] = 1;
for(int i = 1; i < m; i++) {
minn = inf;
for(int j = 1; j <= m; j++) {
if(!vist[j] && dist[j] < minn) {
minn = dist[j];
index = j;
}
}
if(minn == inf)
break;
sum += minn;
vist[index] = 1;
for(int k = 1; k <= m; k++) { //切记习惯性思维。
if(dist[k] > g[index][k]) { //这里用的是加点操作,求最小生成树。
dist[k] = g[index][k];
}
}
}
return sum;
}
void dfs(int u, int cnt) {
node[cnt] = u;
if(cnt >= m) {
for(int i = 1; i <= m; i++) {
for(int j = 1; j <= m; j++) {
g[i][j] = Map[node[i]][node[j]];
}
}
double sumEdge=1.0*Prime(1); //每次从1搜索的原因是,对你各点进行了缩短,明白这点很重要,
double sumPoint=0;
for(int i = 1; i <= m; i++) {
sumPoint += value[node[i]];
}
double sum = sumEdge/sumPoint;
if(sum - MAX < -(1e-9)) { //对精度的要求, 很重要,
MAX = sum;
for(int i = 1; i <= m; i++) {
ans[i] = node[i];
}
}
return;
}
for(int i = u + 1; i <= n; i++) {
dfs(i, cnt+1);
}
}
int main()
{
while(scanf("%d%d", &n, &m) != EOF) {
if(n == 0 && m == 0)
break;
for(int i = 1; i <= n; i++) {
scanf("%d", &value[i]);
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
scanf("%d", &Map[i][j]);
}
}
MAX = inf*1.0;
for(int i = 1; i <= n; i++) {
dfs(i, 1); //枚举所有的点,
}
for(int i = 1; i < m; i++) {
printf("%d ", ans[i]);
}
printf("%d\n", ans[m]);
}
return 0;
}
hdu2489(重要) 列举n 个数中m个数(double型间的比较不是简单的等于号就可以
最新推荐文章于 2021-02-05 15:01:44 发布