PAT (Advanced Level) Practise 1131 Subway Map (30)

1131. Subway Map (30)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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.

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 (i = 1, ..., N) 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 (i = 1, ... M) 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] (i = 1, ..., M-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.

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.

又TM最短路,这种题神烦,这次是先bfs确定最短的,再dfs找出换站最小的输出,讲道理可能会超时的,不过pat的数据不强可以随便过。

#include   
#include  
#include    
#include   
#include
#include     
#include    
#include    
#include        
#include      
#include    
#include        
#include    
using namespace std;
#define ms(x,y) memset(x,y,sizeof(x))        
#define rep(i,j,k) for(int i=j;i<=k;i++)        
#define per(i,j,k) for(int i=j;i>=k;i--)        
#define loop(i,j,k) for (int i=j;i!=-1;i=k[i])        
#define inone(x) scanf("%d",&x)        
#define intwo(x,y) scanf("%d%d",&x,&y)        
#define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z)      
#define infou(x,y,z,p) scanf("%d%d%d%d",&x,&y,&z,&p)     
#define lson x<<1,l,mid    
#define rson x<<1|1,mid+1,r    
#define mp(i,j) make_pair(i,j)    
#define ff first    
#define ss second    
typedef long long LL;
typedef pair pii;
const int low(int x) { return x&-x; }
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 1e5 + 50;
const double eps = 1e-10;
int T, n, m, x, y, d[N];
int ft[N], nt[N], u[N], v[N], sz, now;
pii a[N], b[N];

void dfs(int x, int y, int z, int sz, int g)
{
  if (x == y) {
    if (now > g) {
      now = g;
      rep(i, 0, sz - 1) b[i] = a[i];
    }
  }
  if (sz == d[y] - 1) return;
  loop(i, ft[x], nt)
  {
    if (d[u[i]] != d[x] + 1) continue;
    a[sz] = mp(u[i], v[i]);
    dfs(u[i], y, v[i], sz + 1, g + (int)(z != v[i]));
  }
}

void bfs(int x, int y)
{
  queue p; p.push(x);
  rep(i, 0, 9999) d[i] = 0;
  d[x] = 1;
  while (!p.empty())
  {
    int q = p.front(); p.pop();
    loop(i, ft[q], nt)
    {
      if (d[u[i]]) continue;
      p.push(u[i]); d[u[i]] = d[q] + 1;
    }
  }

  now = INF;
  dfs(x, y, 0, 0, 0);
  printf("%d\n", d[y] - 1);
  int g = x;
  b[d[y] - 1] = mp(0, 0);
  rep(i, 0, d[y] - 2)
  {
    if (b[i].ss == b[i + 1].ss) continue;
    printf("Take Line#%d from %04d to %04d.\n", b[i].ss, x, b[i].ff);
    x = b[i].ff;
  }
}

int main()
{
  inone(n);
  ms(ft, -1);
  rep(i, 1, n)
  {
    intwo(m, x);
    rep(j, 2, m)
    {
      inone(y);
      u[sz] = y; v[sz] = i; nt[sz] = ft[x]; ft[x] = sz++;
      u[sz] = x; v[sz] = i; nt[sz] = ft[y]; ft[y] = sz++;
      x = y;
    }
  }
  inone(n);
  rep(i, 1, n)
  {
    intwo(x, y);
    bfs(x, y);
  }
  return 0;
}


你可能感兴趣的:(----最短路,PAT甲级)