PAT 1111 Online Map(30 分)

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

思路:直接用dfs,但是最后一个测试点会超时。

程序:

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n,m;
int dist[505][505];
int times[505][505];
int minDist = 100000000;
int now_dist = 0;
vector<int> shortest_road;
vector<int> fastest_road;
vector<int> road1;//唯一的最短路
vector<int> road2;//唯一的用时最少的路
int minTime = 100000000;
int now_time = 0;
int minTime2 = 100000000;
int now_time2 = 0;
int vis[500];
void dfs(int start,int end)
{
  if(start == end)
  {
    if(now_dist < minDist)
    {
      minTime = now_time;
      minDist = now_dist;
      road1 = shortest_road;
    }
    else if(now_dist == minDist)
    {
      if(now_time < minTime)
      {
        minTime = now_time;
        road1 = shortest_road;
      }
    }
    if(now_time2 < minTime2)
    {
      minTime2 = now_time2;
      road2 = fastest_road;
    }
    else if(now_time2 == minTime2)
    {
      if(road2.size() > fastest_road.size())
        road2 = fastest_road;
    }
    return;
  }
  for(int i = 0; i < n; i++)
  {
    if(dist[start][i] && !vis[i])
    {
      vis[i] = 1;
      now_dist += dist[start][i];
      shortest_road.push_back(i);
      now_time += times[start][i];
      fastest_road.push_back(i);
      now_time2 += times[start][i];
      dfs(i,end);
      now_time2 -= times[start][i];
      fastest_road.pop_back();
      now_dist -= dist[start][i];
      vis[i] = 0;
      now_time -= times[start][i];
      shortest_road.pop_back();
    }
  }
}
int main()
{
  scanf("%d%d",&n,&m);
  for(int i = 0; i < m; i++)
  {
    int a,b,one_way,len,time;
    scanf("%d%d%d%d%d",&a,&b,&one_way,&len,&time);
    if(one_way)
    {
      dist[a][b] = len;
      times[a][b] = time;
    }
    else
    {
      dist[a][b] = dist[b][a] = len;
      times[a][b] = times[b][a] = time;
    }
  }
  int start,end;
  scanf("%d%d",&start,&end);
  vis[start] = 1;
  dfs(start,end);
  bool flag = true;
  if(road1.size() == road2.size())
  {
    for(int i = 0; i < road1.size(); i++)
    {
      if(road1[i] != road2[i])
      {
        flag = false;
        break;
      }
    }
  }
  else
    flag = false;
  if(!flag)
  {
    printf("Distance = %d:",minDist);
    printf(" %d",start);
    for(int i = 0; i < road1.size(); i++)
    {
      printf(" -> %d",road1[i]);
    }
    printf("\n");
    printf("Time = %d: %d",minTime2,start);
    for(int i = 0; i < road2.size(); i++)
    {
      printf(" -> %d",road2[i]);
    }
    printf("\n");
  }
  else
  {
    printf("Distance = %d; Time = %d: %d",minDist,minTime2,start);
    for(int i = 0; i < road1.size(); i++)
    {
      printf(" -> %d",road1[i]);
    }
    printf("\n");
  }
  return 0;
}

参考别人的代码,用的dijkstra;

程序:

#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int inf = 99999999;
int dis[510],Time[510],e[510][510],w[510][510],dispre[510],weight[510];
int vis[510];
vector<int> dispath,Timepath,temppath,Timepre[510];
int st,fin,minnode = inf;
void dfsdispath(int v)
{
  dispath.push_back(v);
//  printf("%d ",v);
  if(v == st) return;
  dfsdispath(dispre[v]);
}
void dfsTimepath(int v)
{
  temppath.push_back(v);
  if(v == st)
  {
    if(temppath.size() < minnode)
    {
      minnode = temppath.size();
      Timepath = temppath;
    }
  }
  for(int i = 0; i < Timepre[v].size(); i++)
    dfsTimepath(Timepre[v][i]);
  temppath.pop_back();
}
int main()
{
  fill(dis,dis+510,inf);
  fill(Time,Time+510,inf);
  fill(weight,weight+510,inf);
  fill(e[0],e[0]+510*510,inf);
  fill(w[0],w[0]+510*510,inf);
  int n,m;
  scanf("%d%d",&n,&m);
  int a,b,flag,len,t;
  for(int i = 0; i < m; i++)
  {
    scanf("%d%d%d%d%d",&a,&b,&flag,&len,&t);
    e[a][b] = len;
    w[a][b] = t;
    if(flag != 1)
    {
      e[b][a] = len;
      w[b][a] = t;
    }
  }
  scanf("%d%d",&st,&fin);
  dis[st] = 0;
  weight[st] = 0;
  //Dijsktra
  for(int i = 0; i < n; i++)
    dispre[i] = i;
  for(int i = 0; i < n; i++)
  {
    int u = -1,minn = inf;
    for(int j = 0; j < n; j++)
    {
      if(!vis[j] && dis[j] < minn)
      {
        minn = dis[j];
        u = j;
      }
    }
    if(u == -1) break;
    vis[u] = 1;
    for(int j = 0; j < n; j++)
    {
      if(!vis[j] && e[u][j] != inf)
      {
        if(e[u][j] + dis[u] < dis[j])
        {
          dis[j] = e[u][j] + dis[u];
          dispre[j] = u;
          weight[j] = weight[u] + w[u][j];
        }
        else if(e[u][j] + dis[u] == dis[j] && weight[u] + w[u][j] < weight[j])
        {
          weight[j] = weight[u] + w[u][j];
          dispre[j] = u;
        }
      }
    }
  }
  dfsdispath(fin);
  Time[st] = 0;
  fill(vis,vis+510,0);
  for(int i = 0; i < n; i++)
  {
    int u = -1,minn = inf;
    for(int j = 0; j < n; j++)
    {
      if(!vis[j] && minn > Time[j])
      {
        u = j;
        minn = Time[j];
      }
    }
    if(u == -1) break;
    vis[u] = 1;
    for(int j = 0; j < n; j++)
    {
      if(!vis[j] && w[u][j] != inf)
      {
        if(Time[u] + w[u][j] < Time[j])
        {
          Time[j] = Time[u] + w[u][j];
          Timepre[j].clear();
          Timepre[j].push_back(u);
        }
        else if(Time[u] + w[u][j] == Time[j])
        {
          Timepre[j].push_back(u);
        }
      }
    }
  }
  dfsTimepath(fin);
  printf("Distance = %d",dis[fin]);
  if(dispath == Timepath)
    printf("; Time = %d: ",Time[fin]);
  else
  {
    printf(": ");
    for(int i = dispath.size() - 1; i >= 0; i--)
    {
      printf("%d",dispath[i]);
      if(i != 0)  printf(" -> ");
    }
    printf("\nTime = %d: ",Time[fin]);
  }
  for(int i = Timepath.size() - 1; i >= 0; i--)
  {
    printf("%d",Timepath[i]);
    if(i != 0)  printf(" -> ");
  }
  return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值