-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsupply_route.c
176 lines (139 loc) · 3.59 KB
/
supply_route.c
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <stdio.h>
#include <time.h>
#define MAX 200
#define MAXNODES 40000
#define INF 999999
#define NO 0
#define YES 1
int inputmat[MAX][MAX];
int adjlist[MAXNODES][4];
/* Dij Data structures */
int dist[MAXNODES];
int visited[MAXNODES];
int IsSafe(int x, int y, int num)
{
if (x >=0 && x<= num-1 && y >= 0 && y <= num-1)
return 1;
else
return 0;
}
void CreateGraph(int num)
{
int r;
int c;
int k;
int x[4] = {0,0,-1,1};
int y[4] = {1,-1,0,0};
for(r=0; r< num; r++) {
for(c=0; c<num; c++) {
int l = 0;
for(k=0; k < 4; k++) {
if (IsSafe(r+x[k], c+y[k], num)) {
int val = num*(r+x[k]) + (c+y[k]);
//printf("indexx[%d] indexy[%d] val[%d]\n", r*num+c, l, val);
adjlist[r*num+c][l] = val;
l++;
}
}
}
}
#if 0
/* Print Adjacency List */
for(r=0; r< num*num; r++) {
for(c=0; c< 4; c++) {
printf("[%d]", adjlist[r][c]);
}
printf("\n");
}
#endif
}
int FindMin(int num)
{
int k;
int smallestdist = INF;
int smallestnode = 0;
for(k=0; k < num*num; k++) {
//printf("node [%d] distcance [%d] visited[%d]\n", k, dist[k], visited[k]);
if (dist[k] <= smallestdist && visited[k] == NO) {
smallestdist = dist[k];
smallestnode = k;
}
}
return smallestnode;
}
int distancetodestination(int destination, int num)
{
return inputmat[destination/num][destination%num];
}
void ShortestPath(int num)
{
int k;
int adj;
for(k=0; k < num*num; k++) {
int startnode = FindMin(num);
//printf("Start node[%d]\n", startnode);
visited[startnode] = YES;
for(adj =0; adj <4; adj++) {
if (adjlist[startnode][adj] != -1 && visited[adjlist[startnode][adj]] == NO) {
int distancetoadj = distancetodestination(adjlist[startnode][adj], num);
if (distancetoadj + dist[startnode] < dist[adjlist[startnode][adj]])
dist[adjlist[startnode][adj]] = distancetoadj + dist[startnode];
}
}
}
}
int main()
{
printf("Welcome to Supply route problem..\n");
int testcase;
int T;
scanf("%d", &T);
for(testcase=1; testcase <=T; testcase++) {
int num;
int r;
int c;
int ans;
char ch;
int k;
/*
time_t start, stop;
clock_t ticks; long count;
time(&start);*/
clock_t start, stop;
start = clock();
scanf("%d", &num);
/* Initialize Adjacency List */
for(r=0; r< num*num; r++) {
for(c=0; c<4; c++) {
adjlist[r][c] = -1;
}
}
for(r=0; r< num; r++) {
for(c=0; c< num; c++) {
scanf(" %c", &ch);
inputmat[r][c] = ch - '0';
}
}
#if 0
for(r=0; r< num; r++) {
for(c=0; c< num; c++) {
printf("[%d]", inputmat[r][c]);
}
printf("\n");
}
#endif
CreateGraph(num);
/* Diktra preparation */
for(k=0; k < num*num; k++) {
dist[k] = INF;
visited[k] = NO;
}
dist[0] = 0;
ShortestPath(num);
ans = dist[num*num -1];
printf("#%d %d\n", testcase, ans);
stop = clock();
//time(&stop);
printf("testcase [%d] started %6.3f seconds Finished %6.3f \n", testcase, start, stop /*difftime(stop, start)*/);
}
}