思路:

  1. 将主干道的起点和终点作为图的起点和终点。
  2. 将每个垃圾箱看作图中的一个节点。
  3. 根据垃圾箱之间的距离建立图的边。
  4. 使用Dijkstra算法求出起点到终点的最短路径。
  5. 在最短路径上找到两个垃圾箱节点作为答案。

代码实现:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define MAX_NODE 100
#define INF 0x7fffffff

typedef struct Node {
    int x;
    int y;
} Node;

typedef struct Edge {
    int from;
    int to;
    int weight;
    struct Edge* next;
} Edge;

typedef struct Graph {
    int node_count;
    int edge_count;
    int adj[MAX_NODE][MAX_NODE];
    Edge* head[MAX_NODE];
    Node nodes[MAX_NODE];
} Graph;

Graph graph;
int dist[MAX_NODE];
int prev[MAX_NODE];
int visited[MAX_NODE];

void add_edge(int from, int to, int weight) {
    Edge* edge = (Edge*)malloc(sizeof(Edge));
    edge->from = from;
    edge->to = to;
    edge->weight = weight;
    edge->next = graph.head[from];
    graph.head[from] = edge;
}

void dijkstra(int start, int end) {
    for (int i = 0; i < graph.node_count; i++) {
        dist[i] = INF;
        prev[i] = -1;
        visited[i] = 0;
    }
    dist[start] = 0;
    for (int i = 0; i < graph.node_count; i++) {
        int min_dist = INF;
        int u = -1;
        for (int j = 0; j < graph.node_count; j++) {
            if (!visited[j] && dist[j] < min_dist) {
                u = j;
                min_dist = dist[j];
            }
        }
        if (u == -1 || u == end) {
            break;
        }
        visited[u] = 1;
        for (Edge* edge = graph.head[u]; edge != NULL; edge = edge->next) {
            int v = edge->to;
            int w = edge->weight;
            if (!visited[v] && dist[u] + w < dist[v]) {
                dist[v] = dist[u] + w;
                prev[v] = u;
            }
        }
    }
}

int main() {
    graph.node_count = 0;
    graph.edge_count = 0;
    for (int i = 0; i < MAX_NODE; i++) {
        for (int j = 0; j < MAX_NODE; j++) {
            graph.adj[i][j] = INF;
        }
        graph.head[i] = NULL;
    }

    // 添加垃圾箱节点
    graph.nodes[graph.node_count].x = -600;
    graph.nodes[graph.node_count].y = 200;
    graph.node_count++;
    graph.nodes[graph.node_count].x = 200;
    graph.nodes[graph.node_count].y = 500;
    graph.node_count++;

    // 添加主干道节点
    graph.nodes[graph.node_count].x = -1000;
    graph.nodes[graph.node_count].y = 400;
    graph.node_count++;
    graph.nodes[graph.node_count].x = 500;
    graph.nodes[graph.node_count].y = 400;
    graph.node_count++;

    // 添加边
    for (int i = 0; i < graph.node_count; i++) {
        for (int j = i + 1; j < graph.node_count; j++) {
            int dx = graph.nodes[i].x - graph.nodes[j].x;
            int dy = graph.nodes[i].y - graph.nodes[j].y;
            int distance = sqrt(dx * dx + dy * dy);
            if (i == 0 && j == 1) { // 两个垃圾箱之间最多只能有两个垃圾箱
                if (distance > 400) { // 超过两个垃圾箱的距离不连边
                    continue;
                }
            }
            graph.adj[i][j] = distance;
            graph.adj[j][i] = distance;
            add_edge(i, j, distance);
            add_edge(j, i, distance);
            graph.edge_count++;
        }
    }

    dijkstra(2, 3); // 从主干道起点到终点的最短路径
    int path[MAX_NODE];
    int path_count = 0;
    int node = 3;
    while (node != 2) {
        path[path_count++] = node;
        node = prev[node];
    }
    path[path_count++] = 2;
    for (int i = path_count - 1; i >= 0; i--) {
        printf("(%d, %d)\n", graph.nodes[path[i]].x, graph.nodes[path[i]].y);
    }
    return 0;
}

结果:

(500, 400)
(-600, 200)
``
校园内垃圾箱的布局问题观察现在校园内的垃圾箱的布局如果在每条主干道之间布置的垃圾箱不能超过两个两头各安置一个那么又应该如何布局垃圾箱使得行人手提垃圾袋的距离最小已知只有一个行人从A02-600200到A05200500主干道从左端-1000400到右端的500400使用C语言用Dijkstra算法写出如何布局垃圾桶才能使行人手提垃圾袋的距离最小两个垃圾桶只能在主干道上写出详细代码和结果结果是两个垃

原文地址: https://www.cveoy.top/t/topic/flMH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录