-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathN_W.cpp
142 lines (134 loc) · 3.33 KB
/
N_W.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int cost,i,j,msizerow,msizecol,matrix[10][10];
int r=0,c=0,p;
int supply[10],demand[10];
int value[10][10],rpos,cpos,rpre,cpre;
void firstiteration();
main()
{
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
value[i][j]=0;
}
}
printf("\n Enter the size of the matrix(rows)=");
scanf("%d",&msizerow);
printf("\n Enter the size of the matrix(column)=");
scanf("%d",&msizecol);
printf("\n Enter the cost matrix=");
for(r=0;r<msizerow;r++)
{
for(c=0;c<msizecol;c++)
{
printf("\n Enter %d row %d column=",r+1,c+1);
scanf("%d",&matrix[r][c]);
}
}
printf("\n Enter the supply=\n");
for(r=0;r<msizerow;r++)
{
printf("%d =",r+1);
scanf("%d",&supply[r]);
}
printf("\n Enter the Demand=\n");
for(c=0;c<msizecol;c++)
{
printf("%d =",c+1);
scanf("%d",&demand[c]);
}
firstiteration();
getch();
}
void firstiteration()
{
c=0;r=0;cost=0;
do{
if(demand[c]!=0)
{
if(demand[c]<supply[r])
{
value[r][c]=demand[c];
supply[r]-=demand[c];
demand[c]=0;
if(c==msizecol-1)
c=0;
else
c++;
}
else
{
if(demand[c]==supply[r])
{
value[r][c]=supply[r];
demand[c]=0;
supply[r]=0;
if(c==msizecol-1)
c=0;
else
c++;
if(r==msizerow-1)
r=0;
else
r++;
}
else
{
value[r][c]=supply[r];
demand[c]-=supply[r];
if(r==msizerow-1)
r=0;
else
r++;
}
}
}
else
{
if(c==msizecol-1)
c=0;
else
c++;
}
}
while(demand[msizecol-1]!=0);
printf("\n");
for(r=0;r<msizerow;r++)
{
for(c=0;c<msizecol;c++)
{
//printf("\n Value[%d][%d]=%d",r+1,c+1,value[r][c]);
}
}
printf("\n The Transportation Problem \n");
for(r=0;r<msizerow;r++)
{
for(c=0;c<msizecol;c++)
{
printf("\t%d",matrix[r][c]);
//cost+=matrix[r][c]*value[r][c];
}
printf("\n");
}
printf("\n The Initial Matrix \n");
for(r=0;r<msizerow;r++)
{
for(c=0;c<msizecol;c++)
{
printf("\t%d",value[r][c]);
//cost+=matrix[r][c]*value[r][c];
}
printf("\n");
}
for(r=0;r<msizerow;r++)
{
for(c=0;c<msizecol;c++)
{
cost+=matrix[r][c]*value[r][c];
}
}
printf("\n Cost=%d",cost);
}