UVA 1151 买还是建(最小生成树)
买还是建
紫书P358
【题目链接】买还是建
【题目类型】最小生成树
&题解:
这题真的心累,看了3天,最后照着码还是wa,先放lrj代码,以后再看吧
&代码:
// UVa1151 Buy or Build
// Rujia Liu
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 1000 + 10;
const int maxq = 8;
int n;
int x[maxn], y[maxn], cost[maxq];
vector<int> subn[maxq];
int pa[maxn];
int findset(int x) { return pa[x] != x ? pa[x] = findset(pa[x]) : x; }
struct Edge {
int u, v, d;
Edge(int u, int v, int d):u(u),v(v),d(d) {}
bool operator < (const Edge& rhs) const {
return d < rhs.d;
}
};
// initialize pa and sort e before calling this method
// cnt is the current number of components
int MST(int cnt, const vector<Edge>& e, vector<Edge>& used) {
if(cnt == 1) return 0;
int m = e.size();
int ans = 0;
used.clear();
for(int i = 0; i < m; i++) {
int u = findset(e[i].u), v = findset(e[i].v);
int d = e[i].d;
if(u != v) {
pa[u] = v;
ans += d;
used.push_back(e[i]);
if(--cnt == 1) break;
}
}
return ans;
}
int main() {
int T, q;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &q);
for(int i = 0; i < q; i++) {
int cnt;
scanf("%d%d", &cnt, &cost[i]);
subn[i].clear();
while(cnt--) {
int u;
scanf("%d", &u);
subn[i].push_back(u-1);
}
}
for(int i = 0; i < n; i++) scanf("%d%d", &x[i], &y[i]);
vector<Edge> e, need;
for(int i = 0; i < n; i++)
for(int j = i+1; j < n; j++) {
int c = (x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]);
e.push_back(Edge(i, j, c));
}
for(int i = 0; i < n; i++) pa[i] = i;
sort(e.begin(), e.end());
int ans = MST(n, e, need);
for(int mask = 0; mask < (1<<q); mask++) {
// union cities in the same sub-network
for(int i = 0; i < n; i++) pa[i] = i;
int cnt = n, c = 0;
for(int i = 0; i < q; i++) if(mask & (1<<i)) {
c += cost[i];
for(int j = 1; j < subn[i].size(); j++) {
int u = findset(subn[i][j]), v = findset(subn[i][0]);
if(u != v) { pa[u] = v; cnt--; }
}
}
vector<Edge> dummy;
ans = min(ans, c + MST(cnt, need, dummy));
}
printf("%d\n", ans);
if(T) printf("\n");
}
return 0;
}
UVA 1151 买还是建(最小生成树)的更多相关文章
- UVa 1151 买还是建
https://vjudge.net/problem/UVA-1151 题意: 平面上有n个点,你的任务是让所有n个点连通.为此,你可以新建一些边,费用等于两个端点的距离平方和.另外还有q个套餐可以购 ...
- UVA 1151二进制枚举子集 + 最小生成树
题意:平面上有n个点(1<=N<=1000),你的任务是让所有n个点连通,为此, 你可以新建一些边,费用等于两个端点的欧几里得距离的平方.另外还有q(0<=q<=8)个套餐(数 ...
- UVA 1151 Buy or Build (MST最小生成树,kruscal,变形)
题意: 要使n个点之间能够互通,要使两点直接互通需要耗费它们之间的欧几里得距离的平方大小的花费,这说明每两个点都可以使其互通.接着有q个套餐可以选,一旦选了这些套餐,他们所包含的点自动就连起来了,所需 ...
- UVA - 1151 Buy or Build (买还是建)(并查集+二进制枚举子集)
题意:平面上有n个点(1<=n<=1000),你的任务是让所有n个点连通.可以新建边,费用等于两端点欧几里德距离的平方.也可以购买套餐(套餐中的点全部连通).问最小费用. 分析: 1.先将 ...
- UVa 1151 - Buy or Build(最小生成树)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- uva 1151(最小生成树,枚举子集)
题意:平面上有n个点(1<=N<=1000),你的任务是让所有n个点连通,为此,你可以新建一些边,费用等于两个端点的欧几里得距离的平方.另外还有q(0<=q<=8)个套餐,可以 ...
- UVa 1151 Buy or Build (最小生成树+二进制法暴力求解)
题意:给定n个点,你的任务是让它们都连通.你可以新建一些边,费用等于两点距离的平方(当然越小越好),另外还有几种“套餐”,可以购买,你购买的话,那么有些边就可以连接起来, 每个“套餐”,也是要花费的, ...
- UVA 1151 Buy or Build (最小生成树)
先求出原图的最小生成树,然后枚举买哪些套餐,把一个套餐内的点相互之间边权为0,直接用并查集缩点.正确性是基于一个贪心, 在做Kruskal算法是,对于没有进入最小生成树的边,排序在它前面的边不会减少. ...
- 【UVA 1151】 Buy or Build (有某些特别的东东的最小生成树)
[题意] 平面上有n个点(1<=N<=1000),你的任务是让所有n个点连通,为此,你可以新建一些边,费用等于两个端点的欧几里得距离的平方. 另外还有q(0<=q<=8)个套餐 ...
随机推荐
- Baskets of Gold Coins_暴力
Problem Description You are given N baskets of gold coins. The baskets are numbered from 1 to N. In ...
- FreeBSD 配置
FreeBSD 配置 1. FreeBSD源代码下载站点:
- icmp_ping学习笔记
1.用字符串指针做为发送缓冲区和接收缓冲区的指针: 2.icmp报文类型结构体可自行定义,也可用<netinet/ip_icmp.h>中定义好的strcut icmp结构体: 3.ip_h ...
- 1.Counting DNA Nucleotides
Problem A string is simply an ordered collection of symbols selected from some alphabet and formed i ...
- js按钮浮动随手指方向移动而移动
window.document.getElementById("moveDIV").addEventListener("touchmove", function ...
- Tomcat的ISO-8859-1
Tomcat的默认编码时ISO8859-1,有些老工程,遗留项目很可能没改这块,这样写代码时如果传输中文,服务器收到的就可能是乱码. 昨天就被郁闷了1小时,左右都不通. 后来发现Android里的字符 ...
- NOIP2014提高组解方程
其实没有太难 但是不知道的话想不到 考场上大概有50分吧 #include <iostream> #include <stdio.h> #include <queue&g ...
- ios常见的页面传值方式
iOS页面间的传值细分有很多种,基本的传值方式有三种:委托Delegate传值.通知NSNotification传值.Block传值,其他在项目中可能会遇到的还有:UserDefault或文件方式传值 ...
- CSS背景色渐变
试了下 渐变色 ,主要确定开始位置 结束位置,以及对应的color-stop; 以下是兼容不同浏览器的代码片段 代码:<style type="text/css& ...
- Office word 2013中直接调用MathType的方法
Office word 2013中直接调用MathType的方法 | 浏览:4403 | 更新:2014-02-20 14:45 | 标签: word 使用Office word 2013的用户肯定早 ...