其實就是必較複雜的bfs
底下(#include<bits/stdc++.h>)記得加上去
吐槽一下學語法前弄這些東西會不會太吃天賦了?
using namespace std;
struct Edge {
int to, dist, speed;
};
pair<int,int> solve(int n, int m, int s, int e, vector<Edge> g[]) {
const int INF = 1e9;
// ----------- Dijkstra 1 (最短距離) -----------
vector<int> dist(n+1, INF);
dist[s] = 0;
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<>> pq;
pq.push({0,s});
while(!pq.empty()){
auto [cd,u] = pq.top(); pq.pop();
if(cd > dist[u]) continue;
for(auto &ed: g[u]){
if(dist[ed.to] > cd + ed.dist){
dist[ed.to] = cd + ed.dist;
pq.push({dist[ed.to], ed.to});
}
}
}
int X = dist[e];
// ----------- Dijkstra 2 (最短時間) -----------
vector<double> time(n+1, 1e18);
vector<int> dist_used(n+1, INF); // 用來記錄那條路徑的距離
time[s] = 0;
dist_used[s] = 0;
priority_queue<pair<double,int>, vector<pair<double,int>>, greater<>> pq2;
pq2.push({0,s});
while(!pq2.empty()){
auto [ct,u] = pq2.top(); pq2.pop();
if(ct > time[u]) continue;
for(auto &ed: g[u]){
double nt = ct + (double)ed.dist/ed.speed;
if(nt < time[ed.to]){
time[ed.to] = nt;
dist_used[ed.to] = dist_used[u] + ed.dist;
pq2.push({nt, ed.to});
}
}
}
int Y = dist_used[e];
return {X,Y};
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while(T--){
int n,m,s,e;
cin >> n >> m >> s >> e;
vector<Edge> g[n+1];
for(int i=0;i<m;i++){
int a,b,d,v;
cin >> a >> b >> d >> v;
g[a].push_back({b,d,v});
g[b].push_back({a,d,v});
}
auto [X,Y] = solve(n,m,s,e,g);
cout << X << " " << Y << "\n";
}
return 0;
}