C 隧道路线图
给定多个城市之间的路线图,两个城市之间的道路包含隧道,您的目标是找到起点城市和所有其他城市之间的最短可能路径,以便每条路径至少包含一条隧道。(问题并不总是有解决办法)。假设给定了道路的成本。输入-从一个文件,输出-到一个文件,包含起始城市和到其他城市的路径 现在我试着用Dijkstra的算法来解决这个问题,它解决了我的大部分问题,除了隧道是强制性的部分。有人能帮我吗?这是我的密码。 提前谢谢 文件输入: 10 1 2 10 1 4 5 2 3 1 2 4 3 3 5 6 4 2 2 4 3 9 4 5 2 5 1 7 5 3 4C 隧道路线图,c,algorithm,dijkstra,shortest-path,C,Algorithm,Dijkstra,Shortest Path,给定多个城市之间的路线图,两个城市之间的道路包含隧道,您的目标是找到起点城市和所有其他城市之间的最短可能路径,以便每条路径至少包含一条隧道。(问题并不总是有解决办法)。假设给定了道路的成本。输入-从一个文件,输出-到一个文件,包含起始城市和到其他城市的路径 现在我试着用Dijkstra的算法来解决这个问题,它解决了我的大部分问题,除了隧道是强制性的部分。有人能帮我吗?这是我的密码。 提前谢谢 文件输入: 10 1 2 10 1 4 5 2 3 1 2 4 3 3 5 6 4 2 2 4 3 9
#包括
#定义图形化2048
#定义无限图形化*图形化
#定义最大值(a,b)((a>b)?(a):(b))
int e;/*图中非零边的数目*/
int n;/*图中的节点数*/
长距离[图形化][图形化];/*dist[i][j]是节点i和j之间的距离;如果没有直接连接,则为0*/
长d[图形化];/*d[i]是源和节点i之间最短路径的长度*/
int prev[图形化];/*prev[i]是在从源到i*的最短路径中位于i之前的节点
void printD(){
int i;
printf(“距离:\n”);
对于(i=1;i运行Dijkstra算法,以找到从起始城市到所有隧道的所有最短路径
再次运行Dijkstra算法,以所有的隧道为起点,找到所有其他城市的最短路径。因此,在Dijkstra算法的中间开始,你已经有了一堆候选(所有的隧道),所有这些都会被标记为被访问。
看起来您没有使用优先级队列(Dijkstra算法的有效实现使用了优先级队列),但我相信您会设法找出如何将我的解决方案应用到代码中。您可以这样构造一个图:每个城市有两个节点(例如,每个城市C有C和C')。对于每条道路,例如从C1到C2,在图表中添加边:C1->C2和C1'->C2。对于每条隧道,例如从C1到C2,在图表C1->C2'和C1'->C2'中添加边
直觉告诉我们,要到达一个C'节点,你必须通过至少一个隧道
现在,要使用至少一个隧道找到从C1到C2的最短路径,只需使用Dijkstra查找从C1到C2的最短路径。或者要查找到每个城市的最短路径,请为每个城市C查找从起始城市到C'的所有最短路径。请重新格式化您的示例输入。
和--
行是否重要?
#include <stdio.h>
#define GRAPHSIZE 2048
#define INFINITY GRAPHSIZE*GRAPHSIZE
#define MAX(a, b) ((a > b) ? (a) : (b))
int e; /* The number of nonzero edges in the graph */
int n; /* The number of nodes in the graph */
long dist[GRAPHSIZE][GRAPHSIZE];/* dist[i][j] is the distance between node i and j; or 0 if there is no direct connection */
long d[GRAPHSIZE]; /* d[i] is the length of the shortest path between the source (s) and node i */
int prev[GRAPHSIZE]; /* prev[i] is the node that comes right before i in the shortest path from the source to i*/
void printD() {
int i;
printf("Distances:\n");
for (i = 1; i <= n; ++i)
printf("%10d", i);
printf("\n");
for (i = 1; i <= n; ++i) {
printf("%10ld", d[i]);
}
printf("\n");
}
/*
* Prints the shortest path from the source to dest.
* dijkstra(int) MUST be run at least once BEFORE
* this is called
*/
void printPath(int dest) {
if (prev[dest] != -1)
printPath(prev[dest]);
printf("%d ", dest);
}
void dijkstra(int s) {
int i, k, mini;
int visited[GRAPHSIZE];
for (i = 1; i <= n; ++i) {
d[i] = INFINITY;
prev[i] = -1; /* no path has yet been found to i */
visited[i] = 0; /* the i-th element has not yet been visited */
}
d[s] = 0;
for (k = 1; k <= n; ++k) {
mini = -1;
for (i = 1; i <= n; ++i)
if (!visited[i] && ((mini == -1) || (d[i] < d[mini])))
mini = i;
visited[mini] = 1;
for (i = 1; i <= n; ++i)
if (dist[mini][i])
if (d[mini] + dist[mini][i] < d[i]) {
d[i] = d[mini] + dist[mini][i];
prev[i] = mini;
}
}
}
int main(int argc, char *argv[]) {
int i, j;
int u, v, w;
FILE *fin = fopen("dist.txt", "r");
/* the first line contains e, the number of edges the following e lines
contain 3 numbers: u, v and w signifying that there’s an edge from u to v of weight w*/
fscanf(fin, "%d", &e);
for (i = 0; i < e; ++i)
for (j = 0; j < e; ++j)
dist[i][j] = 0;
n = -1;
for (i = 0; i < e; ++i) {
fscanf(fin, "%d%d%d", &u, &v, &w);
dist[u][v] = w;
n = MAX(u, MAX(v, n));
}
fclose(fin);
dijkstra(1);
printD();
printf("\n");
for (i = 1; i <= n; ++i) {
printf("Path to %d: ", i);
printPath(i);
printf("\n");
}
return 0;
}