山区医疗点选址及道路维修优化问题:基于 Prim 和 Kruskal 算法的求解

假设某山区中有 100 个村庄,现在要在村庄中建立几个医疗点,方便村民看病。图 1 中给出这 100 个村庄的位置及可选道路连接示意图。附件数据的'位置'表单给出了这 100 个村庄的坐标(单位:米),附件数据的'连接道路'表单给出了可供选择的道路。现在要在 100 个村庄中建立 3 个医疗点,并在可选道路中根据需要进行部分道路进行修建,假定村民看病都选择修建后的道路。

如果各村庄村民到医疗点的距离太远,不便于看病,因此站在村民角度出发,希望各村庄村民到医疗点的距离尽量小。于是我们可以找出三个医疗点。由于每条道路维修都需要成本,因此站在道路维修公司角度出发,希望维修的成本尽量低。现假设医疗点即为站在村民角度使得各村庄到医疗点路程最短的要求所得的那三个医疗点,那么应该维修哪些道路,使得维修成本最低(即各村庄与医疗点连通即可,要求所修道路总长度最短)。给出应修道路的总长度 S2,并做出图形。同时根据维修的道路,计算各村庄到医疗点的总距离 S1(其中两点间距离不可直接按照坐标计算,必须是可选道路所连通的路线)。

以下给出数据:

位置:

| 村庄序号 | X坐标 | Y坐标 | |---|---|---| | 1 | 5500 | 200 | | 2 | 9300 | 300 | | 3 | 7800 | 400 | | 4 | 3100 | 500 | | 5 | 9700 | 600 | | 6 | 6000 | 800 | | 7 | 1200 | 900 | | 8 | 3900 | 900 | | 9 | 5600 | 900 | | 10 | 6800 | 1000 | | 11 | 6350 | 1100 | | 12 | 7500 | 1200 | | 13 | 2000 | 1400 | | 14 | 6100 | 1400 | | 15 | 5000 | 1500 | | 16 | 9900 | 1500 | | 17 | 8200 | 2000 | | 18 | 800 | 1800 | | 19 | 3000 | 1900 | | 20 | 4000 | 1900 | | 21 | 9000 | 1900 | | 22 | 7900 | 1500 | | 23 | 1800 | 2300 | | 24 | 7700 | 2300 | | 25 | 500 | 2400 | | 26 | 4800 | 2400 | | 27 | 10000 | 2400 | | 28 | 8100 | 2600 | | 29 | 1900 | 2700 | | 30 | 3900 | 2700 | | 31 | 6700 | 2500 | | 32 | 6400 | 3100 | | 33 | 8800 | 3200 | | 34 | 5400 | 3400 | | 35 | 800 | 3600 | | 36 | 7500 | 3700 | | 37 | 9300 | 3500 | | 38 | 9500 | 3900 | | 39 | 9900 | 3500 | | 40 | 4000 | 4100 | | 41 | 7300 | 4200 | | 42 | 300 | 4400 | | 43 | 2400 | 4400 | | 44 | 5900 | 4400 | | 45 | 2700 | 4000 | | 46 | 600 | 4800 | | 47 | 3800 | 4600 | | 48 | 1000 | 4800 | | 49 | 6800 | 4900 | | 50 | 9300 | 4900 | | 51 | 700 | 4100 | | 52 | 100 | 5100 | | 53 | 4500 | 5100 | | 54 | 8600 | 5100 | | 55 | 3300 | 5200 | | 56 | 1200 | 5300 | | 57 | 2900 | 5400 | | 58 | 7500 | 5500 | | 59 | 200 | 5600 | | 60 | 6800 | 5800 | | 61 | 9800 | 5400 | | 62 | 4900 | 6000 | | 63 | 9900 | 6100 | | 64 | 2100 | 6300 | | 65 | 4100 | 6300 | | 66 | 4400 | 6500 | | 67 | 9000 | 6500 | | 68 | 9500 | 6400 | | 69 | 9800 | 6800 | | 70 | 6800 | 6900 | | 71 | 1300 | 7200 | | 72 | 5100 | 7200 | | 73 | 2500 | 7300 | | 74 | 1800 | 7500 | | 75 | 4700 | 7500 | | 76 | 5800 | 7800 | | 77 | 300 | 7400 | | 78 | 4900 | 8000 | | 79 | 100 | 8100 | | 80 | 1000 | 7600 | | 81 | 1600 | 8200 | | 82 | 7700 | 8200 | | 83 | 1200 | 8300 | | 84 | 5900 | 8300 | | 85 | 100 | 8600 | | 86 | 9500 | 8100 | | 87 | 9300 | 8700 | | 88 | 5200 | 8900 | | 89 | 1000 | 9000 | | 90 | 2800 | 9100 | | 91 | 4800 | 9100 | | 92 | 700 | 9200 | | 93 | 3400 | 9200 | | 94 | 8200 | 9300 | | 95 | 9100 | 9400 | | 96 | 4800 | 9500 | | 97 | 1900 | 9700 | | 98 | 3000 | 9800 | | 99 | 9800 | 9800 | | 100 | 6200 | 10000 |

