您的位置:首页 > 其它

1111. Online Map (30)

2016-08-13 14:37 316 查看
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 <iostream>
#include <vector>
using namespace std;
const int maxn = 505,INF= 999999999;
int n,m;
struct edge{
int v,d,t;
edge(int vv = 0,int dd = 0,int tt = 0):v(vv),d(dd),t(tt){}
};
vector<edge> G[maxn];
int vis[maxn],dist[maxn],tim[maxn],cnt[maxn],path[maxn];
void get_path(int x,vector<int> & p){
if(x != -1){
get_path(path[x],p);
p.push_back(x);
}
}
void print_path(const vector<int> & vec){
for(int i=0;i<vec.size();i++){
if(i != 0)cout<<" -> ";
cout<<vec[i];
}cout<<endl;
}
void dijkstra1(int x){
for(int i=0;i<n;i++){path[i] = -1;dist[i] = tim[i] = INF;vis[i] = 0;}
dist[x] = tim[x] = 0;
for(int j=0;j<n;j++){
int u = -1,mind = INF;
for(int i=0;i<n;i++)if(vis[i] == 0 && mind > dist[i])mind = dist[u = i];
vis[u] = 1;
for(int i=0;i<G[u].size();i++){
int v = G[u][i].v,d = G[u][i].d,t = G[u][i].t;
if(vis[v] == 0){
if(dist[v] > dist[u] + d){
dist[v] = dist[u] + d;
tim[v] = tim[u] + t;
path[v] = u;
}else if(dist[v] == dist[u] + d && tim[v] > tim[u] + t){
tim[v] = tim[u] + t;
path[v] = u;
}
}
}
}
}
void dijkstra2(int x){
for(int i=0;i<n;i++){path[i] = -1;cnt[i] = INF;tim[i] = INF;vis[i] = 0;}
cnt[x] = tim[x] = 0;
for(int j=0;j<n;j++){
int u = -1,mint = INF;
for(int i=0;i<n;i++)if(vis[i] == 0 && mint > tim[i])mint = tim[u = i];
vis[u] = 1;
for(int i=0;i<G[u].size();i++){
int v = G[u][i].v,t = G[u][i].t;
if(vis[v] == 0){
if(tim[v] > tim[u] + t){
tim[v] = tim[u] + t;
cnt[v] = cnt[u] + 1;
path[v] = u;
}else if(tim[v] == tim[u] + t && cnt[v] > cnt[u] + 1){
cnt[v] = cnt[u] + 1;
path[v] = u;
}
}
}
}
}
int main(){
cin>>n>>m;

a343
for(int i=0;i<m;i++){
int a,b,c,d,t;
cin>>a>>b>>c>>d>>t;
G[a].push_back(edge(b,d,t));
if(c == 0)G[b].push_back(edge(a,d,t));
}
int t,d,s,e;
cin>>s>>e;
vector<int> path1,path2;
dijkstra1(s);
d = dist[e];
get_path(e,path1);
dijkstra2(s);
t = tim[e];
get_path(e,path2);
if(path2 == path1){printf("Distance = %d; Time = %d: ",d,t);print_path(path1);}
else{
printf("Distance = %d: ",d);print_path(path1);
printf("Time = %d: ",t);print_path(path2);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: