zoukankan      html  css  js  c++  java
  • 1131 Subway Map (30 分)

    1131 Subway Map (30 分)
     

    In the big cities, the subway systems always look so complex to the visitors. To give you some sense, the following figure shows the map of Beijing subway. Now you are supposed to help people with your computer skills! Given the starting position of your user, your task is to find the quickest way to his/her destination.

    subwaymap.jpg

    Input Specification:

    Each input file contains one test case. For each case, the first line contains a positive integer N (≤ 100), the number of subway lines. Then N lines follow, with the i-th (,) line describes the i-th subway line in the format:

    M S[1] S[2] ... S[M]

    where M (≤ 100) is the number of stops, and S[i]'s (,) are the indices of the stations (the indices are 4-digit numbers from 0000 to 9999) along the line. It is guaranteed that the stations are given in the correct order -- that is, the train travels between S[i] and S[i+1] (,) without any stop.

    Note: It is possible to have loops, but not self-loop (no train starts from S and stops at S without passing through another station). Each station interval belongs to a unique subway line. Although the lines may cross each other at some stations (so called "transfer stations"), no station can be the conjunction of more than 5 lines.

    After the description of the subway, another positive integer K (≤ 10) is given. Then K lines follow, each gives a query from your user: the two indices as the starting station and the destination, respectively.

    The following figure shows the sample map.

    samplemap.jpg

    Note: It is guaranteed that all the stations are reachable, and all the queries consist of legal station numbers.

    Output Specification:

    For each query, first print in a line the minimum number of stops. Then you are supposed to show the optimal path in a friendly format as the following:

    Take Line#X1 from S1 to S2.
    Take Line#X2 from S2 to S3.
    ......
    

    where Xi's are the line numbers and Si's are the station indices. Note: Besides the starting and ending stations, only the transfer stations shall be printed.

    If the quickest path is not unique, output the one with the minimum number of transfers, which is guaranteed to be unique.

    Sample Input:

    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
    3
    3011 3013
    6666 2001
    2004 3001
    

    Sample Output:

    2
    Take Line#3 from 3011 to 3013.
    10
    Take Line#4 from 6666 to 1306.
    Take Line#3 from 1306 to 2302.
    Take Line#2 from 2302 to 2001.
    6
    Take Line#2 from 2004 to 1204.
    Take Line#1 from 1204 to 1306.
    Take Line#3 from 1306 to 3001.

    地铁模拟路线,dfs就能直接走出来。

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 int n, m, p, x = 0, y = 0;
     4 int vis[10000][10000];
     5 vector<int> v[10000], vt, path;
     6 int val[10000];
     7 int minlen, minpath; 
     8 int getpath(vector<int> a){
     9     int cnt = 0;
    10     int present = 0;
    11     for(int i = 1; i < a.size(); i++){
    12         if(vis[a[i-1]][a[i]] != present)
    13             cnt++;
    14         present = vis[a[i-1]][a[i]];
    15     }
    16     return cnt;
    17 }
    18 void dfs(int st, int k){
    19     if((st == y) && ((k < minlen)||(k == minlen && minpath > getpath(path)))){
    20         minlen = k;
    21         minpath = getpath(path);
    22         vt = path;
    23         return ;
    24     }
    25     for(int i = 0; i < v[st].size(); i++){
    26         if(val[v[st][i]]) continue;
    27         val[v[st][i]] = 1;
    28         path.push_back(v[st][i]);
    29         dfs(v[st][i], k+1);
    30         path.pop_back();
    31         val[v[st][i]] = 0;
    32     }
    33 }
    34 int main(){
    35     scanf("%d", &n);
    36     for(int i = 1; i <= n; i++){
    37         scanf("%d", &p);
    38         for(int j = 0; j < p; j++){
    39             y = x;
    40             scanf("%d", &x);
    41             if(j){
    42                 vis[x][y] = vis[y][x] = i;
    43                 v[x].push_back(y);
    44                 v[y].push_back(x);
    45             }
    46         }
    47     }
    48     scanf("%d", &m);
    49     while(m--){
    50         scanf("%d%d", &x,&y);
    51         minlen = 999999, minpath = 999999;
    52         path.clear();
    53         path.push_back(x);
    54         val[x] = 1;
    55         dfs(x, 0);
    56         val[x] = 0;
    57         printf("%d
    ", minlen);
    58         int line = 0, point = x;
    59         for(int i = 1; i < vt.size(); i++){
    60             if(vis[vt[i-1]][vt[i]] != line){
    61                 if(line != 0) printf("Take Line#%d from %04d to %04d.
    ", line, point, vt[i-1]);
    62                 point = vt[i-1];
    63                 line = vis[vt[i-1]][vt[i]];
    64             }
    65         }
    66         printf("Take Line#%d from %04d to %04d.
    ", line, point, y);
    67     }
    68     return 0;
    69 }




  • 相关阅读:
    【Delphi】VCL 使用TCoolBar控件,在系统触发UAC界面时,引发CMSysFontChanged事件导致界面卡死或弹出System Error 1400错误
    【Delphi】 FMX 下 TImageList的使用方法:获取其中一张图片
    如何在电脑睡眠状态下保持程序运行
    【Delphi】使用TIdHTTPServer开发HTTP服务端在Windows2008部署后,外网无法访问
    fedora 25 virtualbox 增强功能安装
    在Win8系统中如何将一般类型的文件放在开始菜单中
    mac上的替代软件
    spring boot 1.4.1 with jsp file sample
    macbook pro 重装系统
    找不到或无法加载主类 org.codehaus.plexus.classworlds.launcher.Launcher
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/11318081.html
Copyright © 2011-2022 走看看