运输问题表上作业法c语言
时间: 2023-12-21 09:22:52 浏览: 117
运输问题是一种经典的线性规划问题,可以使用线性规划算法来解决。下面是一个使用 C 语言实现运输问题表上作业法的示例代码:
```c
#include <stdio.h>
#define MAX 100
void input(int a[MAX][MAX], int n, int m);
void output(int a[MAX][MAX], int n, int m);
void transport(int a[MAX][MAX], int n, int m, int supply[MAX], int demand[MAX]);
int main() {
int a[MAX][MAX], n, m, supply[MAX], demand[MAX];
printf("Enter the number of sources: ");
scanf("%d", &n);
printf("Enter the number of destinations: ");
scanf("%d", &m);
printf("Enter the transportation cost table:\n");
input(a, n, m);
printf("Enter the supply values:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &supply[i]);
}
printf("Enter the demand values:\n");
for (int j = 0; j < m; j++) {
scanf("%d", &demand[j]);
}
printf("Transportation cost table:\n");
output(a, n, m);
transport(a, n, m, supply, demand);
return 0;
}
void input(int a[MAX][MAX], int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &a[i][j]);
}
}
}
void output(int a[MAX][MAX], int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("%d\t", a[i][j]);
}
printf("\n");
}
}
void transport(int a[MAX][MAX], int n, int m, int supply[MAX], int demand[MAX]) {
int x[MAX][MAX] = {0}, u[MAX] = {0}, v[MAX] = {0}, min, i, j, k;
for (k = 1; k <= n + m - 1; k++) {
min = 1000000000;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (x[i][j] == 0 && a[i][j] - u[i] - v[j] < min) {
min = a[i][j] - u[i] - v[j];
k = i;
l = j;
}
}
}
x[k][l] = supply[k] < demand[l] ? supply[k] : demand[l];
supply[k] -= x[k][l];
demand[l] -= x[k][l];
for (i = 0; i < n; i++) {
if (x[i][l] != 0) {
u[i] += min;
}
}
for (j = 0; j < m; j++) {
if (x[k][j] != 0) {
v[j] -= min;
}
}
}
printf("Transportation matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
printf("%d\t", x[i][j]);
}
printf("\n");
}
}
```
该代码实现了一个简单的运输问题表上作业法程序,通过输入运输花费表、供应量和需求量,输出最终的运输方案。
阅读全文