【PAT】【Advanced Level】1111. Online Map (30)
2017-09-10 19:42
369 查看
1111. Online Map (30)
时间限制300 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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
原题链接:
https://www.patest.cn/contests/pat-a-practise/1111
思路:
DFS 超时
DJ+DFS AC
对于针对时间的最短路,用多维数组存储多个可能的前驱,然后用DFS搜索最少节点的路径
CODE:
DFS:
#include<iostream>
#include<vector>
#include<cstring>
#define N 510
using namespace std;
typedef struct S
{
int le;
int ti;
};
vector<int> pa
;
S dis
;
bool flag
;
int ori;
int des;
int minti;
int mintile;
int minle;
int minleti;
vector<int> re1;
vector<int> re2;
vector<int> re;
void dfs(int n,int lc,int tc)
{
if (lc>minle && tc>minti) return ;
if (n==des)
{
if (lc<minle || (lc==minle && tc<minleti))
{
minle=lc;
minleti=tc;
re1.clear();
for (int i=0;i<re.size();i++)
{
re1.push_back(re[i]);
}
}
if (tc<minti || (tc==minti && re.size()<re2.size()))
{
minti=tc;
mintile=lc;
re2.clear();
for (int i=0;i<re.size();i++)
{
re2.push_back(re[i]);
}
}
}
for (int i=0;i<pa
.size();i++)
{
if (flag[pa
[i]]==1) continue;
flag[pa
[i]]=1;
re.push_back(pa
[i]);
dfs(pa
[i],lc+dis
[pa
[i]].le,tc+dis
[pa
[i]].ti);
flag[pa
[i]]=0;
re.erase(re.end()-1);
}
return ;
}
int judge(int n)
{
for (int i=0;i<n;i++)
if (re1[i]!=re2[i]) return -1;
return 1;
}
int main()
{
int n,m;
cin>>n>>m;
for (int i=0;i<m;i++)
{
int st,ds,ow;
S te;
cin>>st>>ds>>ow>>te.le>>te.ti;
pa[st].push_back(ds);
dis[st][ds]=te;
if (ow==0)
{
pa[ds].push_back(st);
dis[ds][st]=te;
}
}
cin>>ori>>des;
memset(flag,0,sizeof(flag));
minti=10000000;
mintile=10000000;
minle=10000000;
minleti=10000000;
flag[ori]=1;
re.push_back(ori);
dfs(ori,0,0);
int pd;
if (re1.size()==re2.size())
{
pd=judge(re1.size());
}
if (pd==1)
{
cout<<"Distance = "<<minle<<"; Time = "<<minti<<": ";
for (int i=0;i<re1.size();i++)
{
if (i!=0) cout<<" -> ";
cout<<re1[i];
}
}
else
{
cout<<"Distance = "<<minle<<": ";
for (int i=0;i<re1.size();i++)
{
if (i!=0) cout<<" -> ";
cout<<re1[i];
}
cout<<endl;
cout<<"Time = "<<minti<<": ";
for (int i=0;i<re2.size();i++)
{
if (i!=0) cout<<" -> ";
cout<<re2[i];
}
}
return 0;
}
DJ+DFS:
#include<iostream>
#include<cstdio>
#include<vector>
#define N 510
using namespace std;
int from,to;
int m,n;
int minsize;
int le
, ti
;
int distl
, distt
;
int pathl
;
vector<int> patht
, re1, re2, _re2;
using namespace std;
void dfsl(int n)
{
re1.push_back(n);
if (n==from) return;
dfsl(pathl
);
return;
}
void dfst(int n)
{
_re2.push_back(n);
if (n==from)
{
if (_re2.size()<minsize)
{
re2.clear();
minsize=_re2.size();
re2=_re2;
}
}
for (int i=0;i<patht
.size();i++)
{
dfst(patht
[i]);
}
_re2.pop_back();
}
void dj1()
{
bool flag
;
int _ti
;
fill(_ti,_ti+N,10000000);
_ti[from]=0;
distl[from]=0;
fill(flag,flag+N,false);
for (int i=0;i<n;i++) pathl[i]=i;
for (int i=0;i<n;i++)
{
int mi=10000000;
int fi=-1;
for (int j=0;j<n;j++)
{
if (flag[j]==1) continue;
if (distl[j]<mi)
{
mi=distl[j];
fi=j;
}
}
if (fi==-1) break;
flag[fi]=1;
for (int j=0;j<n;j++)
{
if (flag[j]==1 || le[fi][j]==-1) continue;
if (distl[j]>distl[fi]+le[fi][j])
{
pathl[j]=fi;
distl[j]=distl[fi]+le[fi][j];
_ti[j]=_ti[fi]+ti[fi][j];
}
else if (distl[j]==distl[fi]+le[fi][j])
{
if (_ti[j]>_ti[fi]+ti[fi][j])
{
pathl[j]=fi;
distl[j]=distl[fi]+le[fi][j];
_ti[j]=_ti[fi]+ti[fi][j];
}
}
}
}
}
void dj2()
{
bool flag
;
distt[from]=0;
fill(flag,flag+N,false);
for (int i=0;i<n;i++)
{
int mi=10000000;
int fi=-1;
for (int j=0;j<n;j++)
{
if (flag[j]==1) continue;
if (distt[j]<mi)
{
mi=distt[j];
fi=j;
}
}
if (fi==-1)
break;
flag[fi]=1;
for (int j=0;j<n;j++)
{
if (flag[j]==1 || ti[fi][j]==-1) continue;
if (distt[j]>distt[fi]+ti[fi][j])
{
patht[j].clear();
patht[j].push_back(fi);
distt[j]=distt[fi]+ti[fi][j];
}
else if (distt[j]==distt[fi]+ti[fi][j])
{
patht[j].push_back(fi);
}
}
}
}
int main()
{
fill(le[0],le[0]+N*N,-1);
fill(ti[0],ti[0]+N*N,-1);
fill(distl,distl+N,10000000);
fill(distt,distt+N,10000000);
cin>>n>>m;
for (int i=0;i<m;i++)
{
int fr,to,ow,l,t;
cin>>fr>>to>>ow>>l>>t;
le[fr][to]=l;
ti[fr][to]=t;
if (ow!=1)
{
le[to][fr]=l;
ti[to][fr]=t;
}
}
cin>>from>>to;
minsize=10000000;
dj1();
dj2();
dfsl(to);
dfst(to);
if (re1==re2)
{
cout<<"Distance = "<<distl[to]<<"; Time = "<<distt[to]<<": ";
for (int i=re1.size()-1;i>=0;i--)
{
cout<<re1[i];
if (i!=0) cout<<" -> ";
}
}
else
{
cout<<"Distance = "<<distl[to]<<": ";
for (int i=re1.size()-1;i>=0;i--)
{
cout<<re1[i];
if (i!=0) cout<<" -> ";
}
cout<<endl;
cout<<"Time = "<<distt[to]<<": ";
for (int i=re2.size()-1;i>=0;i--)
{
cout<<re2[i];
if (i!=0) cout<<" -> ";
}
}
return 0;
}
相关文章推荐
- PAT (Advanced Level) 1038. Recover the Smallest Number (30) 解题报告
- 【PAT】【Advanced Level】1053. Path of Equal Weight (30)
- 【PAT】【Advanced Level】1064. Complete Binary Search Tree (30)
- 【PAT】【Advanced Level】1091. Acute Stroke (30)
- PAT (Advanced Level) Practise 1072 Gas Station (30)
- 1004. Counting Leaves (30)——PAT (Advanced Level) Practise
- PAT (Advanced Level)1030. Travel Plan (30) dfs
- PAT (Advanced Level) 1026. Table Tennis (30)
- PAT (Advanced Level) 1045. Favorite Color Stripe (30)
- PAT (Advanced Level) 1057. Stack (30) 模拟栈,O(1)输出栈的中位数,用set辅助
- PAT (Advanced Level) 1072. Gas Station (30) Dijkstra最短路径+剪枝
- PAT (Advanced Level) 1095. Cars on Campus (30) 结构体排序,合并区间
- PAT (Advanced Level) 1004. Counting Leaves (30) 解题报告
- 【PAT Advanced Level】1030. Travel Plan (30)
- PAT (Advanced Level) 1127. ZigZagging on a Tree (30) 解题报告
- PAT (Advanced Level) 1127. ZigZagging on a Tree (30)
- 1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise
- 【PAT】【Advanced Level】1076. Forwards on Weibo (30)
- PAT (Advanced Level) Practise 1026 Table Tennis (30)
- PAT (Advanced Level) Practise 1045 Favorite Color Stripe (30)