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个格变为另 ...
随机推荐
- 证明 logX < X 对所有 X > 0 成立
题目取自:<数据结构与算法分析:C语言描述_原书第二版>——Mark Allen Weiss 练习1.5(a) 证明下列公式: logX < X 对所有 X > ...
- Ways to access Oracle Database in PostgreSQL
Today, organizations stores information(data) in different database systems. Each database system ha ...
- 使用Mac的AppleScritp调用控制台的方式
使用Mac的AppleScritp调用 控制台的方法 tell application "Terminal" activate do script "cd Documen ...
- 3D照片放大展示窗口
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- html 圆角边框
<input style="border-radius: 10px;" type="submit" value="确认"> bo ...
- sort 树 hash 排序
STL 中 sort 函数用法简介 做 ACM 题的时候,排序是一种经常要用到的操作.如果每次都自己写个冒泡之类的 O(n^2) 排序,不但程序容易超时,而且浪费宝贵的比赛时间,还很有可能写错. ST ...
- Myeclipse10编写jsp时出现 Multiple annotations found at this line:
今天,老师讲完课做了一个小练习,就是编写一个jsp页面.写完后,我发现少些了点东西,我准备使用<% %>添加是发现,报错了 Multiple annotations found at th ...
- ios json parse
参考:http://wenxin2009.iteye.com/blog/1671691
- II7下配置SSAS通过HTTP 远程链接访问
IIS7下配置SSAS通过HTTP远程连接 安装环境操作系统:Windows7.Windows Server2008IIS版本:7.5 IIS7下配置SSAS通过HTTP远程连接详细的步骤如下:1.首 ...
- [转]]将 ASP.NET MVC3 Razor 项目部署到虚拟主机中
原链接:http://www.cnblogs.com/taven/archive/2011/08/14/2138077.html 国内很多网站空间都只支持.NET 2.0 和 .NET 3.0 3.5 ...