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 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

题意: 输入我们当前的位置和目的地,在线地图可以推荐几条路径。现在,您的工作是向用户推荐两条路径:一条路径最短,另一条路线最快。保证任何请求都存在路径。

分析: 两次dijkstra -比较麻烦

代码


#include "vector"
using namespace std;
const int inf=999999999;
int e[510][510],w[510][510],dis[510],Time[510],weight[510],pre[510];
vector<int> dispath,temppath,timepath,timepre[510];
bool visit[510];
int n,m,sou,des,v1,v2,one_way,length,t;
void (int x){
dispath.push_back(x);
if (x==sou)return;
dfs_dis(pre[x]);
}
int minnode=inf;
void dfs_time(int x){
temppath.push_back(x);
if (x==sou)
{
if (temppath.size()<minnode)
{
minnode=temppath.size();
timepath=temppath;
}
temppath.pop_back();
return;
}
for (int i=0;i<timepre[x].size();i++)
dfs_time(timepre[x][i]);
temppath.pop_back();
}
void dijkstra_dis(int s){
dis[s]=0;
weight[s]=0;
for (int i=0;i<n;i++)
pre[i]=i;
for (int i=0;i<n;i++)
{
int u=-1,MIN=inf;
for (int j=0;j<n;j++)
{
if (visit[j]==false&&dis[j]<MIN)
{
MIN=dis[j];
u=j;
}
}
if (u==-1)break;
visit[u]=true;
for (int v=0;v<n;v++)
{
if (visit[v]==false&&e[u][v]!=inf)
{
if (e[u][v]+dis[u]<dis[v])
{
dis[v]=e[u][v]+dis[u];
weight[v]=weight[u]+w[u][v];
pre[v]=u;
}else if(dis[v]==e[u][v]+dis[u]&&weight[u]+w[u][v]<weight[v]){
weight[v]=weight[u]+w[u][v];
pre[v]=u;
}
}
}
}
}
void dijkstra_time(int s){
Time[s]=0;
for (int i=0;i<n;i++)
{
int u=-1,MIN=inf;
for (int j=0;j<n;j++)
{
if (visit[j]==false&&MIN>Time[j])
{
MIN=Time[j];
u=j;
}
}
if (u==-1)break;
visit[u]=true;
for (int v=0;v<n;v++)
{
if (visit[v]==false&&w[u][v]!=inf)
{
if (w[u][v]+Time[u]<Time[v])
{
Time[v]=w[u][v]+Time[u];
timepre[v].clear();
timepre[v].push_back(u);
}else if (Time[v]==w[u][v]+Time[u])
timepre[v].push_back(u);
}
}
}
}
int main(){
fill(visit,visit+510,false);
fill(e[0],e[0]+510*510,inf);
fill(w[0],w[0]+510*510,inf);
fill(weight,weight+510,inf);
fill(dis,dis+510,inf);
fill(Time,Time+510,inf);
cin>>n>>m;
for (int i=0;i<m;i++)
{
scanf("%d %d %d %d %d",&v1,&v2,&one_way,&length,&t);
if (one_way!=1)
{
e[v1][v2]=e[v2][v1]=length;
w[v1][v2]=w[v2][v1]=t;
}else if (one_way==1)
{
e[v1][v2]=length;
w[v1][v2]=t;
}
}
cin>>sou>>des;
dijkstra_dis(sou);
dfs_dis(des);
fill(visit,visit+510,false);
dijkstra_time(sou);
dfs_time(des);
printf("Distance = %d",dis[des]);
if (timepath==dispath)
{
printf("; Time = %d: ",Time[des]);
for (int i=timepath.size()-1;i>=0;i--)
{
cout<<timepath[i];
if (i!=0)cout<<" -> ";
}
}else{
printf(": ");
for (int i=dispath.size()-1;i>=0;i--)
{
cout<<dispath[i];
if (i!=0)cout<<" -> ";
}
cout<<endl;
printf("Time = %d: ",Time[des]);
for (int i=timepath.size()-1;i>=0;i--)
{
cout<<timepath[i];
if (i!=0)cout<<" -> ";
}
}
return 0;
}