uva-10160-枚举
若当前搜索到的城市n前面1~n-1编号的城市中有没有通电的,则永远也无法输送电力给那个城市,因为在剪枝时附加了和此结点连接的最大结点小于当前的结点
这段
for(int i = 1; i < cur; i++)
if(!vis[i] && map[i][0] < cur)
return;
#include<iostream>
#include <new>
#include<stdio.h>
#include<memory.h>
using namespace std;
int n, m;
int map[][];
int index2[];
int vis[];
int res = ;
void sort(int a[], int length)
{
for(int i = ; i < length; i++)
for(int j = ; j < length - i; j++)
if(a[j] > a[j - ])
{
int k = a[j - ];
a[j - ] = a[j];
a[j] = k;
}
}
void dfs(int cur, int sum, int total)
{
if(sum >= res)
return;
if(total == n)
{
res = sum;
return;
}
for(int i = ; i < cur; i++)
if(!vis[i] && map[i][] < cur)
return;
//不选
dfs(cur + , sum, total);
int k = ;
int vis2[] = { };
for(int i = ; i < index2[cur]; i++)
{
if(!vis[map[cur][i]])
{
vis[map[cur][i]] = ;
//选,这次覆盖到点
vis2[k++] = map[cur][i];
}
}
if(!k)
//没有新的点覆盖
return;
dfs(cur + , sum + , total + k);
for(int i = ; i < k; i++)
vis[vis2[i]] = ;
}
int main()
{
freopen("d:\\1.txt", "r", stdin);
while (cin >> n >> m && n != && m != )
{
memset(map, -, sizeof(map));
memset(vis, , sizeof(vis));
memset(index2, , sizeof(index2));
int s, e;
for(int i = ; i < m; i++)
{
cin >> s >> e;
map[s][index2[s]++] = e;
map[e][index2[e]++] = s;
}
for(int i = ; i <= n; i++)
{
map[i][index2[i]++] = i;
sort(map[i], index2[i]);
}
res = 0x7fffffff;
dfs(, , );
cout << res << endl;
}
return ;
}
uva-10160-枚举的更多相关文章
- UVa 12169 (枚举+扩展欧几里得) Disgruntled Judge
题意: 给出四个数T, a, b, x1,按公式生成序列 xi = (a*xi-1 + b) % 10001 (2 ≤ i ≤ 2T) 给出T和奇数项xi,输出偶数项xi 分析: 最简单的办法就是直接 ...
- UVa 140 (枚举排列) Bandwidth
题意较复杂,请参见原题=_=|| 没什么好说的,直接枚举每个排列就好了,然后记录最小带宽,以及对应的最佳排列. STL里的next_permutation函数真是好用. 比较蛋疼的就是题目的输入了.. ...
- UVa 1151 (枚举 + MST) Buy or Build
题意: 平面上有n个点,现在要把它们全部连通起来.现在有q个套餐,如果购买了第i个套餐,则这个套餐中的点全部连通起来.也可以自己单独地建一条边,费用为两点欧几里得距离的平方.求使所有点连通的最小费用. ...
- Even Parity UVA - 11464 (枚举)
从来没有觉得枚举有多费脑子的.但是这道题还是很香的. 思路:就是非常简单的枚举啦. 从一般的枚举开始考虑.一般的做法就是在所有的格子中有两种状态1, 0. 而一共有225个格子,所有一共要枚举的情 ...
- UVa 1354 枚举子集 Mobile Computing
只要枚举左右两个子天平砝码的集合,我们就能算出左右两个悬挂点到根悬挂点的距离. 但是题中要求找尽量宽的天平但是不能超过房间的宽度,想不到要怎样记录结果. 参考别人代码,用了一个结构体的vector,保 ...
- uva 10160
一开始写的代码加上各种剪枝后还是超时, 然后看了一下状态压缩后过了,两个代码的具体思想是一样的,状态压缩后可以大大提升性能 #include <cstdio> #include <c ...
- UVA 10160 Servicing Stations(深搜 + 剪枝)
Problem D: Servicing stations A company offers personal computers for sale in N towns (3 <= N < ...
- uva 10160 Servicing Stations(DFS+剪枝)
Servicing stations A company offers personal computers for sale in N towns (3 <= N <= 35). The ...
- UVA 10160 Servicing Stations(状态压缩+迭代加深)
[题目链接] LInk [题目大意] 给出一些点和边,选择一个点就能把这个点和相邻的点都覆盖,求最小点覆盖 [题解] 我们压缩点被覆盖的状态,迭代加深搜索覆盖的最小点数, 当剩余的点全部选上时都无法完 ...
- Uva 11754(枚举+中国剩余定理)
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #inclu ...
随机推荐
- POJ 2407:Relatives(欧拉函数模板)
Relatives AC代码 Relatives Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16186 Accept ...
- python 取整
1.向下取整 向下取整直接用内建的 int() 函数即可: >>> a = 3.75 >>> int(a) 3 2.四舍五入 对数字进行四舍五入用 round() ...
- 当php版本为5.6时的提示信息解决方法
ecshop修饰符preg_replace/e不安全的几处改动 Strict standards: Only variables should be passed by reference in D: ...
- Android和Linux下设备节点的创建笔记
1. Linux kernel创建的/dev/下的设备节点是不对的, 其实是kernel仅负责在/sys/(基于内存的虚拟文件系统)创建一大堆下目录和文件,而真正的设备节点是在用户空间程序创建的,应该 ...
- windows 版 nginx 运行错误的一些解决方法
1. 关于文件夹的中文的问题. 错误的截图如下: 看得到这个 failed (1113: No mapping for the Unicode character exists in the targ ...
- HTML/CSS/Javascript代码在线压缩、格式化(美化)工具
CSS 格式化 ProCSSor - http://procssor.com/ CSS 压缩 CSS Compressor - http://www.cssdrive.com/index.php/ ...
- ereg()替换为preg_match(),ereg_repalce替换为preg_replace得加斜杠
PHP 5.3 ereg() 无法正常使用,提示“Function ereg() is deprecated Error”是因为它长ereg 函数进行了升级处理,需要像preg_match使用/ /来 ...
- 【柚子木字幕組】【BBC】 Are Our Kids Tough Enough? Chinese School 英國的孩子足夠堅強嗎?中式教學
https://www.youtube.com/watch?v=ypT6c4NZ6jk 最近很火的一个bbc纪录片 讲的是英国学校请几个中国老师到他们那里试行中国教育的故事 作为学习英文的素材很不错
- MySql检测阻塞,锁等待sql
------------ 1分钟内产生binlog大小计算 select @a1:=VARIABLE_VALUE as a1from information_schema.GLOBAL_STATUSw ...
- 开启postgresql的远程权限
cd /etc/postxxxx/版本号/main vim postgresql.conf 修改 #listen_addresses ='localhost'为 listen_addresses =' ...