UVA - 10160 Servicing Stations(回溯+剪枝)
2014-08-18 14:03
549 查看
Problem D: 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 buildservicing 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个城镇建立服务站,n个城镇有m条道路,每个服务可以服务但前的城镇,和当前城镇相连接的城镇,问最少要建立多少个服务站?
解析:本题采用回溯法,但是单纯的只回溯不剪枝会超时,因为最多有35个城镇,即要枚举2^35中情况,可想而知会超时。
所以这题的做法是建立临接表来保存两节点连接
有4个剪枝:
1、当前的服务站个数大于原先的答案,显然后面只会变大,返回。
2、当前已经覆盖的结点等于n即全部覆盖了,那么返回答案,出口。
3、如果当前枚举的点编号大于n返回。
4、最重要,最难想到的一个剪枝,当出现前面有的已经无法被覆盖,那么后面不可能再覆盖这个点,返回。
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int N = 36; int n,m; int edge ; int vis ; int s ; int ans; void dfs(int u,int sum,int cur) { if(cur == n && sum < ans) { ans = sum; return; } if(u > n || sum >= ans) { return; } for(int i = 1; i < u; i++) { if( !vis[i] && edge[i][s[i]-1] < u) { return ; } } int cnt = 0; for(int i = 0; i < s[u]; i++) { if( !vis[edge[u][i]] ) { cnt++; } vis[edge[u][i]]++; } if(cnt) { dfs(u+1,sum+1,cur+cnt); } for(int i = 0; i < s[u]; i++) { vis[edge[u][i]]--; } dfs(u+1,sum,cur); } int main() { int u,v; while(scanf("%d%d",&n,&m) != EOF && (n || m)) { memset(edge,0,sizeof(edge)); memset(s,0,sizeof(s)); memset(vis,0,sizeof(vis)); for(int i = 0; i < m; i++) { scanf("%d%d",&u,&v); edge[u][s[u]++] = v; edge[v][s[v]++] = u; } for(int i = 1; i <= n; i++) { edge[i][s[i]++] = i; sort(edge[i],edge[i]+s[i]); } ans = n; dfs(1,0,0); printf("%d\n",ans); } return 0; }
相关文章推荐
- UVa 208 - Firetruck 回溯+剪枝 数据
- UVA - 519 Puzzle (II) 回溯+剪枝
- uva225 回溯剪枝
- UVA - 185 Roman Numerals(回溯+剪枝)
- UVa 208 - Firetruck 回溯+剪枝 数据
- Uva140 Bandwidth 【dfs回溯+剪枝】【例题7-6】
- UVa 690 - Pipeline Scheduling(回溯剪枝)
- Uva11882 Biggest Number 【dfs回溯+bfs剪枝】【习题7-15】
- UVa 140 Bandwidth(DFS 回溯 剪枝)
- UVA1600 Dfs回溯完全遍历+记忆化搜索(用最小距离剪枝)+细节处理 (值得复习)(水)
- UVA 10160 Servicing Stations(深搜 + 剪枝)
- UVA 10160 Servicing Stations(深搜 + 剪枝)
- [回溯&&剪枝]Firetruck UVA208
- UVA 208 Firetruck 消防车(回溯 + 剪枝)
- uva140_dfs(回溯)最优性剪枝
- UVa 225 黄金图形(回溯+剪枝)
- UVa 10318 - Security Panel (暴力 + 剪枝 + 回溯)
- UVA - 519 Puzzle (II)(回溯+剪枝)
- uva 307 Sticks(回溯剪枝)
- uva10400 - Game Show Math(回溯+剪枝)