Sicily 1015. Jill's Tour Paths
来源:互联网 发布:高速数据采集存储 编辑:程序博客网 时间:2024/05/16 06:21
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 51 2 21 3 31 4 12 3 23 4 41 344 51 2 21 3 31 4 12 3 23 4 41 4105 71 2 21 4 52 3 12 4 22 5 33 4 33 5 21 385 71 2 21 4 52 3 12 4 22 5 33 4 33 5 21 31-1
Sample Output
Case 1: 3: 1 3 4: 1 2 3Case 2: 1: 1 4 7: 1 3 4 8: 1 2 3 4Case 3: 3: 1 2 3 7: 1 2 4 3 7: 1 2 5 3 8: 1 4 2 3 8: 1 4 3Case 4: NO ACCEPTABLE TOURS
// Problem#: 1015// Submission#: 3176449// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <algorithm>#include <iostream>#include <string>#include <stdio.h>#include <queue>#include <string.h>#include <vector>#include <iomanip>#include <map>#include <stack>#include <functional>#include <list>#include <cmath>using namespace std;#define INF 999999struct road { int sum; vector<int> r; bool used[25]; void init() { sum = 0; for (int i = 0; i < 25; i++) used[i] = false; }};int G[25][25];int n;int start, endp, Max;vector<road> ans;void BFS() { ans.clear(); queue<road> q; road temp; temp.init(); temp.r.push_back(start); temp.used[start] = true; q.push(temp); while (!q.empty()) { road temp = q.front(); q.pop(); if (temp.r[temp.r.size() - 1] == endp && temp.sum <= Max) { ans.push_back(temp); continue; } for (int i = 1; i <= n; i++) { if (G[temp.r[temp.r.size() - 1]][i] != INF && !temp.used[i]) { road newtemp = temp; newtemp.sum += G[temp.r[temp.r.size() - 1]][i]; newtemp.r.push_back(i); newtemp.used[i] = true; if (newtemp.sum <= Max) q.push(newtemp); } } }}bool cmp(const road & r1, const road & r2) { if (r1.sum != r2.sum) return r1.sum < r2.sum; else return r1.r < r2.r;}int main() { std::ios::sync_with_stdio(false); int counter = 1; while (1) { cin >> n; if (n == -1) break; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { G[i][j] = INF; } } int m; cin >> m; for (int i = 0; i < m; i++) { int s, e, c; cin >> s >> e >> c; G[s][e] = G[e][s] = c; } cin >> start >> endp >> Max; BFS(); sort(ans.begin(), ans.end(), cmp); if (counter != 1) cout << endl; cout << "Case " << counter << ":" << endl; counter++; if (ans.size() == 0) { cout << " NO ACCEPTABLE TOURS" << endl; } else { for (int i = 0; i < ans.size(); i++) { cout << " " << ans[i].sum << ":"; for (int j = 0; j < ans[i].r.size(); j++) { cout << " " << ans[i].r[j]; } cout << endl; } } } //getchar(); //getchar(); return 0;}
- Sicily 1015. Jill's Tour Paths
- pku 2197 Jill's Tour Paths
- poj 2197 hoj 2027 Jill's Tour Paths 搜索
- poj2197||zoj2406 Jill's Tour Paths(回溯+优先队列)
- Sicily 13062. SubDiagonal Paths
- Sicily 1450. Tour de France
- poj 1878 Jill's Bike (广搜)
- usaco Betsy's Tour
- Tony's Tour poj1739
- Betsy's Tour
- tony's tour
- POJ1739 Tony's Tour
- CodeforceF. Wizard's Tour
- 【Codeforces858F】Wizard's Tour
- sicily 1441 pie[S]
- A Chinese Knight's Tour
- 【插头DP】Tony's Tour
- poj 1739 Tony's Tour
- 数据库第二章习题
- 快排序 挖坑填数
- C语言该怎么入门
- LeetCode OJ Recover Binary Search Tree
- LeetCode OJ Convert Sorted Array to Binary Search
- Sicily 1015. Jill's Tour Paths
- Sicily 1193. Up the Stairs
- BZOJ 1880 Sdoi2009 Elaxia的路线 SPFA+拓扑排序
- zedboard+opencv+QT+车牌识别进度情况记录
- hdu-2888 Check Corners
- 最适合学英语的50部动画电影(高清画质mkv,双语/双字幕可切换)
- java语言GUI编程之TextField持有对方引用
- JAVA学习笔记(五)- 数组
- 机器学习入门:线性回归及梯度下降