#include <bits/stdc++.h>
#define INFINITY 65535
using namespace std;
const int maxnum = 500;
int dist[maxnum], cost[maxnum], Edge[maxnum][maxnum], Cost[maxnum][maxnum], collected[maxnum];
int N, M, S, D;
int FindMin();
void Dijkstra();
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("in.txt", "r", stdin);
#endif
cin >> N >> M >> S >> D;
int a, b;
for(int i = 0; i < M; i++){
cin >> a >> b;
cin >> Edge[a][b] >> Cost[a][b];
Edge[b][a] = Edge[a][b];
Cost[b][a] = Cost[a][b];
}
for(int i = 0; i < N; i++){
if(Edge[S][i] != 0){
dist[i] = Edge[S][i];
cost[i] = Cost[S][i];
}else{
dist[i] = cost[i] = INFINITY;
}
}
Dijkstra();
return 0;
}
int FindMin()
{
int minimum = 500;
int index = -1;
for(int i = 0; i < N; i++){
if(collected[i] == 0 && dist[i] < minimum){
minimum = dist[i];
index = i;
}
}
return index;
}
void Dijkstra()
{
int v;
dist[S] = 0;
collected[S] = 1;
while(1){
v = FindMin();
if(v == -1) break;
collected[v] = 1;
for(int i = 0; i < N; i++){
if(collected[i] == 0 && Edge[v][i]){
if(dist[v] + Edge[v][i] < dist[i]){
dist[i] = dist[v] + Edge[v][i];
cost[i] = cost[v] + Cost[v][i];
}else if(dist[v] + Edge[v][i] == dist[i] && cost[i] > cost[v] + Cost[v][i]){
cost[i] = cost[v] + Cost[v][i];
}
}
}
}
cout << dist[D] << " " << cost[D];
}
中国大学MOOC-陈越、何钦铭-数据结构-2018秋 07-图6 旅游规划 (25 分)
最新推荐文章于 2022-04-18 08:30:36 发布