当前位置:网站首页>【集训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;
}
边栏推荐
- Multi data source switching
- 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
- xxl-job中 关于所有日志系统的源码的解读(一行一行源码解读)
- How to call the size of two numbers with a function--- Xiao Tang
- (1) DDL, DML, DQL, DCL and common data types
- ORM common requirements
- Output Yang Hui triangle with two-dimensional array
- vim用法记录
- 【集训DAY15】简单计算【树状数组】【数学】
- PySpark数据分析基础:pyspark.sql.SparkSession类方法详解及操作+代码展示
猜你喜欢

IPv4 addresses have been completely exhausted, and the Internet can work normally. NAT is the greatest contributor!

Xiaobai programmer's seventh day

Google analyzes how UA can be transferred to the latest version of GA4

访问者模式(visitor)模式

Interpretation of the source code of all logging systems in XXL job (line by line source code interpretation)

『SignalR』. Net using signalr for real-time communication

ArcGIS10.2配置PostgreSQL9.2标准教程

Playwright tutorial (I) suitable for Xiaobai

什么是分区分桶?

Wechat official account application development (I)
随机推荐
Title: give a group of arrays, arranged from large to small and from small to large.
3 词法分析
访问者模式(visitor)模式
字符型常量和字符串常量的区别?
If jimureport building block report is integrated according to the framework
6-18 vulnerability exploitation - backdoor connection
Output Yang Hui triangle with two-dimensional array
MapGIS格式转ArcGIS方法
SQL基本语句 DQL select与提取 DML插入删除
xss-收集常用的代码
VIM usage record
SQL中in的用法 DQL 查询
聚名十年,说出你的故事,百万豪礼等你拿
synchronized与volatile
力矩电机控制基本原理
点亮字符串中所有需要点亮的位置,至少需要点几盏灯
Leetcode 106. 从中序与后序遍历序列构造二叉树
JS interview questions
Victoriametrics single node of kubernetes
Method of converting MAPGIS format to ArcGIS