# PAT 甲级 A1030

## 题目连接

1030 Travel Plan (30分)

## 思路

/*边定义*/
struct arc
{
int dis, cost;
}V[maxn][maxn];


/*root表示的是当前结点, s表示起始结点*/
vector<int> path, temp_path;
void DFS(int root) {
//当前结点为起始节点时，为一条完整的最短路径(从目标结点开始回溯)
if (root == s) {
temp_path.push_back(root);
int cost = 0;
//计算当前路径成本cost
for (int i = temp_path.size() - 1; i > 0 ; i--) {
int cur = temp_path[i], next = temp_path[i - 1];
cost += V[cur][next].cost;
}
if (cost < minCost) {
minCost = cost;
path = temp_path;
}
temp_path.pop_back();
}
else {
temp_path.push_back(root);
for (int i = 0; i < pre[root].size(); i++) {
DFS(pre[root][i]);
}
temp_path.pop_back();
}
}


## AC代码

#include<iostream>
#include<algorithm>
#include<vector>
#define maxn 505
#define INF ((1 << 31) - 1)
using namespace std;

struct arc
{
int dis, cost;
}V[maxn][maxn];

int n, m, s, d, minCost = INF;
int dis[maxn];
vector<int> pre[maxn], path, temp_path;
bool visit[maxn] = { false };

void Dijkstra() {
fill(dis, dis + maxn, INF);
dis[s] = 0;

for (int i = 0; i < n; i++) {
int u = -1, min = INF;
for (int j = 0; j < n; j++) {
if (!visit[j] && dis[j] < min) {
min = dis[j];
u = j;
}
}

if (u == -1) return;
visit[u] = true;

for (int v = 0; v < n; v++) {
if (!visit[v] && V[u][v].dis > 0) {

if (dis[u] + V[u][v].dis < dis[v]) {
dis[v] = dis[u] + V[u][v].dis;
pre[v].clear();
pre[v].push_back(u);
}
else if (dis[u] + V[u][v].dis == dis[v]){
pre[v].push_back(u);
}

}
}
}
}

void DFS(int root) {
if (root == s) {
temp_path.push_back(root);
int cost = 0;
//计算当前路径成本cost
for (int i = temp_path.size() - 1; i > 0 ; i--) {
int cur = temp_path[i], next = temp_path[i - 1];
cost += V[cur][next].cost;
}
if (cost < minCost) {
minCost = cost;
path = temp_path;
}
temp_path.pop_back();
}
else {
temp_path.push_back(root);
for (int i = 0; i < pre[root].size(); i++) {
DFS(pre[root][i]);
}
temp_path.pop_back();
}
}
int main() {
scanf("%d%d%d%d", &n, &m, &s, &d);
for (int i = 0; i < m; i++) {
int u, v, dist, cost;
scanf("%d%d%d%d", &u, &v, &dist, &cost);
V[v][u].dis = V[u][v].dis = dist;
V[v][u].cost = V[u][v].cost = cost;
}

Dijkstra();
DFS(d);

//打印路径
for (int i = path.size() - 1; i >= 0 ; i--) {
printf("%d ", path[i]);
}
printf("%d %d\n", dis[d], minCost);
return 0;
}


©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客