-
Notifications
You must be signed in to change notification settings - Fork 9
/
Deliver from Farm to Shops.c
66 lines (64 loc) · 1.38 KB
/
Deliver from Farm to Shops.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
#include<stdio.h>
#include <stdlib.h>
int main()
{
int i, j, k, N, dist[100], count=0;
char start[100][100], dest[100][100], shop[100][100], temp[100];
scanf("%d", &N);
for(i=0;i<N;i++)
scanf("%s %s", start[i], dest[i]);
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
if(strcmp(dest[i], start[j])==0)
break;
if(j==N)
strcpy(shop[count++], dest[i]);
}
for(i=0, k=0;i<count;i++, k=0)
{
strcpy(temp, shop[i]);
while(strcmp(start[0], temp)!=0)
for(j=0, k++;j<N;j++)
if(strcmp(temp, dest[j])==0)
{
strcpy(temp, start[j]);
break;
}
dist[i]=k;
}
for(i=0;i<count;i++)
{
for(j=i+1;j<count;j++)
{
if(dist[i]>dist[j])
{
k=dist[i];
dist[i]=dist[j];
dist[j]=k;
strcpy(temp, shop[i]);
strcpy(shop[i], shop[j]);
strcpy(shop[j], temp);
}
else if(strcmp(shop[i], shop[j])==0)
strcpy(shop[j], " ");
}
if(strlen(shop[i])!=0)
printf("%s\n", shop[i]);
}
}
example:
input:
8
a b
a c
a d
c e
e f
d h
f g
b f
ouyput:
h g
h is visited through the route a->d->h
g is visited through route a->b->f->g