顽强的小白
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
题目解析
图的题目,给出图,边有单向的有双向的,边权有两个,一个是长度,一个是通过时间。
求最短的路和最快的路,但是最短的路可能有多条,输出耗费时间最短的那一条。而耗时最短的路也可能有多条,输出路段最少的那一条。
题目思路简单,就是两个类型用 Dijkstra 求最短路径的合并,代码量的大,最后调试耗费了很多时间,数据太多,容易弄错。
思路:
我的想法很简单,时间和距离分别储存,分别用Dijkstra,分别 DFS。
代码实现
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int INF=1000000000;
const int maxn=505;
int Gt[maxn][maxn],Gl[maxn][maxn];
int d[maxn],t[maxn];
bool vis[maxn];
int n,m;
vector<int> fast,shortest,tmp;
vector<int> pret[maxn],pred[maxn];
void DijDis(int s){ //求距离最近
fill(d,d+maxn,INF);
fill(vis,vis+n,false);
d[s]=0;
for(int i=0;i<n;++i){
int u=-1,min=INF;
for(int j=0;j<n;++j){
if(vis[j]==false&&d[j]<min){
u=j;
min=d[j];
}
}
if(u==-1) return; //出口
vis[u]=true;
for(int v=0;v<n;++v){
if(vis[v]==false&&Gl[u][v]!=INF){
if(d[u]+Gl[u][v]<d[v]){
pred[v].clear();
pred[v].push_back(u);
d[v]=d[u]+Gl[u][v];
}else if(d[u]+Gl[u][v]==d[v])
pred[v].push_back(u);
}
}
}
}
int minTime=INF;
void DFSdis(int s,int v){ //最短距离相同的路径中找到耗时最短
if(v==s){
//printf("dfjakdsjfad");
tmp.push_back(s);
int st,ed,time=0;
st=tmp[tmp.size()-1];
for(int i=tmp.size()-2;i>=0;--i){ //注意是倒过来的顺序
ed=tmp[i];
//printf("Gt=%d\n",Gt[st][ed]);
time+=Gt[st][ed];
st=ed;
}
if(time<minTime){
shortest=tmp;
//printf("time=%d\n",time);
minTime=time;
}
tmp.pop_back();
return;
}
tmp.push_back(v);
for(int i=0;i<pred[v].size();++i){
DFSdis(s,pred[v][i]);
}
tmp.pop_back();
}
void DijFast(int s){
fill(t,t+maxn,INF);
fill(vis,vis+n,false);
t[s]=0;
for(int i=0;i<n;++i){
int u=-1,min=INF;
for(int j=0;j<n;++j){
if(vis[j]==false&&t[j]<min){
u=j;
min=t[j];
}
}
if(u==-1) return;
vis[u]=true;
for(int v=0;v<n;++v){
if(vis[v]==false&&Gt[u][v]!=INF){
if(t[u]+Gt[u][v]<t[v]){
t[v]=t[u]+Gt[u][v];
pret[v].clear();
pret[v].push_back(u);
}else if(t[u]+Gt[u][v]==t[v]){
pret[v].push_back(u);
}
}
}
}
}
int minSize=INF;
void DFStime(int s,int v){
if(v==s){
tmp.push_back(s);
int cnt=tmp.size();
if(cnt<minSize){
fast=tmp;
minSize=cnt;
}
tmp.pop_back();
return;
}
tmp.push_back(v);
for(int i=0;i<pret[v].size();++i){
DFStime(s,pret[v][i]);
}
tmp.pop_back();
}
int main(){
int u,v,tag,time,l;
fill(Gt[0],Gt[0]+maxn*maxn,INF);
fill(Gl[0],Gl[0]+maxn*maxn,INF);
scanf("%d%d",&n,&m);
for(int i=0;i<m;++i){
scanf("%d%d%d%d%d",&u,&v,&tag,&l,&time);
if(tag==1){
Gt[u][v]=time;
Gl[u][v]=l;
}else{
Gt[u][v]=Gt[v][u]=time;
Gl[u][v]=Gl[v][u]=l;
}
}
scanf("%d%d",&u,&v);
DijFast(u);
DFStime(u,v); //这里我也有点晕,不知道啥是反着的
tmp.clear();
DijDis(u);
DFSdis(u,v);
if(fast==shortest){
printf("Distance = %d; Time = %d:",d[v],t[v]);
for(int i=fast.size()-1;i>=0;--i){
printf(" %d",fast[i]);
if(i>0) printf(" ->");
else printf("\n");
}
}else{
printf("Distance = %d:",d[v]);
for(int i=shortest.size()-1;i>=0;--i){
printf(" %d",shortest[i]);
if(i>0) printf(" ->");
else printf("\n");
}
printf("Time = %d:",t[v]);
for(int i=fast.size()-1;i>=0;--i){
printf(" %d",fast[i]);
if(i>0) printf(" ->");
else printf("\n");
}
}
}