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 DISTC1 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[20];
    int size;
    int dista;
};
vector<sol> sols;
bool visited[21];
int adj[21][21];
int path[21][21];
int adjs[21];
int r[20];
int j, src, dist, len, maxr; // init

void calP() {
    int i;
    sol one;
    one.size = j;
    one.dista = len;
    for (i = 0; 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 = 0; 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 = 0; 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 = 0, k;
    while (cin >> nv && nv != -1) {
        cin >> nr;
        sols.clear();
        memset(path, 0, sizeof(path));
        memset(adj, 0, sizeof(adj));
        memset(adjs, 0, sizeof(adjs));
        memset(visited, 0, sizeof(visited));
        for (i = 0; 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 = 0;
        visited[src] = true;
        r[j++] = src;
        dfs(src);
        sort(sols.begin(), sols.end(), cmp);
        if (ncase != 0) {
            cout << endl;
        }
        cout << "Case " << ++ncase << ":\n";
        for (i = 0; i < sols.size(); i++) {
            cout << " " << sols[i].dista << ":";
            for (k = 0; k < sols[i].size; k++) {
                cout << " " << sols[i].route[k];
            }
            cout << endl;
        }
        if (sols.size() == 0) {
            cout << " NO ACCEPTABLE TOURS" << endl;
        }
    }
}

复杂度:

图的节点数为V,边数为E, 解个数为n。

时间:初始化(E) + 深搜(E*V) + 排序(n*lg(n))

空间:V^2

深搜

soj 1015 Jill's Tour Paths 解题报告

时间: 2024-12-18 00:13:56

soj 1015 Jill's Tour Paths 解题报告的相关文章

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 robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in t

【LeetCode】257. Binary Tree Paths 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51474128 Subject 出处:https://leetcode.com/problems/binary-tree-paths/ Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-le

【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 robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish'

LeetCode: Unique Paths II 解题报告

Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty spac

【LeetCode】Unique Paths II 解题报告

[题目] Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the

解题报告 之 POJ2135 Farm Tour

解题报告 之 POJ2135 Farm Tour Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000) fields numbered 1..N, the first of which contains his house and the Nth of which contains the big barn.

leetCode解题报告5道题(十)

Disk Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2368    Accepted Submission(s): 333 Problem Description 有很多从磁盘读取数据的需求,包括顺序读取.随机读取.为了提高效率,需要人为安排磁盘读取.然而,在现实中,这种做法很复杂.我们考虑一个相对简单的场景.磁

leetCode解题报告5道题(十一)

题目一:Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2]

POJ3728 The merchant解题报告

Description There are N cities in a country, and there is one and only one simple path between each pair of cities. A merchant has chosen some paths and wants to earn as much money as possible in each path. When he move along a path, he can choose on