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个格变为另 ...
随机推荐
- JavaScript----插入视频
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- php session session_set_save_handler 接管所有的session管理工作
一个已知管用的方法是,使用session_set_save_handler,接管所有的session管理工作,一般是把session信息存储到数 据库,这样可以通过SQL语句来删除所有过期的sessi ...
- 夺命雷公狗ThinkPHP项目之----企业网站23之网站前台二级分类的跳转(URL跳转到列表页或产品页)
我们现在开始做实现我们的二级菜单如何跳转到指定的列表页或者产品也呢?? 我们分享下数据库情况: 我们的数据库里提前给我们预留了一个cate_type的字段,那么我们可以让这个字段进行判断,从而遍历出指 ...
- ADIv5.2
ADI:ARM Debug Interface,出到现在共有五代: 1)version1 and version 2:只针对ARM7TDMI和ARM9的processor: 2)version 3:只 ...
- ios学习笔记(一)Windows7上使用VMWare搭建iPhone开发环境(转)
原文地址:http://blog.csdn.net/shangyuan21/article/details/18153605 我们都知道开发iPhone等ios平台的移动应用时需要使用Mac本,但是M ...
- java.lang.UnsatisfiedLinkError: no sapjco3 in java.library.path
1.system32添加sapjco3.dll 2.tomcat bin下添加sapjco3.dll 3.项目中添加sapjco3.jar 包
- django view使用学习记录
判断用户是否登录 request.user.is_authenticated()auth.authenticate(username=username, password=password)
- lamp环境编译错误
GD 编译出错解决方法 编译最新的2.0.35版本,用默认的 ./configure编译 当make的时候,出现以下错误 configure.ac:64: error: possibly undefi ...
- 获取url中的参数\+发送ajax请求根路径|+获取复选框的值
//获取url中的参数function getUrlParam(name) { var reg = new RegExp("(^|&)" + name + "=( ...
- Install Sogou IM 2.0 in Ubuntu14.04+/Xfce
Ubuntu14.04+ 安装搜狗输入法 搜狗输入法是一款非常友好的输入法产品,从Ubuntu14.04开始对Linux支持,不过只是Debian系的,是Ubuntu优麒麟组引入的.优麒麟是针对国人设 ...