Poj(2784),二进制枚举最小生成树
题目链接:http://poj.org/problem?id=2784
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 1528 | Accepted: 592 |
Description
Problem
There are several local companies running small networks (called
subnetworks in the following) that partially cover the n largest cities
of Borduria. WWN would like to setup a network that connects all n
cities. To achieve this, it can either build edges between cities from
scratch or it can buy one or several subnetworks from local companies.
You are requested to help WWN to decide how to setup its network for a
minimal total cost.
- All n cities are located by their two-dimensional Cartesian coordinates.
- There are q existing subnetworks. If q>=1 then each
subnetwork c ( 1<=c<=q ) is defined by a set of interconnected
cities (the exact shape of a subnetwork is not relevant to our problem). - A subnetwork c can be bought for a total cost wc and it cannot be split (i.e., the network cannot be fractioned).
- To connect two cities that are not connected through the
subnetworks bought, WWN has to build an edge whose cost is exactly the
square of the Euclidean distance between the cities.
You have to decide which existing networks you buy and which edges
you setup so that the total cost is minimal. Note that the number of
existing networks is always very small (typically smaller than 8).
A 115 Cities Instance
Consider a 115 cities instance of the problem with 4 subnetworks
(the 4 first graphs in Figure 1). As mentioned earlier the exact shape
of a subnetwork is not relevant still, to keep figures easy to read, we
have assumed an arbitrary tree like structure for each subnetworks. The
bottom network in Figure 1 corresponds to the solution in which the
first and the third networks have been bought. Thin edges correspond to
edges build from scratch while thick edges are those from one of the
initial networks.
Input
first line contains the number n of cities in the country (
1<=n<=1000 ) followed by the number q of existing subnetworks (
0<=q<=8 ). Cities are identified by a unique integer value ranging
from 1 to n . The first line is followed by q lines (one per
subnetwork), all of them following the same pattern: The first integer
is the number of cities in the subnetwork. The second integer is the the
cost of the subnetwork (not greater than 2 x 106 ). The
remaining integers on the line (as many as the number of cities in the
subnetwork) are the identifiers of the cities in the subnetwork. The
last part of the file contains n lines that provide the coordinates of
the cities (city 1 on the first line, city 2 on the second one, etc).
Each line is made of 2 integer values (ranging from 0 to 3000)
corresponding to the integer coordinates of the city.
Output
Sample Input
7 3
2 4 1 2
3 3 3 6 7
3 9 2 4 5
0 2
4 0
2 0
4 2
1 3
0 5
4 4
Sample Output
17
Hint


