1111. Online Map (30) <Dijkstra>
2017-08-02 23:51
344 查看
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:
Sample Output 1:
Sample Input 2:
Sample Output 2:
两次Dijkstra求最短路径和最快路径
如果最短路径不唯一,输出最短路径下的最快路径
如果最快路径不唯一,输出最快路径下节点数最少的
很常规的题目。额外判断最短和最快路径相同的情况,纪录path (前驱)
1087. All Roads Lead to Rome (30) <Dijkstra优先队列>
两个题目很类似
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
typedef pair<int,int> P;
const int INF=1<<29;
int n,m;
int le[555][555],tim[555][555];
int l[555],t[555];
int path_l[555]={0},path_t[555]={0};
int vi[555];
int cro[555];
vector<int> v[1000];
int x,y;
void Dijkstra_len(int num){
priority_queue<P,vector<P>,greater<P> > que;
for(int i=0;i<555;i++) l[i]=INF,vi[i]=0;
l[num]=0;
que.push({l[num],num});
while(que.size()){
P p=que.top();
que.pop();
int fz=p.second;
if(vi[fz]) continue;
vi[fz]=1;
for(int i=0;i<v[fz].size();i++){
int nu=v[fz][i];
if(l[nu]>l[fz]+le[fz][nu]){
l[nu]=l[fz]+le[fz][nu];
t[nu]=t[fz]+tim[fz][nu];
path_l[nu]=fz;
que.push({l[nu],nu});
}
else if(l[nu]==l[fz]+le[fz][nu]) {
if(t[nu]>t[fz]+tim[fz][nu]){
path_l[nu]=fz;
t[num]=t[fz]+tim[fz][nu];
}
}
}
}
}
void Dijkstra_time(int num){
priority_queue<P,vector<P>,greater<P> > que;
for(int i=0;i<555;i++) t[i]=INF,vi[i]=0,cro[i]=0;
t[num]=0;
cro[num]=1;
que.push({t[num],num});
while(que.size()){
P p=que.top();
que.pop();
int fz=p.second;
if(vi[fz]) continue;
vi[fz]=1;
for(int i=0;i<v[fz].size();i++){
int nu=v[fz][i];
if(t[nu]>t[fz]+tim[fz][nu]){
t[nu]=t[fz]+tim[fz][nu];
path_t[nu]=fz;
cro[nu]=cro[fz]+1;
que.push({t[nu],nu});
}
else if(t[nu]==t[fz]+tim[fz][nu]) {
if(cro[nu]>cro[fz]+1){
path_t[nu]=fz;
cro[nu]=cro[fz]+1;
}
}
}
}
}
int main(){
cin>>n>>m;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)
le[i][j]=INF,tim[i][j]=INF;
}
for(int i=0;i<m;i++){
int a,b,c,d,e;
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
v[a].push_back(b);
le[a][b]=d;
tim[a][b]=e;
if(c==0){
le[b][a]=d;
tim[b][a]=e;
v[b].push_back(a);
}
}
cin>>x>>y;
Dijkstra_len(x);
Dijkstra_time(x);
int Dis[1000],T[1000];
int cnt_d=0,cnt_t=0;
int fz=y;
stack<int> s;
while(fz!=x){
s.push(fz);
fz=path_l[fz];
}
while(s.size()){
Dis[cnt_d++]=s.top();
s.pop();
}
fz=y;
while(fz!=x){
s.push(fz);
fz=path_t[fz];
}
while(s.size()){
T[cnt_t++]=s.top();
s.pop();
}
if(cnt_t==cnt_d){
int i;
for( i=0;i<cnt_t;i++){
if(Dis[i]!=T[i]) break;
}
if(i==cnt_t){
printf("Distance = %d; Time = %d: %d",l[y],t[y],x);
for(int i=0;i<cnt_d;i++) printf(" -> %d",Dis[i]);
return 0;
}
}
printf("Distance = %d: %d",l[y],x);
for(int i=0;i<cnt_d;i++) printf(" -> %d",Dis[i]);
cout<<endl;
printf("Time = %d: %d",t[y],x);
for(int i=0;i<cnt_t;i++)
printf(" -> %d",T[i]);
return 0;
}
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求最短路径和最快路径
如果最短路径不唯一,输出最短路径下的最快路径
如果最快路径不唯一,输出最快路径下节点数最少的
很常规的题目。额外判断最短和最快路径相同的情况,纪录path (前驱)
1087. All Roads Lead to Rome (30) <Dijkstra优先队列>
两个题目很类似
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
typedef pair<int,int> P;
const int INF=1<<29;
int n,m;
int le[555][555],tim[555][555];
int l[555],t[555];
int path_l[555]={0},path_t[555]={0};
int vi[555];
int cro[555];
vector<int> v[1000];
int x,y;
void Dijkstra_len(int num){
priority_queue<P,vector<P>,greater<P> > que;
for(int i=0;i<555;i++) l[i]=INF,vi[i]=0;
l[num]=0;
que.push({l[num],num});
while(que.size()){
P p=que.top();
que.pop();
int fz=p.second;
if(vi[fz]) continue;
vi[fz]=1;
for(int i=0;i<v[fz].size();i++){
int nu=v[fz][i];
if(l[nu]>l[fz]+le[fz][nu]){
l[nu]=l[fz]+le[fz][nu];
t[nu]=t[fz]+tim[fz][nu];
path_l[nu]=fz;
que.push({l[nu],nu});
}
else if(l[nu]==l[fz]+le[fz][nu]) {
if(t[nu]>t[fz]+tim[fz][nu]){
path_l[nu]=fz;
t[num]=t[fz]+tim[fz][nu];
}
}
}
}
}
void Dijkstra_time(int num){
priority_queue<P,vector<P>,greater<P> > que;
for(int i=0;i<555;i++) t[i]=INF,vi[i]=0,cro[i]=0;
t[num]=0;
cro[num]=1;
que.push({t[num],num});
while(que.size()){
P p=que.top();
que.pop();
int fz=p.second;
if(vi[fz]) continue;
vi[fz]=1;
for(int i=0;i<v[fz].size();i++){
int nu=v[fz][i];
if(t[nu]>t[fz]+tim[fz][nu]){
t[nu]=t[fz]+tim[fz][nu];
path_t[nu]=fz;
cro[nu]=cro[fz]+1;
que.push({t[nu],nu});
}
else if(t[nu]==t[fz]+tim[fz][nu]) {
if(cro[nu]>cro[fz]+1){
path_t[nu]=fz;
cro[nu]=cro[fz]+1;
}
}
}
}
}
int main(){
cin>>n>>m;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)
le[i][j]=INF,tim[i][j]=INF;
}
for(int i=0;i<m;i++){
int a,b,c,d,e;
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
v[a].push_back(b);
le[a][b]=d;
tim[a][b]=e;
if(c==0){
le[b][a]=d;
tim[b][a]=e;
v[b].push_back(a);
}
}
cin>>x>>y;
Dijkstra_len(x);
Dijkstra_time(x);
int Dis[1000],T[1000];
int cnt_d=0,cnt_t=0;
int fz=y;
stack<int> s;
while(fz!=x){
s.push(fz);
fz=path_l[fz];
}
while(s.size()){
Dis[cnt_d++]=s.top();
s.pop();
}
fz=y;
while(fz!=x){
s.push(fz);
fz=path_t[fz];
}
while(s.size()){
T[cnt_t++]=s.top();
s.pop();
}
if(cnt_t==cnt_d){
int i;
for( i=0;i<cnt_t;i++){
if(Dis[i]!=T[i]) break;
}
if(i==cnt_t){
printf("Distance = %d; Time = %d: %d",l[y],t[y],x);
for(int i=0;i<cnt_d;i++) printf(" -> %d",Dis[i]);
return 0;
}
}
printf("Distance = %d: %d",l[y],x);
for(int i=0;i<cnt_d;i++) printf(" -> %d",Dis[i]);
cout<<endl;
printf("Time = %d: %d",t[y],x);
for(int i=0;i<cnt_t;i++)
printf(" -> %d",T[i]);
return 0;
}
相关文章推荐
- PAT (Advanced Level) Practise 1111 Online Map (30)
- 1087. All Roads Lead to Rome (30) <Dijkstra优先队列>
- PAT(A) - 1111. Online Map (30)
- PAT(A)1111 Online Map (30)(最短路)
- PAT 1111 —— Online Map(Dijkstra单源最短路径)
- PAT (Advanced Level) Practise 1111 Online Map (30)
- Android List<Map<String,String>转json(例子)
- Gson工具类 Json 转 List<T> Json转 Map Json 转 JsonObject
- <Python进阶读书笔记>之(三) Map, Filter和reduce
- 我刚刚写了一个map的题目,还用到了#include<string>的头文件,下面我来总结一下 map<类型>变量 q q.find(找字符串) q.end()是否到达末尾
- java操作<Map转换List——>list集合重新排序—>List集合转换成JSON>
- HTML <map> 设置图热点
- Java集合三种类型:Set<集>、List<列表>、Map<映射>
- Missing map from Nullable`1 to String. Create using Mapper.CreateMap<Nullable`1, String>. 解决办法
- 程序员_Java基础之<八>-Map集合、集合工具类
- List<Map> 排序
- js 动态根据后台接受的Map<String,String>参数 动态增加参数input框
- Intent传递List<Map<String,String>>的使用
- <转> com.google.gson.internal.StringMap cannot be cast XXX(整理)
- Map<String,String> m=new HashMap<String,String>()和HashMap<String,String> hashMapOut=new HashMap<Stri