连接道路:

| 序号 | 起点 | 终点 | |---|---|---| | 1 | 1 | 6 | | 2 | 1 | 8 | | 3 | 2 | 3 | | 4 | 2 | 5 | | 5 | 3 | 10 | | 6 | 4 | 8 | | 7 | 4 | 13 | | 8 | 5 | 16 | | 9 | 5 | 21 | | 10 | 6 | 9 | | 11 | 6 | 10 | | 12 | 7 | 13 | | 13 | 7 | 18 | | 14 | 8 | 9 | | 15 | 8 | 15 | | 16 | 9 | 11 | | 17 | 10 | 11 | | 18 | 10 | 12 | | 19 | 11 | 12 | | 20 | 11 | 14 | | 21 | 12 | 14 | | 22 | 12 | 22 | | 23 | 13 | 18 | | 24 | 13 | 19 | | 25 | 14 | 15 | | 26 | 14 | 26 | | 27 | 15 | 20 | | 28 | 15 | 26 | | 29 | 16 | 21 | | 30 | 17 | 21 | | 31 | 17 | 22 | | 32 | 18 | 23 | | 33 | 18 | 25 | | 34 | 19 | 20 | | 35 | 19 | 23 | | 36 | 20 | 26 | | 37 | 20 | 30 | | 38 | 21 | 22 | | 39 | 21 | 28 | | 40 | 22 | 24 | | 41 | 22 | 28 | | 42 | 23 | 25 | | 43 | 23 | 29 | | 44 | 24 | 28 | | 45 | 24 | 31 | | 46 | 25 | 29 | | 47 | 25 | 35 | | 48 | 26 | 30 | | 49 | 26 | 32 | | 50 | 27 | 33 | | 51 | 27 | 37 | | 52 | 28 | 31 | | 53 | 28 | 32 | | 54 | 29 | 35 | | 55 | 29 | 43 | | 56 | 30 | 34 | | 57 | 30 | 40 | | 58 | 31 | 32 | | 59 | 32 | 34 | | 60 | 32 | 36 | | 61 | 33 | 36 | | 62 | 33 | 37 | | 63 | 34 | 40 | | 64 | 34 | 44 | | 65 | 35 | 42 | | 66 | 35 | 43 | | 67 | 36 | 41 | | 68 | 36 | 44 | | 69 | 37 | 38 | | 70 | 37 | 39 | | 71 | 38 | 39 | | 72 | 38 | 50 | | 73 | 39 | 50 | | 74 | 40 | 45 | | 75 | 41 | 44 | | 76 | 41 | 49 | | 77 | 42 | 52 | | 78 | 43 | 45 | | 79 | 44 | 49 | | 80 | 44 | 53 | | 81 | 45 | 47 | | 82 | 45 | 55 | | 83 | 46 | 48 | | 84 | 46 | 51 | | 85 | 47 | 53 | | 86 | 47 | 55 | | 87 | 48 | 51 | | 88 | 48 | 52 | | 89 | 49 | 58 | | 90 | 49 | 60 | | 91 | 50 | 54 | | 92 | 50 | 61 | | 93 | 51 | 52 | | 94 | 51 | 56 | | 95 | 52 | 56 | | 96 | 52 | 59 | | 97 | 53 | 55 | | 98 | 54 | 58 | | 99 | 54 | 61 | | 100 | 55 | 57 | | 101 | 55 | 62 | | 102 | 56 | 57 | | 103 | 56 | 59 | | 104 | 57 | 64 | | 105 | 57 | 65 | | 106 | 58 | 60 | | 107 | 58 | 70 | | 108 | 60 | 70 | | 109 | 61 | 63 | | 110 | 61 | 67 | | 111 | 62 | 65 | | 112 | 62 | 66 | | 113 | 63 | 67 | | 114 | 63 | 68 | | 115 | 64 | 71 | | 116 | 64 | 73 | | 117 | 65 | 66 | | 118 | 66 | 72 | | 119 | 66 | 75 | | 120 | 67 | 68 | | 121 | 68 | 69 | | 122 | 68 | 86 | | 123 | 69 | 86 | | 124 | 70 | 72 | | 125 | 70 | 76 | | 126 | 71 | 73 | | 127 | 71 | 74 | | 128 | 72 | 75 | | 129 | 72 | 76 | | 130 | 73 | 74 | | 131 | 74 | 77 | | 132 | 74 | 80 | | 133 | 75 | 76 | | 134 | 75 | 78 | | 135 | 76 | 78 | | 136 | 76 | 84 | | 137 | 77 | 79 | | 138 | 77 | 80 | | 139 | 78 | 84 | | 140 | 78 | 88 | | 141 | 79 | 80 | | 142 | 79 | 81 | | 143 | 80 | 81 | | 144 | 81 | 83 | | 145 | 81 | 85 | | 146 | 82 | 87 | | 147 | 82 | 94 | | 148 | 82 | 100 | | 149 | 83 | 85 | | 150 | 83 | 89 | | 151 | 84 | 88 | | 152 | 85 | 89 | | 153 | 85 | 92 | | 154 | 86 | 87 | | 155 | 87 | 94 | | 156 | 87 | 95 | | 157 | 88 | 91 | | 158 | 89 | 92 | | 159 | 89 | 97 | | 160 | 90 | 93 | | 161 | 90 | 97 | | 162 | 91 | 93 | | 163 | 91 | 96 | | 164 | 92 | 97 | | 165 | 93 | 96 | | 166 | 93 | 97 | | 167 | 94 | 95 | | 168 | 94 | 99 | | 169 | 94 | 100 | | 170 | 95 | 99 | | 171 | 96 | 100 | | 172 | 97 | 98 |

