两次Dijkstra + DFS
//
// main.cpp
// PATA1111
//
// Created by Phoenix on 2018/2/23.
// Copyright © 2018年 Phoenix. All rights reserved.
//
#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
const int maxn = 510;
const int inf = 1000000000;
int n, m, st, ed;
int G[maxn][maxn], T[maxn][maxn];
int d[maxn], t[maxn];
bool vis[maxn] = {false}, vis1[maxn] = {false};
vector<int> pre[maxn], pre1[maxn];
void dijkstra1(int s){
fill(d, d + maxn, inf);
d[s] = 0;
for(int i = 0; i < n; i++) {
int u = -1, MIN = inf;
for(int j = 0; j < n; j++) {
if(vis[j] == false && d[j] < MIN) {
u = j;
MIN = d[j];
}
}
if(u == -1) return;
vis[u] = true;
for(int v = 0; v < n; v++) {
if(vis[v] == false && G[u][v] != inf) {
if(d[u] + G[u][v] < d[v]) {
pre[v].clear();
d[v] = d[u] + G[u][v];
pre[v].push_back(u);
} else if(d[u] + G[u][v] == d[v]) {
pre[v].push_back(u);
}
}
}
}
}
vector<int> temp, path;
int optvalue = inf;
void DFS(int v) {
if(v == st) {
temp.push_back(v);
int value = 0;
for(int i = temp.size() - 1; i > 0; i--) {
value += T[temp[i]][temp[i - 1]];
}
if(value < optvalue) {
optvalue = value;
path = temp;
}
temp.pop_back();
}
temp.push_back(v);
for(int i = 0; i < pre[v].size(); i++) {
DFS(pre[v][i]);
}
temp.pop_back();
}
void dijkstra2(int s){
fill(t, t + maxn, inf);
t[s] = 0;
for(int i = 0; i < n; i++) {
int u = -1, MIN = inf;
for(int j = 0; j < n; j++) {
if(vis1[j] == false && t[j] < MIN) {
u = j;
MIN = t[j];
}
}
if(u == -1) return;
vis1[u] = true;
for(int v = 0; v < n; v++) {
if(vis1[v] == false && G[u][v] != inf) {
if(t[u] + T[u][v] < t[v]) {
pre1[v].clear();
t[v] = t[u] + T[u][v];
pre1[v].push_back(u);
} else if(t[u] + T[u][v] == t[v]) {
pre1[v].push_back(u);
}
}
}
}
}
vector<int> temp1, path1;
int optvalue1 = inf;
void DFS1(int v) {
if(v == st) {
temp1.push_back(v);
int value = temp1.size();
if(value < optvalue1) {
optvalue1 = value;
path1 = temp1;
}
temp1.pop_back();
}
temp1.push_back(v);
for(int i = 0; i < pre1[v].size(); i++) {
DFS1(pre1[v][i]);
}
temp1.pop_back();
}
int main(int argc, const char * argv[]) {
fill(G[0], G[0] + maxn * maxn, inf);
scanf("%d %d", &n, &m);
for(int i = 0; i < m; i++) {
int v1, v2, flag, length, time;
scanf("%d %d %d %d %d", &v1, &v2, &flag, &length, &time);
G[v1][v2] = length;
T[v1][v2] = time;
if(flag == 0) {
G[v2][v1] = length;
T[v2][v1] = time;
}
}
scanf("%d %d", &st, &ed);
dijkstra1(st);
DFS(ed);
dijkstra2(st);
DFS1(ed);
bool flag = true;
if(path.size() != path1.size()) flag = false;
else {
for(int i = 0; i < path.size(); i++) {
if(path[i] != path1[i]) flag = false;
}
}
if(flag == true) {
printf("Distance = %d; Time = %d: ", d[ed], t[ed]);
for(int i = path.size() - 1; i >= 0; i--) {
printf("%d", path[i]);
if(i > 0) printf(" -> ");
else printf("\n");
}
} else {
printf("Distance = %d: ", d[ed]);
for(int i = path.size() - 1; i >= 0; i--) {
printf("%d", path[i]);
if(i > 0) printf(" -> ");
else printf("\n");
}
printf("Time = %d: ", t[ed]);
for(int i = path1.size() - 1; i >= 0; i--) {
printf("%d", path1[i]);
if(i > 0) printf(" -> ");
else printf("\n");
}
}
return 0;
}