uva 10160 Servicing Stations(DFS+剪枝)
Servicing stations
A company offers personal computers for sale in N towns (3 <= N <= 35). The towns are denoted by 1, 2, ..., N. There are direct routes connecting M pairs from among these towns. The company decides to build servicing stations in several towns, so that for any town X, there would be a station located either in X or in some immediately neighbouring town of X.
Write a program for finding out the minumum number of stations, which the company has to build, so that the above condition holds.
Input
The input consists of more than one description of town (but totally, less than ten descriptions). Every description starts with number N of towns and number M of pairs of towns directly connected each other. The integers N and M are separated by a space. Every one of the next M rows contains a pair of connected towns, one pair per row. The pair consists of two integers for town's numbers, separated by a space. The input ends with N = 0 and M = 0.
Output
For every town in the input write a line containing the obtained minimum.
An example:
Input:
8 12
1 2
1 6
1 8
2 3
2 6
3 4
3 5
4 5
4 7
5 6
6 7
6 8
0 0
Output:
2
题目大意:给出n个点和m个关系,可以在任何一个点放服务站,如果这个点放了服务站,与它相连得点均可以被服务到,问最少放多少个服务站可以使得所有所有点均可以被服务到。
解题思路:思路很简单,用DFS收索,关键就在与剪枝,我的主要优化在于两个地方。
首先按点得序号开始DFS, cur 表示当前访问得点。
<1>对于每个点,无非就是放与不放(注意这里不能单纯根据这个点有没有被覆盖到去判断该不该放服务站)
<2>第一个剪枝,如果这个点增加服务站之后,被覆盖得点数没有增加,就可以确定当前点是不放(与放得情况相同干嘛要多加一个点)
<3>第二个剪枝,当放得服务站个数大于前面计算的最小值时,剪掉这条路。
<4>第三个剪枝,当出现前面有的点无法被覆盖得时候,可以终止这条路得收索。
这里问解释一下第三个剪枝得情况,比如当前已经访问到第5个点,而1 这点任然没有被覆盖到,而与1 有联系得点2,2 < 5, 说明2 已经被访问过了,说明1 和 2 都没有放服务站,而后面n个点得放与不放都影响不到1 得覆盖状态,所以这条路是无法被满足的。
我在将一下问对优化3的一个小优化,如果按正常的思路,需要开一个数组去记录哪些点放了服务站,然后进行优化3得时候对判断点要去遍历判断点的所有有关系的点是否已经考虑过。而我得做法是将点得关系点排序,每次只需将最大的点与当前访问的点进行比较,大于得话说明这个点还没被确定。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; #define N 40
int n, m, Min, rec[N];
int g[N][N], son[N]; int cmp(const int &a, const int &b){
return a > b;
} void DFS(int cur, int cnt, int sum){
if (sum >= Min) // 剪枝2:当放得服务站数量大于前面计算的最小值时。
return;
if (cnt == n)
Min = sum; for (int i = 1; i < cur; i++) //剪枝3:出现已经遍历的点无法被覆盖,这条路继续收索是无用功。
if (!rec[i] && g[i][0] < cur)
return;
DFS(cur + 1, cnt, sum); int k = 0, vis[N];
for (int i = 0; i < son[cur]; i++)
if (rec[g[cur][i]] == 0){
vis[k++] = g[cur][i];
rec[g[cur][i]] = 1;
}
if (!k) // 剪枝1:增加覆盖点为0。
return ; DFS(cur + 1, cnt + k, sum + 1); for (int i = 0; i < k; i++) // 回溯要将点还原,不然答案会错。
rec[vis[i]] = 0;
} int main(){
int a, b;
while (scanf("%d%d", &n, &m), n + m){
// Init;
memset(g, 0, sizeof(g));
memset(son, 0, sizeof(son));
memset(rec, 0, sizeof(rec));
Min = n + 1; // Read;
for (int i = 0; i < m; i++){
scanf("%d%d", &a, &b);
g[a][son[a]++] = b;
g[b][son[b]++] = a;
} // Handle;
for (int i = 1; i <= n; i++){
g[i][son[i]++] = i;
sort(g[i], g[i] + son[i], cmp); // 优化3的优化,具体看解释。
}
DFS(1, 0, 0); printf("%d\n", Min);
}
return 0;
}
uva 10160 Servicing Stations(DFS+剪枝)的更多相关文章
- UVA 10160 Servicing Stations(深搜 + 剪枝)
Problem D: Servicing stations A company offers personal computers for sale in N towns (3 <= N < ...
- UVA 10160 Servicing Stations(状态压缩+迭代加深)
[题目链接] LInk [题目大意] 给出一些点和边,选择一个点就能把这个点和相邻的点都覆盖,求最小点覆盖 [题解] 我们压缩点被覆盖的状态,迭代加深搜索覆盖的最小点数, 当剩余的点全部选上时都无法完 ...
- UVa 208 消防车(dfs+剪枝)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 140 Bandwidth (dfs 剪枝 映射)
题意: 给定一个n个结点的图G和一个结点的排列, 定义结点i的带宽b(i)为i和相邻结点在排列中的最远距离, 所有b(i)的最大值就是这个图的带宽, 给定G, 求让带宽最小的结点排列. 给定的图 n ...
- Sticks(UVA - 307)【DFS+剪枝】
Sticks(UVA - 307) 题目链接 算法 DFS+剪枝 1.这道题题意就是说原本有一些等长的木棍,后来把它们切割,切割成一个个最长为50单位长度的小木棍,现在想让你把它们组合成一个个等长的大 ...
- *HDU1455 DFS剪枝
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- POJ 3009 DFS+剪枝
POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...
- poj 1724:ROADS(DFS + 剪枝)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10777 Accepted: 3961 Descriptio ...
- DFS(剪枝) POJ 1011 Sticks
题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...
随机推荐
- php利用root权限执行shell脚本 (转)
转一篇博客,之前搞这个东西搞了好久,结果今天晚上看到了一篇救命博客,瞬间开心了...转载转载 利用sudo来赋予Apache的用户root的执行权限,下面记录一下: 利用PHP利用root权限执行sh ...
- 机器学习之路:python 集成分类器 随机森林分类RandomForestClassifier 梯度提升决策树分类GradientBoostingClassifier 预测泰坦尼克号幸存者
python3 学习使用随机森林分类器 梯度提升决策树分类 的api,并将他们和单一决策树预测结果做出对比 附上我的git,欢迎大家来参考我其他分类器的代码: https://github.com/l ...
- android studio 汉化 个性化 美化 快速操作项目 目录
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 汉化包 百度云盘 下载地址:https://pan.baidu.com/s/1pLjwy ...
- [COGS2639]偏序++
[COGS2639]偏序++ 题目大意: \(n(n\le40000)\)个\(k(k\le7)\)元组,求\(k\)维偏序. 思路: 分块后用bitset维护. 时间复杂度\(\mathcal O( ...
- 保存全局Crash报告&发送邮件
上篇写到,将程序中没有处理到的crash信息保存到本地文件夹下.但是实际的情况是,你不可能总是将用户的设备拿过来.所以一般性的处理是,将crash reports发送到服务器或者邮箱.所以针对上篇的代 ...
- 【51nod-1239&1244】欧拉函数之和&莫比乌斯函数之和 杜教筛
题目链接: 1239:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1239 1244:http://www.51nod. ...
- HDU 1269 移动城堡 联通分量 Tarjan
迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- wikioi 1294 全排列 dfs
1294 全排列 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 给出一个n, 请输出n的所有全排列 输入描述 Inpu ...
- ASP.NET 构建高性能网站 第6篇
内存问题概述 和CPU一样,内存也是一个直接影响服务端性能的重要的硬件资源. 一般来说,如果服务端内存不足,从导致以下两个问题产生: 1. 导致服务端把一些原本要写到内存中的数据,写到硬盘 ...
- mysqld --debug=d:t:i:O:n --user=mysql (源码调试)
--debug=d:t--debug=d:f,main,subr1:F:L:t,20--debug=d,input,output,files:n--debug=d:t:i:O,\\mysqld.tra ...