PTA 天体地图
题意:n个点,m条边,给出两点的距离和通行时间,要求求出起点和终点的最短时间路径和最短距离路径,如果多条最短路径时间一样,求出在此条件下的路径最短,数据保证唯一,如果多条最短路径距离一样,求出在此条件下的经过点最少的路径,数据保证唯一。
思路:两次
d
i
j
k
s
t
r
a
dijkstra
dijkstra或者
s
p
f
a
spfa
spfa,不同一般的最短路,开两个额外的数组记录距离和点数,在时间或距离相等时再次比较一下即可。
#include<bits/stdc++.h>
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define x first
#define y second
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
typedef pair<int,int> PII;
const int mod=1000000007 , N = 510 , M = 3e5 + 10;
const double eps=1e-8;
int n,m;
int e[M],ne[M],d[M],t[M],h[N],idx;
void add(int a,int b,int c,int x)
{
e[idx] = b , d[idx] = c , t[idx] = x, ne[idx] = h[a] , h[a] = idx++;
}
int dis1[N],dis2[N],path[N],ans1[N],ans2[N],pre1[N],pre2[N],cnt[N];
bool st[N];
int S,T;
void dijkstra1()
{
memset(dis1,0x3f,sizeof dis1);
memset(st,0,sizeof st);
dis1[S] = 0;
priority_queue<PII,vector<PII>,greater<PII> > qu;
qu.push({0,S});
while(qu.size())
{
auto p = qu.top();
qu.pop();
int pos = p.y , dist = p.x;
if(st[pos]) continue;
st[pos] = 1;
for(int i = h[pos] ; ~i ; i = ne[i])
{
int j = e[i];
if(dis1[j] > dis1[pos] + t[i])
{
path[j] = path[pos] + d[i];
dis1[j] = dis1[pos] + t[i];
pre1[j] = pos;
qu.push({dis1[j] , j});
}
else if(dis1[j] == dis1[pos] + t[i])//时间相同
{
if(path[j] > path[pos] + d[i])
{
path[j] = path[pos] + d[i];
pre1[j] = pos;
}
}
}
}
}
void dijkstra2()
{
memset(dis2,0x3f,sizeof dis2);
memset(st,0,sizeof st);
dis2[S] = 0;
priority_queue<PII,vector<PII>,greater<PII> > qu;
qu.push({0,S});
while(qu.size())
{
auto p = qu.top();
qu.pop();
int pos = p.y , dist = p.x;
if(st[pos]) continue;
st[pos] = 1;
for(int i = h[pos] ; ~i ; i = ne[i])
{
int j = e[i];
if(dis2[j] > dis2[pos] + d[i])
{
cnt[j] = cnt[pos] + 1;
dis2[j] = dis2[pos] + d[i];
pre2[j] = pos;
qu.push({dis2[j] , j});
}
else if(dis2[j] == dis2[pos] + d[i])//距离相同
{
if(cnt[j] > cnt[pos] + 1)
{
cnt[j] = cnt[pos] + 1;
pre2[j] = pos;
}
}
}
}
}
int main(){
cin>>n>>m;
memset(h,-1,sizeof h);
while(m--)
{
int a,b,c1,c2;
int op;
cin>>a>>b>>op>>c1>>c2;
if(op == 1) add(a,b,c1,c2);
else if(op == 0)
{
add(a,b,c1,c2);
add(b,a,c1,c2);
}
}
cin>>S>>T;
dijkstra1();
dijkstra2();
int cnt1 = 0 , cnt2 = 0;
for(int i = T ; i != S ; i = pre1[i])
ans1[cnt1++] = i;
for(int i = T ; i != S ; i = pre2[i])
ans2[cnt2++] = i;
bool flag = true;
if(cnt1 == cnt2)
{
for(int i = cnt1 - 1 ; i >= 0 ; i -- )
{
if(ans1[i] != ans2[i])
{
flag = false;
break;
}
}
}
if(flag && cnt1 == cnt2)
{
cout<<"Time = "<<dis1[T]<<"; ";
cout<<"Distance = "<<dis2[T]<<": ";
cout<<S;
for(int i = cnt1 - 1 ; i >= 0 ; i -- ) cout<<" => "<<ans1[i];
}
else
{
cout<<"Time = "<<dis1[T]<<": ";
cout<<S;
for(int i = cnt1 - 1 ; i >= 0 ; i -- ) cout<<" => "<<ans1[i];
cout<<"\n";
cout<<"Distance = "<<dis2[T]<<": ";
cout<<S;
for(int i = cnt2 - 1 ; i >= 0 ; i -- ) cout<<" => "<<ans2[i];
cout<<"\n";
}
return 0;
}