Figure 3: An optimal solution of the 7 City instance in which which
the first and second existing networkshave been bought while two extra
edges (1, 5) and (2, 4)
Source
#include <stdio.h>
#include <vector>
#include <algorithm> using namespace std; #define MAXN 1005 struct Edge
{
int u,v;
int w;
bool operator < (const Edge a) const
{
return w<a.w;
}
}edge[MAXN*MAXN]; int n,m,q;
vector<int> v[];
int cost[];
int lx[MAXN];
int ly[MAXN];
int father[MAXN]; int Find_Set (int x)
{
if(x!=father[x])
father[x] = Find_Set(father[x]);
return father[x];
} int MST()
{
int ans = ;
int k = ;
for(int i=;i<m;i++)
{
int fx = Find_Set(edge[i].u);
int fy = Find_Set(edge[i].v);
if(fx!=fy)
{
father[fx] = fy;
ans +=edge[i].w;
k++;
}
if(k==n-) break;
}
return ans;
} int main()
{
scanf("%d%d",&n,&q);
for(int i=; i<q; i++)
{
v[i].clear();
int t;
scanf("%d%d",&t,&cost[i]);
for(int j=; j<t; j++)
{
int to;
scanf("%d",&to);
v[i].push_back(to);
}
}
for(int i=; i<=n; i++)
scanf("%d%d",&lx[i],&ly[i]); m = ;
for(int i=; i<=n; i++)
for(int j=i+; j<=n; j++)
{
edge[m].u = i;
edge[m].v = j;
edge[m++].w = (lx[i]-lx[j])*(lx[i]-lx[j])+(ly[i]-ly[j])*(ly[i]-ly[j]);
}
sort(edge,edge+m); for(int i=; i<=n; i++)
father[i] = i; int ans = MST();
//printf("%d\n",ans); for(int mark=; mark<(<<q); mark++)
{
for(int i=;i<=n;i++)
father[i] = i; int c = ;
for(int i=; i<q; i++)
{
if(mark&(<<i))
{
c+=cost[i];
for(int k=;k<v[i].size();k++)
{
int fx = Find_Set(v[i][k]);
int fy = Find_Set(v[i][]);
if(fx!=fy)
father[fy] = fx;
}
}
}
ans = min(ans,c+MST());
}
printf("%d\n",ans);
return ;
}
Poj(2784),二进制枚举最小生成树的更多相关文章
- POJ 2436 二进制枚举+位运算
题意:给出n头牛的得病的种类情况,一共有m种病,要求找出最多有K种病的牛的数目: 思路:二进制枚举(得病处为1,否则为0,比如得了2 1两种病,代号就是011(十进制就是3)),首先枚举出1的个数等于 ...
- POJ 2436 二进制枚举
题意: 思路: 拆成二进制枚举 有哪个病毒在 判一判 就好了 //By SiriusRen #include <cstdio> #include <cstring> #incl ...
- UVA 1151二进制枚举子集 + 最小生成树
题意:平面上有n个点(1<=N<=1000),你的任务是让所有n个点连通,为此, 你可以新建一些边,费用等于两个端点的欧几里得距离的平方.另外还有q(0<=q<=8)个套餐(数 ...
- POJ.3279 Fliptile (搜索+二进制枚举+开关问题)
POJ.3279 Fliptile (搜索+二进制枚举+开关问题) 题意分析 题意大概就是给出一个map,由01组成,每次可以选取按其中某一个位置,按此位置之后,此位置及其直接相连(上下左右)的位置( ...
- poj 3977 Subset(折半枚举+二进制枚举+二分)
Subset Time Limit: 30000MS Memory Limit: 65536K Total Submissions: 5721 Accepted: 1083 Descripti ...
- POJ 1681 Painter's Problem 【高斯消元 二进制枚举】
任意门:http://poj.org/problem?id=1681 Painter's Problem Time Limit: 1000MS Memory Limit: 10000K Total ...
- 紫书 例题 11-3 UVa 1151 (有边集的最小生成树+二进制枚举子集)
标题指的边集是说这道题的套餐, 是由几条边构成的. 思路是先做一遍最小生成树排除边, 因为如果第一次做没有加入的边, 到后来新加入了很多权值为0的边,这些边肯定排在最前面,然后这条边的前面的那些边肯定 ...
- 【uva 1151】Buy or Build(图论--最小生成树+二进制枚举状态)
题意:平面上有N个点(1≤N≤1000),若要新建边,费用是2点的欧几里德距离的平方.另外还有Q个套餐,每个套餐里的点互相联通,总费用为Ci.问让所有N个点连通的最小费用.(2组数据的输出之间要求有换 ...
- POJ 3279 Fliptile (二进制枚举)
<题目链接> <转载于 >>> > 题目大意: 给定一个M*N矩阵,有些是黑色(1表示)否则白色(0表示),每翻转一个(i,j),会使得它和它周围4个格变为另 ...
随机推荐
- 转:python字符串/元组/列表/字典互转
#-*-coding:utf-8-*- #1.字典 dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回:<type ...
- each实现原理
<script> function isEach(arr, callback) { for (var i in arr) { callback(i, arr[i]); } }; funct ...
- java ajax传值 中文乱码
String remark = new String(this.getRequest().getParameter("remark").getBytes("iso885 ...
- spark使用Hive表操作
spark Hive表操作 之前很长一段时间是通过hiveServer操作Hive表的,一旦hiveServer宕掉就无法进行操作. 比如说一个修改表分区的操作 一.使用HiveServer的方式 v ...
- Android—监听器
网上有很多短信和电话监听的程序,使用Broadcast. 记得一年前自己对照视频和教材是能够实现的,这周打开视频和教材照猫画虎,无论怎么都不会,纠结啊! 问题原因: 3.0之后没有主动开启过的应用无法 ...
- 【crunch bang】国内源
/etc/apt/sources.list 因为测试需要,装完Debian7 后,更新为163的源,但是后来装软件时,一些软件依赖包还是装不上.后来把163源稍加改动,就好用了.163源内容如下: d ...
- html5的download下载标签
Html5的下载标签download <a href="files/1.jpg" download="1.jpg">Download</a&g ...
- Broadwell I7-5775c/5675c BSOD 蓝屏问题
今年6月末Intel发布了第五代台式机CPU,代号Broadwell,跳票一年多,仅有两款零售型号,I7-5775C和I5-5675C. 第一时间就买来试用,当时还是在日亚购买的,等待了10天左右终于 ...
- 四种MySQL存储引擎
前言 数据库存储引擎是数据库底层软件组织,数据库管理系统(DBMS)使用数据引擎进行创建.查询.更新和删除数据.不同的存储引擎提供不同的存储机制.索引技巧.锁定水平等功能,使用不同的存储引擎,还可以 ...
- autohotkey --- win10运行不兼容
在win10下许多脚本运行有问题, 将AutoHotkey.exe设置为兼容模式为win7 同时要设置为以管理员身份运行此程序 这个必须得记录一下.