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
给定n个点,m条边,起始点和终点,每条边给定长度和花费的时间,如果one-way等于1则是单向边,0是双向边,输出最短路径和花费时间最少的路径,如果最短路不唯一输出在最短路中花费时间最少的那条,如果花费时间最少的路径不唯一,则输出经过点最少的那条。
运用两次dijkstra算法,第一次求出花费最少时间的最短路径,第二次求出花费最少时间的路径(可能多条)再用dfs选出经过点最少的那条。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=510,inf=1000000000;
int n,m,s,e;
int g[maxn][maxn],timee[maxn][maxn];
int dis[maxn],fast[maxn],pre[maxn];//pre是保存最短路径并且时间最短的路径
bool vis[maxn];
vector<int> pre1[maxn];//保存花费时间最少的路径
vector<int> path,temp,timepath;
void dijkstra(int s){
for(int i=0;i<n;i++) pre[i]=i;
memset(vis,false,sizeof(vis));
fill(dis,dis+n,inf);
dis[s]=0;
for(int i=0;i<n;i++){
int u=-1,minn=inf;
for(int j=0;j<n;j++){
if(dis[j]<minn&&vis[j]==false){
u=j;
minn=dis[j];
}
}
if(u==-1) return;
vis[u]=true;
for(int v=0;v<n;v++){
if(vis[v]==false&&g[u][v]>0){
if(dis[u]+g[u][v]<dis[v]){
dis[v]=dis[u]+g[u][v];
fast[v]=timee[u][v]+fast[u];
pre[v]=u;
}else if(dis[u]+g[u][v]==dis[v]&&fast[v]>timee[u][v]+fast[u]){
pre[v]=u;
fast[v]=timee[u][v]+fast[u];
}
}
}
}
}
void DFS(int v){
path.push_back(v);
if(v==s){
return;
}
DFS(pre[v]);
}
void dijkstra1(int s){
memset(vis,false,sizeof(vis));
fill(fast,fast+n,inf);
fast[s]=0;
for(int i=0;i<n;i++){
int u=-1,minn=inf;
for(int j=0;j<n;j++){
if(fast[j]<minn&&vis[j]==false){
u=j;
minn=fast[j];
}
}
if(u==-1) return;
vis[u]=true;
for(int v=0;v<n;v++){
if(vis[v]==false&&timee[u][v]>0){
if(fast[u]+timee[u][v]<fast[v]){
fast[v]=fast[u]+timee[u][v];
pre1[v].clear();
pre1[v].push_back(u);
}else if(fast[u]+timee[u][v]==fast[v]){
pre1[v].push_back(u);
}
}
}
}
}
int optcnt=inf;
void DFS1(int v,int cnt){
if(v==s){
temp.push_back(v);
if(cnt<optcnt){
timepath=temp;
optcnt=cnt;
}
temp.pop_back();
return;
}
temp.push_back(v);
for(int i=0;i<pre1[v].size();i++){
DFS1(pre1[v][i],cnt+1);
}
temp.pop_back();
}
int main(){
int a,b,way,len,t;
cin>>n>>m;
for(int i=0;i<m;i++){
cin>>a>>b>>way>>len>>t;
if(way!=1){
g[a][b]=g[b][a]=len;
timee[a][b]=timee[b][a]=t;
}else{
g[a][b]=len;timee[a][b]=t;
}
}
cin>>s>>e;
dijkstra(s);
DFS(e);
dijkstra1(s);
DFS1(e,0);
if(path==timepath){
printf("Distance = %d; ",dis[e]);
}else{
printf("Distance = %d: %d",dis[e],s);
for(int i=path.size()-2;i>=0;i--){
printf(" -> %d",path[i]);
}
printf("\n");
}
printf("Time = %d: %d",fast[e],s);
for(int i=timepath.size()-2;i>=0;i--){
printf(" -> %d",timepath[i]);
}
printf("\n");
return 0;
}