要求给出建模过程、运用kruskal算法的Matlab代码与求解结果。

内容:

建模过程:

首先,我们需要找到三个最佳的医疗点,使得村民到医疗点的距离尽量小。为了达到这个目标,我们可以使用 Prim 算法或 Dijkstra 算法,找到最小生成树或最短路径树,然后选择覆盖所有村庄的三个医疗点。这里我们选择使用 Prim 算法,因为它更适合于稠密图。

其次,我们需要找出应该维修哪些道路,以使得各村庄与医疗点连通,并且所修道路总长度最短。为了达到这个目标,我们可以使用 Kruskal 算法,找到最小生成树。这里我们选择使用 Kruskal 算法,因为它在计算边权值时不需要考虑具体的路径,只需要考虑边的长度。

最后,我们需要计算各村庄到医疗点的总距离。由于两点间距离不可直接按照坐标计算,必须是可选道路所连通的路线,因此我们可以使用 Dijkstra 算法,找到每个村庄到三个医疗点的最短路径,并将其相加得到总距离。

Matlab 代码:


% 计算距离矩阵n = size(pos, 1);dist = zeros(n, n);for i = 1:n    for j = i+1:n        dx = pos(i, 1) - pos(j, 1);        dy = pos(i, 2) - pos(j, 2);        dist(i, j) = sqrt(dx^2 + dy^2);        dist(j, i) = dist(i, j);    endend

