-
Notifications
You must be signed in to change notification settings - Fork 1
/
ambeasilhas.c
222 lines (199 loc) · 4.2 KB
/
ambeasilhas.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <stdio.h>
#include <stdlib.h>
#define MAX 10000000
typedef struct node
{
int item;
struct node *next;
} NODE;
typedef struct queue
{
NODE *head;
NODE *tail;
int size;
} QUEUE;
typedef struct adj_list
{
NODE *front;
} ADJ_LIST;
typedef struct graph
{
ADJ_LIST *vertices[MAX];
int visited[MAX];
int distancy[MAX];
} GRAPH;
NODE *createNode(int item)
{
NODE *newNode = (NODE *)malloc(sizeof(NODE));
newNode->item = item;
newNode->next = NULL;
return newNode;
}
ADJ_LIST *create_adj_list(int item)
{
ADJ_LIST *new = (ADJ_LIST *)malloc(sizeof(ADJ_LIST));
new->front = (NODE *)malloc(sizeof(NODE));
new->front->item = item;
new->front->next = NULL;
return new;
}
QUEUE *create_queue()
{
QUEUE *new = (QUEUE *)malloc(sizeof(QUEUE));
new->head = NULL;
new->tail = NULL;
new->size = 0;
return new;
}
GRAPH *create_graph()
{
GRAPH *new = (GRAPH *)malloc(sizeof(GRAPH));
int i;
for (i = 1; i < MAX - 1; i++)
{
new->vertices[i] = NULL;
new->visited[i] = 0;
new->distancy[i] = 0;
}
return new;
}
void enqueue(QUEUE *queue, int item)
{
NODE *newNode = createNode(item);
if (queue->size == 0)
{
queue->head = newNode;
queue->tail = newNode;
queue->size++;
return;
}
queue->tail->next = newNode;
queue->tail = newNode;
queue->size++;
return;
}
int dequeue(QUEUE *queue)
{
if (!queue->size)
{
printf("0 elementos\n");
return 0;
}
if (queue->size == 1)
{
queue->size--;
NODE *a = queue->head;
queue->head = NULL;
queue->tail = NULL;
int value = a->item;
free(a);
return value;
}
NODE *aux = queue->head;
int value = aux->item;
queue->head = aux->next;
queue->size--;
free(aux);
return value;
}
int is_empty(QUEUE *queue)
{
return (queue->size == 0);
}
//directed
void add_edge(GRAPH *graph, int v1, int v2)
{
if (graph->vertices[v1] == NULL)
{
graph->vertices[v1] = create_adj_list(v2);
}
else
{
NODE *new = createNode(v2);
NODE *new2 = graph->vertices[v1]->front;
new->next = graph->vertices[v1]->front;
graph->vertices[v1]->front = new;
// graph->distancy[v1]++;
}
}
int bfs(GRAPH *graph, int origem, int destino)
{
QUEUE *queue = create_queue();
int dequeued, count = 0;
NODE *node = graph->vertices[origem]->front;
graph->visited[origem] = 1;
enqueue(queue, origem);
while (!is_empty(queue))
{
dequeued = dequeue(queue);
if (graph->vertices[dequeued] == NULL)
{
continue;
}
node = graph->vertices[dequeued]->front;
while (node != NULL)
{
if (graph->visited[node->item] == 0)
{
enqueue(queue, node->item);
graph->visited[node->item] = 1;
graph->distancy[node->item] = graph->distancy[dequeued] + 1;
}
node = node->next;
}
}
return graph->distancy[destino];
}
void printGraph(GRAPH *graph)
{
int i;
NODE *aux;
for (i = 1; i <= MAX - 1; i++)
{
if (graph->vertices[i] == NULL)
{
continue;
}
aux = graph->vertices[i]->front;
printf("Vertex: %d: ", i);
while (aux != NULL)
{
printf("%d => ", aux->item);
aux = aux->next;
}
printf("\n");
}
}
void add_undirected(GRAPH *graph, int v1, int v2)
{
add_edge(graph, v1, v2);
add_edge(graph, v2, v1);
}
int main()
{
int caso_teste, n, m, x, y, i, j, k;
scanf("%d", &caso_teste);
int ok;
for (i = 0; i < caso_teste; i++)
{
GRAPH *graph = create_graph();
scanf("%d%d", &n, &m);
for (j = 0; j < m; j++)
{
scanf("%d%d", &x, &y);
add_undirected(graph, x, y);
}
// printf("\n");
ok = bfs(graph, 1, n);
if (ok >= 1)
{
printf("%d\n", ok);
}
else
{
printf("-1\n");
}
// printf("ok = %d\n",ok);
// printGraph(graph);
}
}