Buy or Build UVA - 1151 Kruskal+枚举
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 1000005
#define inf 0x3f3f3f3f
#define INF 9999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ ll qpow(ll a, ll b, ll c) {
ll ans = 1;
a = a % c;
while (b) {
if (b % 2)ans = ans * a%c;
b /= 2; a = a * a%c;
}
return ans;
}
int T;
int n, q;
int cost[maxn]; int fa[maxn];
int cnt;
vector<int>vc[maxn]; struct point {
int x, y;
}pint[maxn]; struct node {
int x, y;
int w;
}edge[maxn]; bool cmp(node a, node b) {
return a.w < b.w;
} int dis(int a, int b, int x, int y) {
return ((a - x)*(a - x) + (b - y)*(b - y));
} void init(int n) {
for (int i = 0; i <= n; i++)fa[i] = i;
} int findfa(int x) {
if (x == fa[x])return x;
return fa[x] = findfa(fa[x]);
} void Union(int p, int q) {
if (findfa(q) != findfa(p)) {
fa[findfa(p)] = findfa(q);
}
} int ans; int kruskal() {
int res = 0;
int ct = 0;
for (int i = 0; i < cnt; i++) {
int u = edge[i].x; int v = edge[i].y;
if (findfa(u) != findfa(v)) {
Union(u, v); res += edge[i].w;
ct++;
if (ct == n - 1)break;
}
}
return res;
} void sol() {
init(n);
ans = kruskal(); for (int i = 0; i < (1 << q); i++) {
init(n);int cst = 0;
for (int j = 0; j < q; j++) {
if (((i >> j) & 1)==0)continue;
cst += cost[j];
for (int k = 1; k < vc[j].size(); k++) {
Union(vc[j][k], vc[j][0]);
}
}
ans = min(ans, cst + kruskal());
}
printf("%d\n", ans);
} int main()
{
//ios::sync_with_stdio(0);
rdint(T); int kase = 1;
while (T--) {
if (kase > 1)printf("\n");
kase++;
rdint(n); rdint(q); cnt = 0;
for (int i = 0; i < 10; i++)vc[i].clear();
for (int i = 0; i < q; i++) {
int tmp; rdint(tmp); rdint(cost[i]);
while (tmp--) {
int x; rdint(x); vc[i].push_back(x);
}
} for (int i = 1; i <= n; i++) {
rdint(pint[i].x); rdint(pint[i].y);
}
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
edge[cnt].x = i; edge[cnt].y = j; edge[cnt].w = dis(pint[i].x, pint[i].y, pint[j].x, pint[j].y); cnt++;
}
}
sort(edge, edge + cnt, cmp);
sol(); } return 0;
}
Buy or Build UVA - 1151 Kruskal+枚举的更多相关文章
- UVA 1151二进制枚举子集 + 最小生成树
题意:平面上有n个点(1<=N<=1000),你的任务是让所有n个点连通,为此, 你可以新建一些边,费用等于两个端点的欧几里得距离的平方.另外还有q(0<=q<=8)个套餐(数 ...
- UVA 1151 Buy or Build (最小生成树)
先求出原图的最小生成树,然后枚举买哪些套餐,把一个套餐内的点相互之间边权为0,直接用并查集缩点.正确性是基于一个贪心, 在做Kruskal算法是,对于没有进入最小生成树的边,排序在它前面的边不会减少. ...
- POJ(2784)Buy or Build
Buy or Build Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1369 Accepted: 542 Descr ...
- Buy or Build (poj 2784 最小生成树)
Buy or Build Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1348 Accepted: 533 Descr ...
- UVA 1151 买还是建(最小生成树)
买还是建 紫书P358 [题目链接]买还是建 [题目类型]最小生成树 &题解: 这题真的心累,看了3天,最后照着码还是wa,先放lrj代码,以后再看吧 &代码: // UVa1151 ...
- UVA - 1151 Buy or Build (买还是建)(并查集+二进制枚举子集)
题意:平面上有n个点(1<=n<=1000),你的任务是让所有n个点连通.可以新建边,费用等于两端点欧几里德距离的平方.也可以购买套餐(套餐中的点全部连通).问最小费用. 分析: 1.先将 ...
- 【uva 1151】Buy or Build(图论--最小生成树+二进制枚举状态)
题意:平面上有N个点(1≤N≤1000),若要新建边,费用是2点的欧几里德距离的平方.另外还有Q个套餐,每个套餐里的点互相联通,总费用为Ci.问让所有N个点连通的最小费用.(2组数据的输出之间要求有换 ...
- UVa 1151 (枚举 + MST) Buy or Build
题意: 平面上有n个点,现在要把它们全部连通起来.现在有q个套餐,如果购买了第i个套餐,则这个套餐中的点全部连通起来.也可以自己单独地建一条边,费用为两点欧几里得距离的平方.求使所有点连通的最小费用. ...
- UVa 1151 - Buy or Build(最小生成树)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
随机推荐
- SQL中得到最后一个“-”后面的字符串
declare @str varchar(50) set @str='-'+'1-9-3-2'--前面加个'-'防止没有'-'出错 select REVERSE(SUBSTRING(REVERSE(@ ...
- [Codeforces]#179 div1-----295ABCDE
摘自我的github:https://github.com/Anoxxx The Solution Source: Codeforces Round #179 (Div. 1) VJudge链接: h ...
- CGContextRef详解
/* CoreGraphics - CGContext.h */ /** Graphics state functions. **/ //为了让开发者在进行坐标变换时无须计算多次坐标变换后的累加结果, ...
- Python 标准库 -> Pprint 模块 -> 用于打印 Python 数据结构
使用 pprint 模块 pprint 模块( pretty printer ) 用于打印 Python 数据结构. 当你在命令行下打印特定数据结构时你会发现它很有用(输出格式比较整齐, 便于阅读). ...
- select 动态添加option函数
转自:https://lym6520.iteye.com/blog/309937 经常会用到select动态添加元素,写了个方法,方便调用! ... /** * 功能:select对象动态添加Opt ...
- docker 笔记(3)第一个dockerfile
#vim Dockerfile FROM ubuntu RUN apt-get update && apt-get install -y vim #docker build -t ub ...
- ie6-ie8不支持opacity,rgba解决方法
半透明部分设置样式:opacity:0.7在ie9/ie10/ff/chrome/opera/safari显示正常. 但是这样在ie6-ie8中是不支持的,需要加上下面这句话: filter: pro ...
- day36-hibernate检索和优化 01-上次课内容回顾
clear():直接清空一级缓存的所有对象.evict(Object obj):清空一个对象.flush():控制你的刷出的时机.refresh(Object obj);将你的数据查出来把一级缓存覆盖 ...
- php apc 安装
APC简介 APC(Alternative PHP Cache)是一个PHP缓存.它在内存中存储PHP页面并且减少了硬盘的I/O.这对于性能的提升十分明显.你甚至可以在CPU使用率下降50%的情况下提 ...
- a标签中href=""的几种用法(转)
a标签中href=""的几种用法 标签: html / a标签 / javascript 46371 众所周知,a标签的最重要功能是实现超链接和锚点.而且,大多数人认为a标签最 ...