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 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种连接方式,每个城市可以放一个服务站,服务站可以覆盖本城市和与本城市连接的城市。要求最少几个服务站可以覆盖所有城市。

思路:深搜,每个服务站可以选择放与不放服务站。最多35个城市,直接搜最多要搜2^35次,所以要剪枝。。

进行了几个剪枝

1:用邻接表代替邻接矩阵来存放关系。可以减少一点搜索次数。

2:每次找到一个放置方案,保存下个数,如果之后搜索过程中个数超过这个个数。就可以直接结束这次搜索。

3:如果一个点已经搜索过了。并且该点没被覆盖到, 并且之后的点没有连接到改点,那么该点永远不会被覆盖到,就可以直接结束这条的搜索。

有一点的要注意的是。标记覆盖不能用单纯的0,1来标记。因为一个点是可以重复覆盖的。我用的是vis[] ++, vis[]--的方法来标记的

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int n, m;
int x, y;
int snum[40];
int map[40][40];
int f[40];
int minn; bool cmp(int a, int b)
{
return a > b;
} void dfs(int star, int num, int tnum)
{
if (tnum >= minn)
return;
if (num == n)
{
minn = tnum;
return;
}
if (star > n)
return;
for (int i = 1; i < star; i ++)
if (!f[i] && map[i][0] < star)
return;
int jia = 0;
for (int i = 0; i < snum[star]; i ++)
{
if (f[map[star][i]] == 0)
jia ++;
f[map[star][i]] ++;
}
if (jia)
dfs(star + 1, num + jia, tnum + 1);
for (int i = 0; i < snum[star]; i ++)
{
f[map[star][i]] --;
}
dfs(star + 1, num, tnum);
}
int main()
{
while (scanf("%d%d", &n, &m) != EOF && n + m)
{
minn = n;
memset(snum, 0, sizeof(snum));
memset(map, 0, sizeof(map));
memset(f, 0, sizeof(f));
for (int i = 0; i < m; i ++)
{
scanf("%d%d", &x, &y);
map[x][snum[x] ++] = y;
map[y][snum[y] ++] = x;
}
for (int i = 1; i <= n; i++)
{
map[i][snum[i] ++] = i;
sort(map[i], map[i] + snum[i], cmp);
}
dfs(1, 0, 0);
printf("%d\n", minn);
}
return 0;
}

UVA 10160 Servicing Stations(深搜 + 剪枝)的更多相关文章

  1. uva 10160 Servicing Stations(DFS+剪枝)

    Servicing stations A company offers personal computers for sale in N towns (3 <= N <= 35). The ...

  2. UVA 10160 Servicing Stations(状态压缩+迭代加深)

    [题目链接] LInk [题目大意] 给出一些点和边,选择一个点就能把这个点和相邻的点都覆盖,求最小点覆盖 [题解] 我们压缩点被覆盖的状态,迭代加深搜索覆盖的最小点数, 当剩余的点全部选上时都无法完 ...

  3. Hdu3812-Sea Sky(深搜+剪枝)

    Sea and Sky are the most favorite things of iSea, even when he was a small child.  Suzi once wrote: ...

  4. poj1190 生日蛋糕(深搜+剪枝)

    题目链接:poj1190 生日蛋糕 解题思路: 深搜,枚举:每一层可能的高度和半径 确定搜索范围:底层蛋糕的最大可能半径和最大可能高度 搜索顺序:从底层往上搭蛋糕,在同一层尝试时,半径和高度都是从大到 ...

  5. ACM 海贼王之伟大航路(深搜剪枝)

    "我是要成为海贼王的男人!" 路飞他们伟大航路行程的起点是罗格镇,终点是拉夫德鲁(那里藏匿着"唯一的大秘宝"--ONE PIECE).而航程中间,则是各式各样的 ...

  6. hdu 1518 Square(深搜+剪枝)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1518 题目大意:根据题目所给的几条边,来判断是否能构成正方形,一个很好的深搜应用,注意剪枝,以防超时! ...

  7. POJ-1724 深搜剪枝

    这道题目如果数据很小的话.我们通过这个dfs就可以完成深搜: void dfs(int s) { if (s==N) { minLen=min(minLen,totalLen); return ; } ...

  8. 一本通例题-生日蛋糕——题解<超强深搜剪枝,从无限到有限>

    题目传送 显然是道深搜题.由于蛋糕上表面在最底层的半径确认后就确认了,所以搜索时的面积着重看侧面积. 找维度/搜索面临状态/对象:当前体积v,当前外表面面积s,各层的半径r[],各层的高度h[]. 可 ...

  9. 模拟赛T5 : domino ——深搜+剪枝+位运算优化

    这道题涉及的知识点有点多... 所以还是比较有意思的. domino 描述 迈克生日那天收到一张 N*N 的表格(1 ≤ N ≤ 2000),每个格子里有一个非 负整数(整数范围 0~1000),迈克 ...

随机推荐

  1. Windows下搭建deepnet环境

    近期在做deep learning的项目,学习了一下deepnet,之前搭建了一个windows下的deepnet的学习开发环境,把搭建系统的过程分享给大家. 1.我用的是windows下的visua ...

  2. Altera FPGA中的pin进一步说明

    最近END china上的大神阿昏豆发表了博文 <FPGA研发之道(25)-管脚>,刚好今天拿到了新书<深入理解Altera FPGA应用设计>第一章开篇就讲pin.这里就两者 ...

  3. java基础之junit测试框架

    1.导入junit包, 2.测试方法格式 public void test_*(){} 继承  TestCase  包(keep the bar green to keep the code clea ...

  4. 言未及之而言,谓之躁;言及之而不言,谓之隐;未见颜色而言,谓之瞽(gǔ)

    前言 一个高效的团队离不开leader和组员之前,组员和组员之前的通力合作.而合作的基础便是彼此之间的商讨与协调,意见的统一,进而在达成共识的前提下行动.那么如何才能和组员达成共识呢? 和组员之间的沟 ...

  5. TCP和UDP的"保护消息边界" (经典)

    在socket网络程序中,TCP和UDP分别是面向连接和非面向连接的.因此TCP的socket编程,收发两端(客户端和服务器端)都要有一一成对的socket,因此,发送端为了将多个发往接收端的包,更有 ...

  6. hdu 1565&&hdu 1569 (最大点权独立集)

    题目意思很明确就是选一些没有相连的数字,使和最大,建成二分图后求最大点权独立集,, #include<stdio.h> #include<string.h> const int ...

  7. stm32之watchdog

    在嵌入式系统中,由于MCU的工作常常受到来自外界电磁场的干扰,造成程序的跑飞,而陷入死循环,程序的正常运行被打断,由单片机控制的系统无法继续工作,会造成整个系统陷入停滞状态,发送不可预料的后果,所以出 ...

  8. e-mail Web端管理

    邮件是和上海的一家微软的代理商合作的,管理很方便,但是目前感觉他家的邮件过滤机制有问题.

  9. javascript每日一练(十三)——运动实例

    一.图片放大缩小 <!doctype html> <html> <head> <meta charset="utf-8"> < ...

  10. 简单描述RAID级别:

    简单描述RAID级别: RAID 0 是俩盘一起读写,如果一个坏了那么数据全丢失: RAID 1是一块写,一块用来备份,坏一块无所谓: RAID 2 ,3 ,4 不常用: 最常用的就是RAID 5和R ...