天天看点

PAT (Advanced Level) Practise 1111 Online Map (30)

1111. Online Map (30)

时间限制

300 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is guaranteed that a path exists for any request.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (2 <= N <= 500), and M, being the total number of streets intersections on a map, and the number of streets, respectively. Then M lines follow, each describes a street in the format:

V1 V2 one-way length time

where V1 and V2 are the indices (from 0 to N-1) of the two ends of the street; one-way is 1 if the street is one-way from V1 to V2, or 0 if not;length is the length of the street; and time is the time taken to pass the street.

Finally a pair of source and destination is given.

Output Specification:

For each case, first print the shortest path from the source to the destination with distance D in the format:

Distance = D: source -> v1 -> ... -> destination

Then in the next line print the fastest path with total time T:

Time = T: source -> w1 -> ... -> destination

In case the shortest path is not unique, output the fastest one among the shortest paths, which is guaranteed to be unique. In case the fastest path is not unique, output the one that passes through the fewest intersections, which is guaranteed to be unique.

In case the shortest and the fastest paths are identical, print them in one line in the format:

Distance = D; Time = T: source -> u1 -> ... -> destination

Sample Input 1:

10 15

0 1 0 1 1

8 0 0 1 1

4 8 1 1 1

3 4 0 3 2

3 9 1 4 1

0 6 0 1 1

7 5 1 2 1

8 5 1 2 1

2 3 0 2 2

2 1 1 1 1

1 3 0 3 1

1 4 0 1 1

9 7 1 3 1

5 1 0 5 2

6 5 1 1 2

3 5

Sample Output 1:

Distance = 6: 3 -> 4 -> 8 -> 5

Time = 3: 3 -> 1 -> 5

Sample Input 2:

7 9

0 4 1 1 1

1 6 1 1 3

2 6 1 1 1

2 5 1 2 2

3 0 0 1 1

3 1 1 1 3

3 2 1 1 2

4 5 0 2 2

6 5 1 1 2

3 5

Sample Output 2:

Distance = 3; Time = 4: 3 -> 2 -> 5

求两种最短路,并输出路径,考试的时候题目读错了,以为两种最短路条件刚好是互补的,结果第二种是要经过点最少的,坑了我一个多小时,英语是硬伤。

#include<cstdio>
#include<vector>
#include<queue>
#include<string>
#include<map>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int INF = 0x7FFFFFFF;
const int maxn = 5e2 + 10;
int n, m, x, y, z, S, T, dm, tm;
int d[maxn][maxn], t[maxn][maxn];
int dis[maxn], cost[maxn], vis[maxn];
vector<int> ans[2];

void dfs(int x)
{
  if (x == S) { ans[0].push_back(x); return; }
  for (int i = 0; i < n; i++)
  {
    if (!d[i][x]) continue;
    if (dis[x] != dis[i] + d[i][x]) continue;
    if (cost[x] != cost[i] + t[i][x]) continue;
    dfs(i);
    ans[0].push_back(x);
  }
}

void Dfs(int x)
{
  if (x == S) { ans[1].push_back(x); return; }
  for (int i = 0; i < n; i++)
  {
    if (!t[i][x]) continue;
    if (dis[x] != dis[i] + t[i][x]) continue;
    if (cost[x] != cost[i] + 1) continue;
    Dfs(i);
    ans[1].push_back(x);
  }
}

int main()
{
  scanf("%d%d", &n, &m);
  while (m--)
  {
    scanf("%d%d%d", &x, &y, &z);
    scanf("%d%d", &d[x][y], &t[x][y]);
    if (!z) d[y][x] = d[x][y], t[y][x] = t[x][y];
  }
  scanf("%d%d", &S, &T);
  memset(dis, -1, sizeof(dis));
  memset(vis, 0, sizeof(vis));
  dis[S] = 0;
  while (true)
  {
    int now = -1;
    for (int i = 0; i < n; i++)
    {
      if (vis[i] || dis[i] == -1) continue;
      if (now == -1 || dis[now] > dis[i]) now = i;
    }
    if (now == -1) break;
    vis[now] = 1;
    for (int i = 0; i < n; i++)
    {
      if (!d[now][i]) continue;
      if (dis[i] == -1 || dis[now] + d[now][i] < dis[i])
      {
        dis[i] = dis[now] + d[now][i];
        cost[i] = cost[now] + t[now][i];
      }
      else if (dis[now] + d[now][i] == dis[i])
      {
        cost[i] = min(cost[i], cost[now] + t[now][i]);
      }
    }
  }
  dfs(T); dm = dis[T];
  memset(dis, -1, sizeof(dis));
  memset(vis, 0, sizeof(vis));
  dis[S] = 0; cost[S] = 0;
  while (true)
  {
    int now = -1;
    for (int i = 0; i < n; i++)
    {
      if (vis[i] || dis[i] == -1) continue;
      if (now == -1 || dis[now] > dis[i]) now = i;
    }
    if (now == -1) break;
    vis[now] = 1;
    for (int i = 0; i < n; i++)
    {
      if (!t[now][i]) continue;
      if (dis[i] == -1 || dis[now] + t[now][i] < dis[i])
      {
        dis[i] = dis[now] + t[now][i];
        cost[i] = cost[now] + 1;
      }
      else if (dis[now] + t[now][i] == dis[i])
      {
        cost[i] = min(cost[i], cost[now] + 1);
      }
    }
  }
  Dfs(T); tm = dis[T];
  if (ans[0] == ans[1])
  {
    printf("Distance = %d; Time = %d:", dm, tm);
    for (int i = 0; i < ans[0].size(); i++)
    {
      printf(" %d", ans[0][i]);
      if (i == ans[0].size() - 1) printf("\n");
      else printf(" ->");
    }
  }
  else
  {
    printf("Distance = %d:", dm);
    for (int i = 0; i < ans[0].size(); i++)
    {
      printf(" %d", ans[0][i]);
      if (i == ans[0].size() - 1) printf("\n");
      else printf(" ->");
    }
    printf("Time = %d:", tm);
    for (int i = 0; i < ans[1].size(); i++)
    {
      printf(" %d", ans[1][i]);
      if (i == ans[1].size() - 1) printf("\n");
      else printf(" ->");
    }
  }
  return 0;
}