标签:back turn oid temp ack 代码 技术 next 遍历
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 510;
const int INF = 100000000;
bool vis[MAXN] = {false};
int d[MAXN], c[MAXN];
int G[MAXN][MAXN], cost[MAXN][MAXN];
vector<int> pre[MAXN];
vector<int> path, tempPath;
int n, m, c1, c2;//求的两城市id号
int optValue = INF;
void Djikstra(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]){
d[v] = d[u] + G[u][v];
pre[v].clear();
pre[v].push_back(u);
}else if(d[u] + G[u][v] == d[v]){
pre[v].push_back(u);
}
}
}
}
}
void DFS(int v){
if(v == c1){
tempPath.push_back(v);
int value = 0;
for(int i = tempPath.size()-1; i > 0; i--){
int id = tempPath[i], idNext = tempPath[i-1];
value += cost[id][idNext];
}
if(value < optValue){
path = tempPath;
optValue = value;
}
tempPath.pop_back();
return;
}
tempPath.push_back(v);
for(int i = 0; i < pre[v].size(); i++){
DFS(pre[v][i]);
}
tempPath.pop_back();
}
int main(){
scanf("%d%d%d%d", &n, &m, &c1, &c2);
int id1, id2;
int distance, price;
fill(G[0], G[0]+MAXN*MAXN, INF);
fill(cost[0], cost[0]+MAXN*MAXN, INF);
for(int i = 0; i < m; i++){
scanf("%d%d%d%d", &id1, &id2, &distance, &price);
G[id1][id2] = distance;
G[id2][id1] = distance;
cost[id1][id2] = price;
cost[id2][id1] = price;
}
Djikstra(c1);
DFS(c2);
for(int i = path.size()-1; i >= 0; i--){
printf("%d ", path[i]);
}
printf("%d %d", d[c2], optValue);
return 0;
}
标签:back turn oid temp ack 代码 技术 next 遍历
原文地址:https://www.cnblogs.com/tsruixi/p/12395412.html