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个格变为另 ...
 
随机推荐
- Undefined symbols “_OBJC_CLASS_$_XXX” 问题
			
解决方法是点击工程,在targets界面中找到Build Phases,根据提示信息“XXX”来判断缺少什么文件,一般如果缺少自定义的文件,XXX会是缺少的类名,那么就在Complie Sources ...
 - Lintcode: Minimum Subarray
			
Given an array of integers, find the subarray with smallest sum. Return the sum of the subarray. Hav ...
 - application 统计网站访问人数
			
参考书<JSP Web 开发案例教程> index.jsp welcome.jsp 显示
 - each实现原理
			
<script> function isEach(arr, callback) { for (var i in arr) { callback(i, arr[i]); } }; funct ...
 - find 命令使用总结
			
参考:http://os.51cto.com/art/200908/141119.htm 1.find命令的一般形式 find pathname -options [-print -exec -ok ...
 - Linux  MD5值递归比对目录中的文件是否有修改
			
项目上今天遇到检查两个版本的发布包rc1.tar.gz和rc2.tar.gz的一致性,解决方法做个总结,步骤如下 1. 建立文件夹 mkdir test_rc1 test_rc2 2. 文件解压缩 t ...
 - 活动组件(五):一个activity的例子
			
建立两个Activity,一个是NormalActivity,另一个是DialogActivity.首先建立这两个Activity的布局文件,如下图: 然后编写这连个Activity,如下: 接着对这 ...
 - exec 临时表,报错
			
因为零时表只存在于一个exec 会话中,所以可以用 多个 select 返回到 dataset 中处理多个table,按照select 的顺序,读取 tables[0],table[1] , 多用于统 ...
 - spark history-server的使用
			
为什么需要historyServer? 在运行Spark Application的时候,Spark会提供一个WEBUI列出应用程序的运行时信息:但该WEBUI随着Application的完成(成功/失 ...
 - MVC3/4 自定义HtmlHelper截断文本内容(截取)
			
在MVC目录下新建一个名为 Extersions 的文件夹,在该文件夹中新建一个截断文本类,取名为:CutOfTextExtersions 该类代码如下: using System; using S ...