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. [Android]使用Kotlin+Anko开发Android(一)

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4800656.html  Kotlin是由JetBrains开发 ...

  2. git使用详细介绍

    1. Git概念 1.1. Git库中由三部分组成        Git 仓库就是那个.git 目录,其中存放的是我们所提交的文档索引内容,Git 可基于文档索引内容对其所管理的文档进行内容追踪,从而 ...

  3. oc程序编译【-framework Foundation 编译】

    第一种编译 第二种编译 -framework Foundation 编译

  4. 【代码笔记】iOS-iCarouselDemo

    一,效果图. 二,工程图. 三,代码. RootViewController.h RootViewController.m myCell.h #import <UIKit/UIKit.h> ...

  5. android Gui系统之SurfaceFlinger(4)---Vsync(1)

    8.Vsync 8.1概论 VSYNC(Vertical Synchronization)是一个相当古老的概念,对于游戏玩家,它有一个更加大名鼎鼎的中文名字—-垂直同步. “垂直同步(vsync)”指 ...

  6. 深入理解“HelloWorld”小程序

    对于每个Java程序员来说,HelloWorld是一个再熟悉不过的程序.它很简单,但是这段简单的代码能指引我们去深入理解一些复杂的概念.这篇文章,我将探索我们能从这段简单的代码中学到什么.如果你对He ...

  7. GY编辑平台产品总结

    产品亮点一.实时直播流的关键帧识别并展示选择频道的实时流并播放后,会在窗口中自动展示关键帧图片:配对选择关键帧的截图即确定了素材的入点,出点:编辑平台图如下所示:二.广告自动识别与监测方案1. 制作样 ...

  8. asp.net之treeview无法显示树结点图标(IP与域名的表现竟不一样)

    背景   今天接到客户的电话,说部署上去的项目树型的treeview无法正常显示,显示成了好几个大红叉.如: 排查   于是我通过远程登录到服务器,在本地测试了一会发现没有这个问题存在,无论是通过IP ...

  9. sqlplus: error while loading shared libraries: /u01/app/lib/libclntsh.so.11.1

    成功安装了Oracle 11g后,使用sqlplus登录数据库时遇到下面错误: [oracle@DB-Server ~]$ sqlplus / as sysdba   sqlplus: error w ...

  10. Storm系列(二):使用Csharp创建你的第一个Storm拓扑(wordcount)

    WordCount在大数据领域就像学习一门语言时的hello world,得益于Storm的开源以及Storm.Net.Adapter,现在我们也可以像Java或Python一样,使用Csharp创建 ...