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 C1C2, 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;}                                 


0 0
原创粉丝点击