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& ...
随机推荐
- UIView显示原理和过程
一.UIView显示原理 一个控件,UIView之所以可以显示,是因为内部在UIView的内部有一个layer属性作为根图层,根图层上可以放其他子图层,在UIView中所有能够看到的内 ...
- 部署和调优 3.2 dns安装配置-2
配置一个自定义的域,随便定义的,不实际存在. 在配置文件里,增加一个域 vim /etc/named.conf zone "123.com" IN { type master; f ...
- 关于JS正则表达式的一篇文章(转载)
原文:http://www.cnblogs.com/xujh/archive/2008/08/21/1273525.html <input onkeypress="return ...
- C语言学习笔记--内存操作常见错误
1. 野指针 (1)指针变量中的值是非法的内存地址,进而形成野指针 (2)野指针不是 NULL 指针,是指向不可用内存地址的指针 (3)NULL 指针并无危害,很好判断,也很好调试 (4)C 语言中无 ...
- VS2013 ERROR SCRIPT5009: “WebForm_AutoFocus”未定义
提示错误: <script type="text/javascript">//<![CDATA[WebForm_AutoFocus('txtcUserID');/ ...
- Consumer设计-high/low Level Consumer
1 Producer和Consumer的数据推送拉取方式 Producer Producer通过主动Push的方式将消息发布到Broker n Consumer Consumer通过Pull从Br ...
- Tensorflow学习练习-卷积神经网络应用于手写数字数据集训练
# coding: utf-8 import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data mn ...
- bzoj5450 轰炸
传送门 分析 不难想到如果这个图是一个DAG则答案就是图的最长路 于是我们考虑有环的情况 我们发现一个环上的所有点颜色一定不相同 于是我们发现答案就是缩点之后跑一遍点权最长路 点权就是这个强联通分量中 ...
- rest-framework组件 之 视图三部曲
浏览目录 使用混合(mixins) mixin类编写视图 使用通用的基于类的视图 viewsets.ModelViewSet 视图三部曲 使用混合(mixins) from rest_framewor ...
- Git 之 github分享代码
作为一个技术人员还是脱离不了屌丝的本质,所以每天都是逛逛github,看看别人有什么好的项目,自己可以给他挑挑bug也可以提供自己的水平,但是别人不那怎么才能给别人贡献代码呢?那就是fork了.... ...