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)个套餐 ...
随机推荐
- iOS 架构模式MVVM
MVVM Model-View-ViewModelMVVM 其实是MVC的进化版,他将业务逻辑从VC中解耦到ViewModel,实现VC的瘦身. 做一个简单的登录判断: 创建LoginViewMode ...
- day04关于MySqL—Android小白的学习笔记
Mysql入门 1. 数据库基本知识(了解) 1.1.数据库介绍 1.1.1.什么是数据库?数据库的作用是什么? 数据库就是存储数据的仓库,其本质是一个文件系统,数据按照特定的格式将数据存储起来,用户 ...
- C#、js、json Datetime格式总结
在工作过程中遇到时间格式的数据在C#.js 和 json保存的不同结果,现在总结一下 JavaScript Parser: 1.数字型时间转字符串时间 如var data = "/Date( ...
- A - Ice_cream’s world III
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Pract ...
- 去掉win10桌面小图标
参考:http://bbs.kafan.cn/thread-1843802-1-1.html
- 分布式系统(Distributed System)资料
这个资料关于分布式系统资料,作者写的太好了.拿过来以备用 网址:https://github.com/ty4z2008/Qix/blob/master/ds.md 希望转载的朋友,你可以不用联系我.但 ...
- 从一个数组中提取出第start位到第end位
假设通过数组in来表示一个很大的数(in[0]表示最低bit),提取该数的第start位到第end位(计数起始位为0): #define MAX_BYTE_LEN ( 48 ) int getData ...
- RealProxy实现AOP编程(1)
Program.cs class Program { static void Main(string[] args) { User user = " }; var processor = T ...
- android颜色指列表
<?xml version="1.0" encoding="utf-8" ?> 2 <resources> 3 <color na ...
- iOS RSA加密解密及签名验证
1.首先要下载openssl,这个不用说,直接官网下载或者用brew install openssl下载 2.终端生成私钥密钥 2.1生成私钥 openssl genrsa - 2.2生成密钥 ope ...