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中Post方式发送HTTP请求

    Post方式比Get方式要复杂一点,因为该方式需要将请求的参数放在http请求的正文中,所以需要构造请求体. 步骤: 1.构造URL URL url = new URL(PATH); 2.设置连接 H ...

  2. AndRoid studio创建APP图标

    打开---File----New----Image asset 注意:在design页面可能没有image asset选项!必须在其他编辑页面! 这就打开了图标设置页面,找到自己想要的图标就好!下面框 ...

  3. android MediaPlayer的一些使用方法简历

    这里提供一些MediaPlayer的简单方法,方便以后熟练的使用它! 1)如何获得MediaPlayer实例: 可以使用直接new的方式: MediaPlayer mp = new MediaPlay ...

  4. web service上传参数代码实例

    web service上传参数代码实例 这次做的项目用到webservice比较多,最开始在网上看的参考dome,发现都不行,后来发现安卓4.0以后有很大的不同,在做传参时,有些东西需要注意: 第一, ...

  5. android环境搭建

    Android开发环境的两种安装方式:JDK+Eclipse+ADT直接下载集成的开发工具,解压即可用.下载地址:http://pan.baidu.com/s/1dD3s3bN ,不管采用哪种安装方式 ...

  6. jQuery和ExtJS的timeOut超时问题和event事件处理问题

    对jQuery来说,超时可以直接设置timeout参数,并在error事件中捕获第二个参数,如果是“timeout”则表明捕获了超时事件,非常清楚. 例子: $.ajax({         type ...

  7. 转:JS获取浏览器高度和宽度

    发现一篇好文章,汇总到自己的网站上. IE中: document.body.clientWidth ==> BODY对象宽度 document.body.clientHeight ==> ...

  8. Servlet简介与Servlet和HttpServlet运行的流程

    1.Servlet      [1] Servlet简介         > Server + let         > 意为:运行在服务器端的小程序.         > Ser ...

  9. 1.7 基础知识——GP2.6 配置管理(CM)

    摘要: 某某工作产品要进行配置管理,并不意味非要进行很重型的配置管理,根据实际需要采取合适的方式就可以了. 正文: GP2.6 Place designed work products of XXX ...

  10. .NET 多语言支持解决方案(转)

    asp.net 2.0中的App_GlobalResources可以用来解决本地化的问题,程序会根据浏览器的语言首选项自动判断显示出本地化的界面. 首先在App_GlobalResources新建re ...