uva10160(dfs+状态压缩)
2014-05-04 00:36
246 查看
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 servicingstations 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
参考了点击打开链接,多添加了|结果wa了好多次
#include<iostream> #include<cstdio> #include<cstring> using namespace std; #define ll long long int n, m; ll a[40], b[40]; bool dfs(ll state, int cur, int next, int num){ if(state== ((ll)1<<n)-1){ return true; } if(cur == num){ return false; } for(int i = next; i <= n; i++){ if((state|a[i])==state){ continue; } if((state|b[i])!=((ll)1<<n)-1){ return false; } if(dfs(state|a[i], cur+1, i+1, num)){ return true; } } return false; } int main(){ // freopen("in.txt", "r", stdin); while(scanf("%d%d", &n, &m),n&&m){ // memset(a, 0, sizeof(a)); for(int i = 1; i <=n; i++){ a[i] = ((ll)1<<(i-1)); } int tmp, tmp2; for(int i = 1; i <= m; i++){ scanf("%d%d", &tmp, &tmp2); a[tmp] = a[tmp] |((ll)1<<(tmp2-1)); a[tmp2] = a[tmp2] |((ll)1<<(tmp-1)); } b = a ; for(int i = n-1; i>0; i--){ b[i] = a[i]|b[i+1]; } for(int i = 1; i <= n; i++){ if(dfs(0,0,1,i)){ printf("%d\n", i); break; } } } return 0; }
相关文章推荐
- SSD固态存储大观(二)
- 基于keepalived的Haproxy高可用配置 推荐
- struts2表单ui
- PHP常见排序算法
- linux进程、调度、线程、进程上下文等几点理解
- yii 用户登录验证(cwebuser) yii 用户登录 (记)
- 视频主观质量评价工具:MSU Perceptual Video Quality tool
- 仿腾讯手机卫士的配置动画欢迎界面
- 视频主观质量评价工具:MSU Perceptual Video Quality tool
- 视频主观质量评价工具:MSU Perceptual Video Quality tool
- 给对象添加方法和属性
- IceHe的技术博客自述
- jquery 序列化表单进行提交笔记
- POJ 1324 Holedox Moving(状态压缩BFS)
- Vmware kernel headers 3.12-kali-686-pae找不到问题
- lambda calculus语言解释器
- HashMap与HashCode()、equals()的关系---在HashMap中实现以对象为键(key)
- 寄存器及七种寻址方式
- boost::tie()和boost::variant()讲解
- HDU 1227 Fast Food (DP)