当前位置:网站首页>【集训DAY12】Minn ratio 【dfs】【最小生成树】
【集训DAY12】Minn ratio 【dfs】【最小生成树】
2022-07-25 22:24:00 【VL——MOESR】

思路:
我们直接暴力把要选的点dfs出来,然后做最小生成树就行了
c o d e code code
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n, m, a[20], fa[20];
int head[20], tot;
int p[20], an[20];
double ans = 1000000000.0;
bool v[20];
struct node {
int from, to, next, w;
}b[1000];
void add(int x, int y, int w) {
b[++ tot] = (node) {
x, y, head[x], w };
head[x] = tot;
}
bool cmp(node x, node y) {
return x.w < y.w;
}
int getfa(int x) {
if(x == fa[x]) return x;
return fa[x] = getfa(fa[x]);
}
double jis() {
double dian = 0;
for(int i = 1; i <= m; i ++) dian = dian + a[p[i]] * 1.0;
for(int i = 1; i <= n; i ++) fa[i] = i;
double bian = 0;
for(int i = 1; i <= tot; i ++) {
int x = b[i].from, y = b[i].to;
if(v[x] != 1 || v[y] != 1) continue;
int xx = getfa(x), yy = getfa(y);
if(xx == yy) continue;
fa[yy] = xx, bian = bian + b[i].w * 1.0;
}
return bian / dian;
}
void dfs(int x, int y) {
if(x > n && y == m) {
double sum = jis();
if(sum < ans) {
for(int i = 1; i <= m; i ++) an[i] = p[i];
ans = sum;
}
return;
}
if(x > n) return;
if(m - y > n - x + 1) return;
p[y + 1] = x, v[x] = 1;
dfs(x + 1, y + 1);
v[x] = 0;
dfs(x + 1, y);
}
int main() {
freopen("ratio.in", "r", stdin);
freopen("ratio.out", "w", stdout);
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i ++) scanf("%d", &a[i]);
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= n; j ++) {
int x;
scanf("%d", &x);
if(i != j) add(i, j, x);
}
sort(b + 1, b + 1 + tot, cmp);
dfs(1, 0);
for(int i = 1; i <= m; i ++) printf("%d ", an[i]);
return 0;
}
边栏推荐
- MapGIS格式转ArcGIS方法
- What have I experienced to become a harder tester than development?
- arcgis开发常用源码
- Wechat applet (anti shake, throttling), which solves the problem that users keep pulling down refresh requests or clicking buttons to submit information; Get the list information and refresh the data
- Google analyzes how UA can be transferred to the latest version of GA4
- Which is reliable between qiniu business school and WeiMiao business school? Is it safe to open an account recommended by the teacher?
- 点亮字符串中所有需要点亮的位置,至少需要点几盏灯
- 还不懂mock测试?一篇文章带你熟悉mock
- xss-工具-Beef-Xss安装以及使用
- Tfrecord write and read
猜你喜欢

Don't vote, software testing posts are saturated

数据平台下的数据治理

Randomly generate 10 (range 1~100) integers, save them to the array, and print the array in reverse order. And find the average value, the maximum value and the subscript of the maximum value, and fin

『Skywalking』. Net core fast access distributed link tracking platform

Playwright tutorial (I) suitable for Xiaobai

Advanced database · how to add random data for data that are not in all user data - Dragonfly Q system users without avatars how to add avatar data - elegant grass technology KIR

访问者模式(visitor)模式

How to resolve a domain name to multiple IP addresses?

4day

TS:typora代码片段缩进显示异常(已解决)-2022.7.24
随机推荐
4day
xss-工具-Beef-Xss安装以及使用
xxl-job中 关于所有日志系统的源码的解读(一行一行源码解读)
TFrecord写入与读取
Visitor mode
Imitation Tiktok homepage interface
Leetcode 106. construct binary tree from middle order and post order traversal sequence
Compile and decompile
internship:普通常用的工具类编写
Five constraints and three paradigms
『Skywalking』. Net core fast access distributed link tracking platform
C语言逆序打印字符串的两种方法
MapGIS格式转ArcGIS方法
How to call the size of two numbers with a function--- Xiao Tang
Xiaobai programmer's seventh day
对需求的内容进行jieba分词并按词频排序输出excel文档
3dslicer importing medical image data
IFLYTEK smart office book air e-book reader makes my work life healthier
SMART S7-200 PLC通道自由映射功能块(DO_Map)
Interpretation of the source code of all logging systems in XXL job (line by line source code interpretation)