整理一下做完整的钢管订购与运输问题(题1,3)[前面的数据输入有误,多谢明明同学帮忙找错……]

首先铁路的数据

a = {{1, 29, 20}, {1, 30, 202}, {2, 30, 1200}, {3, 31, 690}, {4, 33,
690}, {5, 35, 462}, {6, 38, 70}, {7, 39, 30}, {23, 24, 450}, {24,
25, 80}, {24, 26, 1150}, {27, 28, 306}, {28, 29, 195}, {26, 30,
1100}, {30, 31, 720}, {31, 32, 520}, {32, 33, 170}, {33, 35,
88}, {33, 34, 160}, {34, 36, 70}, {34, 37, 320}, {37, 38,
160}, {38, 39, 290}}

公路数据(题1)

b = {{8, 9, 104}, {9, 10, 301}, {10, 11, 750}, {11, 12, 606}, {12, 13,
194}, {13, 14, 205}, {14, 15, 201}, {15, 16, 680}, {16, 17,
480}, {17, 18, 300}, {18, 19, 220}, {19, 20, 210}, {20, 21,
420}, {21, 22, 500}, {9, 23, 3}, {10, 25, 2}, {11, 26, 600}, {12,
27, 10}, {13, 28, 5}, {14, 29, 10}, {1, 14, 31}, {15, 30, 12}, {16,
31, 42}, {17, 32, 70}, {18, 35, 10}, {19, 36, 10}, {20, 37,
62}, {6, 21, 110}, {21, 38, 30}, {22, 39, 20}, {7, 22, 20}}

公路数据(题2)

b = {{8, 9, 104}, {9, 10, 301}, {10, 11, 750}, {11, 12, 606}, {12, 13,
194}, {13, 14, 205}, {14, 15, 201}, {15, 16, 680}, {16, 17,
480}, {17, 18, 300}, {18, 19, 220}, {19, 20, 210}, {20, 21,
420}, {21, 22, 500}, {9, 23, 3}, {10, 25, 2}, {11, 26, 600}, {12,
27, 10}, {13, 28, 5}, {14, 29, 10}, {1, 14, 31}, {15, 30, 12}, {16,
31, 42}, {17, 32, 70}, {18, 35, 10}, {19, 36, 10}, {20, 37,
62}, {6, 21, 110}, {21, 38, 30}, {22, 39, 20}, {7, 22, 20}, {34,
35, 130}, {35, 36, 190}, {36, 37, 260}, {6, 37, 100}}

Floyd算法

Floyd[n_, a_List] :=
Module[{i, j, k}, d = Table[Infinity, {i, n}, {j, n}];
r = Table[j, {i, n}, {j, n}];
For[i = 1, i <= n, i++, d[[i, i]] = 0];
For[i = 1, i <= Length[a], i++,
d[[a[[i, 1]], a[[i, 2]]]] = a[[i, 3]];
d[[a[[i, 2]], a[[i, 1]]]] = a[[i, 3]]];
For[k = 1, k <= n, k++,
For[i = 1, i <= n, i++,
For[j = 1, j <= n, j++,
If[d[[i, k]] + d[[k, j]] < d[[i, j]],
d[[i, j]] = d[[i, k]] + d[[k, j]];
r[[i, j]] = r[[i, k]]]]]]];
Floyd[39, a];
MatrixForm[d]
MatrixForm[r]

铁路运费求法(Which和If嵌套都可以)

For[i = 1, i <= 39, i++,
For[j = 1, j <= 39, j++,
Which[d[[i, j]] > 0 && d[[i, j]] <= 300, d[[i, j]] = 20,
d[[i, j]] >= 301 && d[[i, j]] <= 350, d[[i, j]] = 23,
d[[i, j]] >= 351 && d[[i, j]] <= 400, d[[i, j]] = 26,
d[[i, j]] >= 401 && d[[i, j]] <= 450, d[[i, j]] = 29,
d[[i, j]] >= 451 && d[[i, j]] <= 500, d[[i, j]] = 32,
d[[i, j]] >= 501 && d[[i, j]] <= 600, d[[i, j]] = 37,
d[[i, j]] >= 601 && d[[i, j]] <= 700, d[[i, j]] = 44,
d[[i, j]] >= 701 && d[[i, j]] <= 800, d[[i, j]] = 50,
d[[i, j]] >= 801 && d[[i, j]] <= 900, d[[i, j]] = 55,
d[[i, j]] >= 901 && d[[i, j]] <= 1000, d[[i, j]] = 60,
d[[i, j]] > 1000 && d[[i, j]] < Infinity,
If[Mod[(d[[i, j]] – 1000), 100] == 0,
d[[i, j]] = 5*d[[i, j]]/100 + 10,
d[[i, j]] = 5*IntegerPart[(d[[i, j]] – 1000)/100] + 65]
]
]
]
MatrixForm[d]

两者合并

For[i = 1, i <= 39, i++,
For[j = 1, j <= 39, j++,
If[aa[[i, j]] <= cc[[i, j]], ee[[i, j]] = aa[[i, j]],
ee[[i, j]] = cc[[i, j]]
]
]
]
MatrixForm[ee]

合并之后再一次FLoyd

For[k = 1, k <= 39, k++,
For[i = 1, i <= 39, i++,
For[j = 1, j <= 39, j++,
If[ee[[i, k]] + ee[[k, j]] < ee[[i, j]],
ee[[i, j]] = ee[[i, k]] + ee[[k, j]],
r[[i, j]] = r[[i, k]]
]]]]
MatrixForm[ee]

提取第一题中的运费

m = Table[0, {i, 7}, {j, 14}];
For[i = 1, i <= 7, i++,
For[j = 1, j <= 14, j++,
m[[i, j]] = ee[[i, j + 8]]
]
];
MatrixForm[m]

加上出厂费

For[j = 1, j <= 14, j++,
m[[1, j]] = m[[1, j]] + 160;
m[[2, j]] = m[[2, j]] + 155;
m[[3, j]] = m[[3, j]] + 155;
m[[4, j]] = m[[4, j]] + 160;
m[[5, j]] = m[[5, j]] + 155;
m[[6, j]] = m[[6, j]] + 150;
m[[7, j]] = m[[7, j]] + 160
];
MatrixForm[m]

同理可求出题3的数据

最终数据题1

钢管题2

最终数据题3

钢管题1

One Reply to “整理一下做完整的钢管订购与运输问题(题1,3)[前面的数据输入有误,多谢明明同学帮忙找错……]”

发表评论

电子邮件地址不会被公开。 必填项已用*标注