% 使用 Prim 算法找到三个医疗点start = 1;visited = zeros(n, 1);visited(start) = 1;medicals = [start];while length(medicals) < 3    min_dist = Inf;    next_medical = -1;    for i = 1:length(medicals)        for j = 1:n            if visited(j) == 0 && dist(medicals(i), j) < min_dist                min_dist = dist(medicals(i), j);                next_medical = j;            end        end    end    visited(next_medical) = 1;    medicals = [medicals; next_medical];end

% 使用 Kruskal 算法找到应修道路edges = [];for i = 1:size(road, 1)    if ismember(road(i, 1), medicals) && ismember(road(i, 2), medicals)        edges = [edges; road(i, :)];    endend[E, L] = kruskal(edges, n);

% 计算修建的道路总长度S2 = sum(L);

% 计算各村庄到医疗点的总距离S1 = 0;for i = 1:n    min_dist = Inf;    for j = 1:3        dist_medical = dijkstra(E, L, medicals(j), i);        if dist_medical < min_dist            min_dist = dist_medical;        end    end    S1 = S1 + min_dist;end

% 绘制图形figure;hold on;axis equal;for i = 1:size(road, 1)    x1 = pos(road(i, 1), 1);    y1 = pos(road(i, 1), 2);    x2 = pos(road(i, 2), 1);    y2 = pos(road(i, 2), 2);    if ismember([road(i, 1), road(i, 2)], E, 'rows') || ismember([road(i, 2), road(i, 1)], E, 'rows')        plot([x1, x2], [y1, y2], 'b-', 'LineWidth', 2);    else        plot([x1, x2], [y1, y2], 'r--');    endendscatter(pos(medicals, 1), pos(medicals, 2), 60, 'g', 'filled');scatter(pos(:, 1), pos(:, 2), 20, 'k', 'filled');text(pos(medicals(1), 1), pos(medicals(1), 2), 'M1', 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom', 'FontSize', 12);text(pos(medicals(2), 1), pos(medicals(2), 2), 'M2', 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom', 'FontSize', 12);text(pos(medicals(3), 1), pos(medicals(3), 2), 'M3', 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom', 'FontSize', 12);xlim([0, 10000]);ylim([0, 11000]);

% Kruskal 算法实现function [E, L] = kruskal(edges, n)    E = [];    L = [];    sets = (1:n)';    for i = 1:size(edges, 1)        u = edges(i, 1);        v = edges(i, 2);        w = edges(i, 3);        if sets(u) ~= sets(v)            E = [E; u, v];            L = [L; w];            old_set = sets(v);            new_set = sets(u);            sets(sets == old_set) = new_set;        end    endend

% Dijkstra 算法实现function dist = dijkstra(E, L, start, end_)    n = max(max(E));    dist = Inf(1, n);    dist(start) = 0;    visited = zeros(1, n);    for i = 1:n        min_dist = Inf;        current = -1;        for j = 1:n            if visited(j) == 0 && dist(j) < min_dist                min_dist = dist(j);                current = j;            end        end        if current == -1 || current == end_            break;        end        visited(current) = 1;        neighbors = E(E(:,1)==current | E(:,2)==current, :);        for j = 1:size(neighbors, 1)            if neighbors(j,1) == current                neighbor = neighbors(j,2);            else                neighbor = neighbors(j,1);            end            if visited(neighbor) == 0                new_dist = dist(current) + L(j);                if new_dist < dist(neighbor)                    dist(neighbor) = new_dist;                end            end        end    ende

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

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