https://www.liuchuo.net/archives/2369
X.
https://www.cnblogs.com/demian/p/6090189.html
A traveler’s map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (<=500) is the number of cities (and hence the cities are numbered from 0 to N-1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:
City1 City2 Distance Cost
where the numbers are all integers no more than 500, and are separated by a space.
Output Specification:
For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.
Sample Input
4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20
Sample Output
0 2 3 3 40
题目大意:求起点到终点的最短路径最短距离和花费,要求首先路径最短,其次花费最少,要输出完整路径
分析:Dijksta + DFS。 Dijkstra记录路径pre数组,然后用dfs求最短的一条mincost以及它的路径path,最后输出path数组和mincost
注意路径path因为是从末端一直压入push_back到path里面的,所以要输出路径的时候倒着输出
https://blog.csdn.net/qq_38283262/article/details/848427164 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20
Sample Output
0 2 3 3 40
题目大意:求起点到终点的最短路径最短距离和花费,要求首先路径最短,其次花费最少,要输出完整路径
分析:Dijksta + DFS。 Dijkstra记录路径pre数组,然后用dfs求最短的一条mincost以及它的路径path,最后输出path数组和mincost
注意路径path因为是从末端一直压入push_back到path里面的,所以要输出路径的时候倒着输出
这题对比上题是将点权换成了边权,先通过Dijkstra算法求出多条最短路径,然后用DFS找到最短路径中边权(此题中就是cost)最小的那条路径。
private static int n;
private static int m;
private static int s;
private static int d;
private static int[][] e;
private static int[][] cost;
private static int[] dis;
private static boolean[] visit;
private static ArrayList<Integer>[] pre;
private static LinkedList<Integer> tempPath = new LinkedList<>();
private static LinkedList<Integer> path = new LinkedList<>();
private static int min = Integer.MAX_VALUE;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
s = sc.nextInt();
d = sc.nextInt();
visit = new boolean[n];
e = new int[n][n];
cost = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
e[i][j] = e[j][i] = Integer.MAX_VALUE;
cost[i][j] = cost[j][i] = Integer.MAX_VALUE;
}
}
for (int i = 0; i < m; i++) {
int i1 = sc.nextInt();
int i2 = sc.nextInt();
e[i1][i2] = e[i2][i1] = sc.nextInt();
cost[i1][i2] = cost[i2][i1] = sc.nextInt();
}
dis = new int[n];
for (int i = 0; i < n; i++) {
dis[i] = Integer.MAX_VALUE;
}
dis[s] = 0;
pre = new ArrayList[n];
for (int i = 0; i < n; i++) {
pre[i] = new ArrayList<>();
}
/***********************************************/
for (int i = 0; i < n; i++) {
int u = -1, min = Integer.MAX_VALUE;
for (int j = 0; j < n; j++) {
if (!visit[j] && dis[j] < min) {
min = dis[j];
u = j;
}
}
if (u == -1)
break;
visit[u] = true;
for (int v = 0; v < n; v++) {
if (!visit[v] && e[u][v] != Integer.MAX_VALUE) {
if (dis[v] > dis[u] + e[u][v]) {
dis[v] = dis[u] + e[u][v];
pre[v].clear();
pre[v].add(u);
} else if (dis[v] == dis[u] + e[u][v]) {
pre[v].add(u);
}
}
}
}
/**********************************************/
ArrayList<Integer>[] temppre = pre;
dfs(d);
for (int i = 0; i < path.size(); i++) {
System.out.print(path.get(i) + " ");
}
System.out.print(dis[d] + " ");
System.out.print(min);
}
private static void dfs(int v) {
tempPath.push(v);
if (v == s) {
int c = 0;
for (int i = 1; i < tempPath.size(); i++) {
c += cost[tempPath.get(i)][tempPath.get(i - 1)];
}
if (c < min) {
min = c;
path = new LinkedList<>(tempPath);
}
tempPath.pop();
return;
}
for (int i = 0; i < pre[v].size(); i++)
dfs(pre[v].get(i));
tempPath.pop();
}
X.
https://www.cnblogs.com/demian/p/6090189.html
- const int maxn = 5e2 + 10;
- int n, m, s, t, map[maxn][maxn], cost[maxn][maxn], x, y, z, c;
- int dis[maxn], v[maxn];
- void dfs(int x)
- {
- if (x == t)return;
- for (int i = 0; i < n; i++)
- {
- if (map[x][i])
- {
- if (dis[i]>dis[x]+map[x][i])
- {
- dis[i] = dis[x] + map[x][i];
- v[i] = v[x] + cost[x][i];
- dfs(i);
- }
- else if (dis[i] == dis[x] + map[x][i] && v[i] > v[x] + cost[x][i])
- {
- v[i] = v[x] + cost[x][i];
- dfs(i);
- }
- }
- }
- }
- bool Dfs(int x)
- {
- if (x == s){ printf("%d ", s); return true; }
- for (int i = 0; i < n; i++)
- {
- if (map[x][i] && dis[x] == dis[i] + map[x][i] && v[x] == v[i] + cost[x][i])
- {
- if (Dfs(i)){printf("%d ", x); return true;}
- }
- }
- return false;
- }
- int main()
- {
- scanf("%d%d%d%d", &n, &m, &s, &t);
- while (m--)
- {
- scanf("%d%d%d%d", &x, &y, &z, &c);
- if (!map[x][y] || map[x][y] > z)
- {
- map[x][y] = map[y][x] = z;
- cost[x][y] = cost[y][x] = c;
- }
- else if (map[x][y] == z) cost[x][y] = cost[y][x] = min(z, cost[x][y]);
- }
- for (int i = 0; i < n; i++)dis[i] = v[i] = INF;
- dis[s] = v[s] = 0;
- dfs(s);
- Dfs(t);
- printf("%d %d\n", dis[t], v[t]);
- return 0;
- }