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& ...
随机推荐
- react-router4.x 实用例子(路由过渡动画、代码分割)
react-router4.2.0实用例子 代码分割 官网上面写的代码分割是不支持create-react-app脚手架的,要使用import实现 创建一个bundle.js文件 import { C ...
- eclipse下搭建Drools规则引擎环境
插件下载地址:http://download.jboss.org/drools/release/ 1.点开对应的版本文件,选择标红的两个压缩包下载,其他的如有需要也可以自行选择: 2.将下载的压缩包解 ...
- Linux 命令及获取帮助 目录文件浏览,管理和维护
开启Linux操作系统,要求以root用户登录GNOME图形界面,语言支持选择为汉语 使用快捷键切换到虚拟终端2,使用普通用户身份登录,查看系统提示符 $ 使用命令退出虚拟终端2上登录的用户 exit ...
- Android 4学习(7):用户界面 - 基础
参考<Professional Android 4 Development> Android UI基本元素 下面这些概念是Android UI设计的基础,深入学习和理解它们是Android ...
- Spring注解-TaskScheduler
一.定义配置类 import org.springframework.context.annotation.ComponentScan; import org.springframework.cont ...
- jQuery-图片的放大镜显示效果(不需要大小图) ,放大镜图层显示在图片左右侧,不适用table
放大镜图层显示在图片的一侧,但当图片嵌套到table里,放大镜图层位置就有误,此方法只适用于没有table 错误原因: 原来的写法是图片相对于Td 的位置,而不是图片的真实位置,所以两张图片的坐标是一 ...
- ROS探索总结(四)——简单的机器人仿真
前边我们已经介绍了ROS的基本情况,以及新手入门ROS的初级教程,现在就要真正的使用ROS进入机器人世界了.接下来我们涉及到的很多例程都是<ROS by Example>这本书的内容,我是 ...
- go语言的基本命令
go run命令: 用于运行命令源码文件 只能接受一个命令源码文件以及若干个库源码文件作为文件参数其内部操作是:先编译源码文件在执行 -v:列出被编译的代码包的名称 -work: 显示编译时创建的临时 ...
- Opengl创建几何实体——四棱锥和立方体
//#include <gl\glut.h>#include <GL\glut.h>#include <iostream> using namespace std; ...
- UIActionSheet(操作列表)
#import "AppDelegate.h" @interface AppDelegate ()<UIActionSheetDelegate> @end @imple ...