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.
Sample 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
SampleOutput
      2

  1. 题目大意:给定n个城市,m条无向边分别连接两个城市。若城市x建造了发电厂,则与x相连的城市都有电供应。求使所有城市都有电供应所需的最小发电厂数。
  2. 思路:回溯法+剪枝
  • 可行性剪枝1:若当前已有n个城市有了电,即所有城市都通了电,则记录当前最小值。
  • 可行性剪枝2:若当前搜索到的城市n前面1~n-1编号的城市中有没有通电的,则永远也无法输送电力给那个城市,无解。
  • 剪枝2实现方法:用邻接数组存储每个点相邻的点,将每个邻接数组的点以其编号为关键字降序排序,dfs判断时若前面1~n-1个点中有没有电的,并且它的最大编号儿子的编号小于当前城市(即搜索序更小,已经搜索过却无电),则剪枝。
  • 最优化剪枝:若当前要用的最小发电厂数已经超过了当前求得最有解,则剪枝。

//顺便注意以下:uva上输入时的“while(scanf("%d%d",&n,&m)&&n&&m)“无法通过,要用”while(scanf("%d%d",&n,&m),n+m)”哦;

Accepted 660ms (resourse:virtual judge)

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int g[40][40],son[40],n,m,ans;
bool elt[40];
bool cmp(const int &a,const int &b)
{
return a>b;
}
void Init()
{
ans=n+1;
memset(g,0,sizeof(g));
memset(elt,0,sizeof(elt));
memset(son,0,sizeof(son));
for(int i=0; i<m; i++)
{
int u,v;
scanf("%d%d",&u,&v);
g[u][son[u]++]=v;
g[v][son[v]++]=u;
}
for(int i=1; i<=n; i++)
{
g[i][son[i]++]=i;
sort(g[i],g[i]+son[i],cmp);
}
}
void dfs(int cur,int elcity,int sum)
{
if(sum>=ans)return;//tree cut 1;
if(elcity==n)
ans=sum;
for(int i=1; i<cur; i++) //tree cut 2;
if((!elt[i])&&(g[i][0]<cur))
return;
dfs(cur+1,elcity,sum);
int k=0,vis[40];
for(int i=0; i<son[cur]; i++)
if(!elt[g[cur][i]])
{
elt[g[cur][i]]=1;
vis[k++]=g[cur][i];
}
if(!k)return;//tree cut 3;
dfs(cur+1,elcity+k,sum+1);
for(int i=0; i<k; i++)
elt[vis[i]]=0;
}
int main()
{
while(scanf("%d%d",&n,&m),n+m)
{
Init();
dfs(1,0,0);
printf("%d\n",ans);
}
return 0;
}

uva10160 Servicing Stations的更多相关文章

  1. UVA 10160 Servicing Stations(深搜 + 剪枝)

    Problem D: Servicing stations A company offers personal computers for sale in N towns (3 <= N < ...

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

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

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

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

  4. 备战NOIP每周写题记录(一)···不间断更新

    ※Recorded By ksq2013 //其实这段时间写的题远远大于这篇博文中的内容,只不过那些数以百记的基础题目实在没必要写在blog上; ※week one 2016.7.18 Monday ...

  5. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  6. (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

    http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...

  7. ACM训练计划step 1 [非原创]

    (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...

  8. 算法竞赛入门经典+挑战编程+USACO

    下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...

  9. HDUOJ----2485 Destroying the bus stations(2008北京现场赛A题)

    Destroying the bus stations                                                                          ...

随机推荐

  1. yum源的配置(centos6.5)

    # cd /etc/yum.repos.d/ # mv CentOS-Base.repo CentOS-Base.repo.bak # wget http://mirrors.163.com/.hel ...

  2. IOS圆头像

    前言 随着腾讯QQ的普及,现在越来越多的社交类APP在显示好友头像时,都选择用圆形头像,效果如下(不包括黑底): 在ios开发中,大致有以下三种方案来实现圆形头像效果. 方案一:用Quartz2D绘制 ...

  3. filezilla sftp 文件和linux 文件不同步的问题

    删除掉以前的链接,重新建立链接就好了

  4. 使用TARGET_IPHONE_SIMULATOR和TARGET_OS_IPHONE宏区分模拟器和设备

    #if TARGET_IPHONE_SIMULATOR          NSLog(@"run on simulator");      #else          NSLog ...

  5. [Android] android studio 2.0即时运行功能探秘

    即时运行instant Run是android studio 2中,开发人员最关心的特性之一 在google发布studio 2.0之后,马上更新体验了一把,然而发现,并没快多少,说好的即时运行呢? ...

  6. yii2 gridview 是否显示某列案例

    作者:白狼 出处:http://www.manks.top/article/yii2_gridview_visible 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给 ...

  7. ORACLE SQL Developer日期显示格式设置

    ORACLE的SQL Developer工具默认的日期格式DD-MON-RR,在SQL查询中往往你看不到时间信息,此时你必须修改日期格式.具体如下所示 工具->首选项->数据库->N ...

  8. Javascript之旅——第十站:为什么都说闭包难理解呢?

    研究过js的朋友大多会说,理解了js的原型和闭包就可以了,然后又说这些都是js的高级内容,然后就又扯到了各种神马的作用域...然后不少 人就会被忽悠的云里雾里...下面我也试着来说说闭包,看我说的这个 ...

  9. W3School-CSS 边框(border)实例

    CSS 边框(border)实例 CSS 实例 CSS 背景实例 CSS 文本实例 CSS 字体(font)实例 CSS 边框(border)实例 CSS 外边距 (margin) 实例 CSS 内边 ...

  10. Java代码获取NTP服务器时间

    apache的commons-net包下面有ntp相关的实现类,主要类有: 1  org.apache.commons.net.ntp.NTPUDPClient ? 1  org.apache.com ...