L3-014 Travel around the world (30 points)

L3-014 Travel the world (30 points)

Traveling the world is a romantic thing, but planning a travel itinerary is not necessarily... There are thousands of air routes, railway lines, and bus lines around the world, which is dizzying. Therefore, the travel agency chooses some transportation companies to form an alliance, each company provides a route, and then helps customers plan travel routes supported by the companies in the alliance. This question asks you to help a travel agency implement an automatic route planning program that finds the smoothest route for any given starting and ending point. The so-called "smoothest" first refers to the least number of stops on the way; if there are the same number of stops, the route that requires the least number of transfers will be taken.

Input format:

The input gives a positive integer N (≤100) on the first line, the number of affiliate companies. Next there are N lines, the i-th line (i=1,...,N) describes the line provided by the i-th company. The format is:

M S[1] S[2] ⋯ S[M]

Where M (≤100) is the number of stops, and S[i] (i=1, ⋯, M) is the number of the stops (consisting of 4 digits from 0 to 9). It is assumed here that each line is simply a link that can operate in both directions, and that the input is guaranteed to be given in the correct order of stops - that is, any pair of adjacent S[i] and S[i +1] (i=1, ⋯, M−1), there are no other stops. We call the line between adjacent stations an operating section, and each operating section is contracted to only one company. Loop lines are possible, but will not return from the point of departure to the point of departure without stopping at any intermediate stops. Of course, the lines of different companies may cross at some stations, and these stations are the transfer points of customers. We assume that no more than 5 lines of different companies are involved in any transfer point.

After describing the alliance line, the title will give a positive integer K (≤10), followed by K lines, each line gives the needs of a customer, that is, the number of the origin and the number of the destination, with a space in the middle separated.

Output format:

Handle every customer's needs. If there is no existing line to get it to the destination, print "Sorry, no line is available." on one line; if the destination is reachable, print the number of stops on the smoothest route first (originating place and destination are not included), then give the itinerary in the following format:

Go by the line of company #X1 from S1 to S2.
Go by the line of company #X2 from S2 to S3.
......

Among them Xiis the number of the line contracting company and the number Siof the stop. However, only the origin, transfer point and destination must be output, and intermediate stops cannot be output. The subject guarantees that the route that meets the requirements is unique.

Input sample:

4
7 1001 3212 1003 1204 1005 1306 7797
9 9988 2333 1204 2006 2005 2004 2003 2302 2001
13 3011 3812 3013 3001 1306 3003 2333 3066 3212 3008 2302 3010 3011
4 6666 8432 4011 1306
4
3011 3013
6666 2001
2004 3001
2222 6666

Sample output:

2
Go by the line of company #3 from 3011 to 3013.
10
Go by the line of company #4 from 6666 to 1306.
Go by the line of company #3 from 1306 to 2302.
Go by the line of company #2 from 2302 to 2001.
6
Go by the line of company #2 from 2004 to 1204.
Go by the line of company #1 from 1204 to 1306.
Go by the line of company #3 from 1306 to 3001.
Sorry, no line is available.

Quite an interesting topic.

Solution: Because the line between adjacent sites only belongs to one company, you can use a vector container when storing this information. The container subscript is used as the starting site, and the structure is pushed into the container. The structure stores the phase of the starting site. The neighboring site and the company to which the two neighboring sites belong. When querying, just use violent search, start searching from the starting site, keep going to the adjacent sites that can be reached, go to the end point to compare and update and then backtrack, so that the final route that meets the conditions must be reached. . As for the route storage, two vectors can be used to store them separately. Whenever a transfer is performed (the company is changed), the new company and the starting station (transfer station) of the company are stored, that is to say, the vector Each item is a transfer station, and the route of each company that is finally output is the i-th (starting station) to the i+1-th (transfer station), so dfs needs a parameter nowc to save the previous When the station arrives at the company of the current station, in each search, judge whether the current company is the same as that of the current station to the next station, and then you can know whether the current station is a transfer station.

Code:

#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
int n, m, t, s, e, qs, qe, stationnum, companynum, vis[10000];
struct info {
  int next, company;//下一个站点编号,该区间所属公司编号
};
vector<info> q[10000];
vector<int> path, anspath, c, ansc;
void dfs(int now, int snum, int cnum, int nowc) {//当前站点编号,站点数量,涉及的公司数量,当前线路所属公司
  if(now == qe) {
    if(snum < stationnum || (snum == stationnum && cnum < companynum)) {
      stationnum = snum;
      companynum = cnum;
      ansc = c;
      anspath = path;
    }
    return;
  }
  for(int i = 0; i < q[now].size(); i++) {
    if(!vis[q[now][i].next]) {
      vis[q[now][i].next] = 1;
      if(nowc == q[now][i].company) dfs(q[now][i].next, snum+1, cnum, nowc);//当前站点和下一站点是同一公司的
      else {//不是同一个公司,说明需要换乘了
        c.push_back(q[now][i].company);//将换乘公司存入
        path.push_back(now);//存入当前站点,也就是所走线路上每次换乘()的第一个站点
        dfs(q[now][i].next, snum+1, cnum+1, q[now][i].company);
        c.pop_back();//回溯
        path.pop_back();
      }
      vis[q[now][i].next] = 0;
    }
  }
}
int main() {
  scanf("%d", &n);
  for(int i = 1; i <= n; i++) {
    scanf("%d", &m);
    scanf("%d", &qs);//先输入一个区间起点
    for(int j = 1; j < m; j++) {
      scanf("%d", &qe);//与区间起点相邻的区间终点
      struct info io;
      io.next = qe;
      io.company = i;
      q[qs].push_back(io);//存储了qs到qe区间(相邻两个站点)的信息
      io.next = qs;
      q[qe].push_back(io);//存储了qe到qs区间(相邻两个站点)的信息
      qs = qe;//滑动更新区间起点
    }
  }
  scanf("%d", &t);
  while(t--) {
    stationnum = inf;
    companynum = inf;
    memset(vis, 0, sizeof(vis));
    path.clear(), anspath.clear(), c.clear(), ansc.clear();
    scanf("%d%d", &qs, &qe);
    vis[qs] = 1;//标记访问数组,防止不断走环进入死循环
    dfs(qs, 0, 0, 0);
    if(stationnum == inf) printf("Sorry, no line is available.\n");
    else {
      printf("%d\n", stationnum);
      anspath.push_back(qe);//因为存的是每一次换乘线路的起点站,所以需要最后把终点存进去(终点之后是没有换乘的)
      for(int i = 0; i < companynum; i++)
        printf("Go by the line of company #%d from %04d to %04d.\n", ansc[i], anspath[i], anspath[i+1]);
    }
  }
  return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326526088&siteId=291194637