soj 1015 Jill's Tour Paths 解题报告
题目描述:
1015. Jill's Tour Paths
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
Every year, Jill takes a bicycle tour between two villages. There are different routes she can take between these villages, but she does have an upper limit on the distance that she wants to travel. Given a map of the region indicating the cities and the roads between them (and their distances), Jill would like to have a list of the various routes between the selected cities that will meet her distance requirements. Your task is to write a program that will produce a list of these routes, in increasing order of distance.
We make the following assumptions.
- At most one road connects any pair of villages, and this road is two-way and has a non-zero positive distance.
- There are no roads that lead directly from a village back to the same village.
- Jill is only concerned about a one-way trip. That is, she is not concerned about returning to the village from which she starts her tour.
- Jill will not visit any village more than once during the tour.
The farthest Jill will ever travel is 9999 units
Input
The input will contain several possible cases, each including a route map, identification of the start and destination villages, and the maximum distance Jill is willing to travel.
Each case appears in the input as a set of integers separated by blanks and/or ends of lines. The order and interpretation of these integers in each case is as follows:
- NV – the number of villages in the route map. This number will be no larger than 20.
- NR – the number of roads that appear in the route map. Each road connects a distinct pair of villages.
- NR triples, one for each road, containing C1, C2, and DIST – C1 and C2 identify two villages connected by a road, and DIST gives the distance between these villages on that road.
- SV, DV – the numbers associated with the start and destination villages; the villages are numbered 1 to NV.
- MAXDIST – the maximum distance Jill is willing to travel (one way).
The data for the last case will be followed by a single integer with the value –1.
Output
For each case, display the case number (1, 2, …) on the first line of output. Then, each on a separate additional line, list the routes that Jill might take preceded by the length of the route. Order the routes first by length, from shortest to longest. Within routes having the same length, order them in increasing lexicographic order. The sample input and output provide suitable examples, and the formatting shown there should be followed closely (each village number should be separated by a single space). Separate the output for consecutive cases by a single blank line. If there is no route, print out " NO ACCEPTABLE TOURS"(notice there is one space at the front).
Sample Input
4 5
1 2 2
1 3 3
1 4 1
2 3 2
3 4 4
1 3
4
4 5
1 2 2
1 3 3
1 4 1
2 3 2
3 4 4
1 4
10
5 7
1 2 2
1 4 5
2 3 1
2 4 2
2 5 3
3 4 3
3 5 2
1 3
8
5 7
1 2 2
1 4 5
2 3 1
2 4 2
2 5 3
3 4 3
3 5 2
1 3
1
-1
Sample Output
Case 1:
3: 1 3
4: 1 2 3
Case 2:
1: 1 4
7: 1 3 4
8: 1 2 3 4
Case 3:
3: 1 2 3
7: 1 2 4 3
7: 1 2 5 3
8: 1 4 2 3
8: 1 4 3
Case 4:
NO ACCEPTABLE TOURS
题目分析:
比较简单的一道题,要求求出两点之间小于最大长度的所有路径,用深搜和广搜都可以解决,要注意深搜时在回溯的时候一定要记得将数据恢复到上一次搜索前的状态。以下是深搜的实现。
代码实现:
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <string.h>
- using namespace std;
- struct sol {
- sol(){}
- int route[];
- int size;
- int dista;
- };
- vector<sol> sols;
- bool visited[];
- int adj[][];
- int path[][];
- int adjs[];
- int r[];
- int j, src, dist, len, maxr; // init
- void calP() {
- int i;
- sol one;
- one.size = j;
- one.dista = len;
- for (i = ; i < one.size; i++) {
- one.route[i] = r[i];
- }
- sols.push_back(one);
- }
- void dfs(int now) {
- if (now == dist) {
- calP();
- return;
- }
- int i, l;
- l = adjs[now];
- for (i = ; i < l; i++) {
- if (!visited[adj[now][i]]) {
- r[j++] = adj[now][i];
- visited[adj[now][i]] = true;
- len += path[now][adj[now][i]];
- if (len <= maxr) {
- dfs(adj[now][i]);
- }
- len -= path[now][adj[now][i]];
- visited[adj[now][i]] = false;
- j--;
- }
- }
- }
- bool cmp(const sol& a, const sol& b) {
- if (a.dista > b.dista) {
- return false;
- }
- if (a.dista == b.dista) {
- int minl = a.size;
- int i;
- if (minl > b.size) {
- minl = b.size;
- }
- for (i = ; i < minl; i++) {
- if (a.route[i] > b.route[i])
- return false;
- if (a.route[i] < b.route[i])
- return true;
- }
- return a.size < b.size;
- }
- return true;
- }
- int main() {
- int nv, nr, i, f, t, d, ncase = , k;
- while (cin >> nv && nv != -) {
- cin >> nr;
- sols.clear();
- memset(path, , sizeof(path));
- memset(adj, , sizeof(adj));
- memset(adjs, , sizeof(adjs));
- memset(visited, , sizeof(visited));
- for (i = ; i < nr; i++) {
- cin >> f >> t >> d;
- adj[t][adjs[t]++] = f;
- adj[f][adjs[f]++] = t;
- path[t][f] = path[f][t] = d;
- }
- cin >> src >> dist;
- cin >> maxr;
- j = len = ;
- visited[src] = true;
- r[j++] = src;
- dfs(src);
- sort(sols.begin(), sols.end(), cmp);
- if (ncase != ) {
- cout << endl;
- }
- cout << "Case " << ++ncase << ":\n";
- for (i = ; i < sols.size(); i++) {
- cout << " " << sols[i].dista << ":";
- for (k = ; k < sols[i].size; k++) {
- cout << " " << sols[i].route[k];
- }
- cout << endl;
- }
- if (sols.size() == ) {
- cout << " NO ACCEPTABLE TOURS" << endl;
- }
- }
- }
复杂度:
图的节点数为V,边数为E, 解个数为n。
时间:初始化(E) + 深搜(E*V) + 排序(n*lg(n))
空间:V^2
标签:
深搜
soj 1015 Jill's Tour Paths 解题报告的更多相关文章
- 【九度OJ】题目1015:还是A+B 解题报告
[九度OJ]题目1015:还是A+B 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1015 题目描述: 读入两个小于10000的正整 ...
- 【LeetCode】257. Binary Tree Paths 解题报告(java & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...
- 【LeetCode】576. Out of Boundary Paths 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 状态搜索 记忆化搜索 相似题目 参考资料 ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- LeetCode: Unique Paths 解题报告
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- 洛谷 P2860 [USACO06JAN]冗余路径Redundant Paths 解题报告
P2860 [USACO06JAN]冗余路径Redundant Paths 题目描述 为了从F(1≤F≤5000)个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们 ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- [置顶] 刘汝佳《训练指南》动态规划::Beginner (25题)解题报告汇总
本文出自 http://blog.csdn.net/shuangde800 刘汝佳<算法竞赛入门经典-训练指南>的动态规划部分的习题Beginner 打开 这个专题一共有25题,刷完 ...
随机推荐
- 读取bmp图片数据
public void getBMPImage(String source) throws Exception { clearNData(); //清除数据保存区 FileInputStream fs ...
- 从网页上抓取Windows补丁信息然后整型输出(Python)
Powershell实现:http://www.cnblogs.com/IvanChen/p/4488246.html 今天通过Python实现: # coding=utf-8 import re i ...
- 【5集iCore3_ADP演示视频】5-4 iCore3与应用开发平台的组装与拆卸
iCore3双核心应用开发平台基于iCore3双核心板,包含ARM.FPGA.7寸液晶屏.双通道数字示波器.任意波发生器.电压表等模块,是一款专为电子爱好者设计的综合性电子学习系统. [视频简介]本视 ...
- PHP 水印设置
一.图片水印 <?php /* 覆盖水印 */ $image = ImageCreateFromJPEG('memcached.jpg'); $stamp = ImageCreateFromPN ...
- ios-UserDefaults
//单例设计模式 /* 1.单例是一种设计模式 是开发人员在开发过程中总结出来的简单方法 2. 如果某个对象在整个工程中有且只有一个(唯一的)就必须使用单例设计模式创建该对象 3.单例设计模式创建的对 ...
- 如何使用Google Map API开发Android地图应用
两年前开发过的GoogleMap已经大变样,最近有项目要用到GoogleMap,重新来配置Android GoogleMap开发环境,还真是踩了不少坑. 一.下载Android SDK Manager ...
- c# TimeSpan
转自:http://blog.163.com/y_p_xu/blog/static/17085710220116472030543/ /// <summary> /// 将时 ...
- 记录一次Tomcat内存泄露原因的追溯
现象:WEB无法访问.SSH无法登陆.桌面登陆验证失败. 重启服务器后登陆正常. cat /var/log/message显示root用户创建了2000多个sessions后显示内存不足. 进入tom ...
- sprint3总结
经过了半个学期以来的sprint冲刺,并且充分学习了android开发后对项目有了更加充分的认识理解,开发速度自然而然就上来了,没有了上一个学期的懵懂,虽然开发起来还是比较困难,但是胜在有同组组员帮忙 ...
- 小技巧,关于OC打印指针地址和arc下的retaincount
CFGetRetainCount((__bridge CFTypeRef)self : 打印retainCount 打印指针的地址(不是指针指向对象的地址):NSLog(@"aStr指针内 ...