UVa 208 消防车(dfs+剪枝)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=144
题意:给出一个n个结点的无向图以及某个结点k,按照字典序从小到大顺序输出从1到结点k的所有路径。
思路:如果直接矩阵深搜的话是会超时的,所以我们可以从终点出发,将与终点相连的连通块保存起来,这样dfs深搜时可以剪枝掉一些到达不了的点。只要解决了这个,dfs就是小问题。
这道题还有点坑的就是输出格式和它所给的格式不一样,注意一下。
#include<iostream>
#include<cstring>
using namespace std; const int maxn = ; int n, step, route;
int map[maxn][maxn];
int path[maxn];
int vis[maxn];
int trunk[maxn]; void init(int cur) //从终点开始遍历,保存与终点相连的连通块
{
trunk[cur] = ;
for (int i = ; i < maxn; i++)
{
if (map[cur][i] && !trunk[i])
init(i);
}
} void dfs(int cur, int step)
{
if (cur == n)
{
cout << "";
for (int i = ; i < step; i++)
cout << " " << path[i];
cout << endl;
memset(path, , sizeof());
route++;
}
for (int i = ; i < maxn; i++)
{
if (map[cur][i] && !vis[i] && trunk[i])
{
vis[i] = ;
path[step] = i;
dfs(i, step + );
vis[i] = ;
}
}
return;
} int main()
{
//freopen("D:\\txt.txt", "r", stdin);
int a, b, kase = ;
while (cin >> n && n)
{
memset(vis, , sizeof(vis));
memset(map, , sizeof(map));
memset(path, , sizeof(path));
memset(trunk, , sizeof(trunk));
while (cin >> a >> b)
{
if (!a && !b) break;
map[a][b] = map[b][a] = ;
}
vis[] = ;
step = ;
route = ; //记录路径数量
init(n); //计算保存连通块
cout << "CASE " << ++kase << ":" << endl;
dfs(, );
cout << "There are " << route << " routes from the firestation to streetcorner " << n << "." << endl; }
return ;
}
UVa 208 消防车(dfs+剪枝)的更多相关文章
- Sticks(UVA - 307)【DFS+剪枝】
Sticks(UVA - 307) 题目链接 算法 DFS+剪枝 1.这道题题意就是说原本有一些等长的木棍,后来把它们切割,切割成一个个最长为50单位长度的小木棍,现在想让你把它们组合成一个个等长的大 ...
- UVa 208 - Firetruck 回溯+剪枝 数据
题意:构造出一张图,给出一个点,字典序输出所有从1到该点的路径. 裸搜会超时的题目,其实题目的数据特地设计得让图稠密但起点和终点却不相连,所以直接搜索过去会超时. 只要判断下起点和终点能不能相连就行了 ...
- *HDU1455 DFS剪枝
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- POJ 3009 DFS+剪枝
POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...
- poj 1724:ROADS(DFS + 剪枝)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10777 Accepted: 3961 Descriptio ...
- DFS(剪枝) POJ 1011 Sticks
题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...
- DFS+剪枝 HDOJ 5323 Solve this interesting problem
题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...
- HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))
Equation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
随机推荐
- windows使用方法
1:截图搜索英文单词:snipping tool 2: 修改语言,搜索language 3:关闭fn键,按键 fn+esc(fnlock). 就可以将fn关闭和开启.
- gradle build scan
1:gradle build scan 用于视图审查 构建步骤如下 https://guides.gradle.org/creating-build-scans/?_ga=2.80362963.59 ...
- 逻辑运算,&,&&, |, ||, ^, !
&:与 特点:判断时两边为true才为true,只要两边有一个为false则结果为false:true&true=true: false&true=false: true&a ...
- etcd 集群部署
etcd web管理 https://nikfoundas.github.io/etcd-viewer/ # git clone https://github.com/nikfoundas/etcd- ...
- js 使用jquery.form.js文件上传
1.文件上传,使用jquery.form.js插件库 <!DOCTYPE html> <html> <head> <meta charset="UT ...
- webpack 3 & React 的配置 。
今天真是难过的一天
- CodeForces - 946D Timetable (分组背包+思维)
题意 n天的课程,每天有m个时间单位.若时间i和j都有课,那么要在学校待\(j-i+1\)个时间.现在最多能翘k节课,问最少能在学校待多少时间. 分析 将一天的内容视作一个背包的组,可以预处理出该天内 ...
- Sa身份登陆SQL SERVER失败的解决方案
经常使用windows身份登陆,久而久之,基本不动怎么用SQL SERVER身份验证登陆,所以趁着有空,就解决一下一些问题~~ 解决方案: 第一步:打开SSMS,先使用windows身份登陆,右击服 ...
- web.xml+spring mvc基本配置
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...
- 【kafka学习之二】Kafka集群搭建
安装环境 jdk1.7 zookeeper-3.4.5(参考 https://www.cnblogs.com/cac2020/p/9426531.html) VM虚拟机redhat6.5-x64 ...