1
+ #include < stdio.h>
2
+ #include < limits.h>
3
+ #include < string.h>
4
+
5
+ #define V 5 // Number of cities
6
+ #define MAX_ITEMS 10 // Maximum number of items for knapsack
7
+ #define MAX_ACTIVITIES 10 // Maximum number of activities
8
+
9
+ // City names
10
+ const char *cities[V] = {" Dhaka" , " Chittagong" , " Khulna" , " Rajshahi" , " Sylhet" };
11
+
12
+ // Function to find the vertex with the minimum distance
13
+ int minDistance (int dist[], int visited[])
14
+ {
15
+ int min = INT_MAX, min_index;
16
+ for (int v = 0 ; v < V; v++)
17
+ {
18
+ if (!visited[v] && dist[v] <= min)
19
+ {
20
+ min = dist[v], min_index = v;
21
+ }
22
+ }
23
+ return min_index;
24
+ }
25
+
26
+ // Dijkstra's algorithm for shortest path
27
+ void dijkstra (int graph[V][V], int src, int dest)
28
+ {
29
+ int dist[V];
30
+ int visited[V] = {0 };
31
+
32
+ for (int i = 0 ; i < V; i++)
33
+ {
34
+ dist[i] = INT_MAX;
35
+ }
36
+ dist[src] = 0 ;
37
+
38
+ for (int count = 0 ; count < V - 1 ; count++)
39
+ {
40
+ int u = minDistance (dist, visited);
41
+ visited[u] = 1 ;
42
+
43
+ for (int v = 0 ; v < V; v++)
44
+ {
45
+ if (!visited[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v])
46
+ {
47
+ dist[v] = dist[u] + graph[u][v];
48
+ }
49
+ }
50
+ }
51
+
52
+ printf (" Shortest distance from %s to %s: %d\n " , cities[src], cities[dest], dist[dest]);
53
+ }
54
+
55
+ int getCityIndex (char city[])
56
+ {
57
+ for (int i = 0 ; i < V; i++)
58
+ {
59
+ if (strcmp (city, cities[i]) == 0 )
60
+ {
61
+ return i;
62
+ }
63
+ }
64
+ return -1 ;
65
+ }
66
+
67
+ // 0/1 Knapsack for resource optimization
68
+ int knapsack (int W, int wt[], int val[], int n)
69
+ {
70
+ int dp[n + 1 ][W + 1 ];
71
+ for (int i = 0 ; i <= n; i++)
72
+ {
73
+ for (int w = 0 ; w <= W; w++)
74
+ {
75
+ if (i == 0 || w == 0 )
76
+ dp[i][w] = 0 ;
77
+ else if (wt[i - 1 ] <= w)
78
+ dp[i][w] = (val[i - 1 ] + dp[i - 1 ][w - wt[i - 1 ]] > dp[i - 1 ][w]) ? (val[i - 1 ] + dp[i - 1 ][w - wt[i - 1 ]]) : dp[i - 1 ][w];
79
+ else
80
+ dp[i][w] = dp[i - 1 ][w];
81
+ }
82
+ }
83
+ return dp[n][W];
84
+ }
85
+ // Sorts activities by finish time (ascending)
86
+ void sortActivities (int start[], int finish[], int n)
87
+ {
88
+ // Bubble sort implementation
89
+ for (int i = 0 ; i < n - 1 ; i++)
90
+ {
91
+ for (int j = 0 ; j < n - i - 1 ; j++)
92
+ {
93
+ if (finish[j] > finish[j + 1 ])
94
+ {
95
+ // Swap finish times
96
+ int temp = finish[j];
97
+ finish[j] = finish[j + 1 ];
98
+ finish[j + 1 ] = temp;
99
+
100
+ // Swap corresponding start times
101
+ temp = start[j];
102
+ start[j] = start[j + 1 ];
103
+ start[j + 1 ] = temp;
104
+ }
105
+ }
106
+ }
107
+ }
108
+
109
+ // Activity Selection for scheduling tasks
110
+ void activitySelection (int start[], int finish[], int n)
111
+ {
112
+ int i, j;
113
+ printf (" You can complete the following activities without overlapping:\n " );
114
+ i = 0 ;
115
+ printf (" Activity %d\n " , i + 1 );
116
+ for (j = 1 ; j < n; j++)
117
+ {
118
+ if (start[j] >= finish[i])
119
+ {
120
+ printf (" Activity %d\n " , j + 1 );
121
+ i = j;
122
+ }
123
+ }
124
+ printf (" \n " );
125
+ }
126
+
127
+ int main ()
128
+ {
129
+ int graph[V][V] = {
130
+ {0 , 200 , 300 , 0 , 0 },
131
+ {200 , 0 , 150 , 400 , 0 },
132
+ {300 , 150 , 0 , 250 , 350 },
133
+ {0 , 400 , 250 , 0 , 500 },
134
+ {0 , 0 , 350 , 500 , 0 }};
135
+
136
+ char source[20 ], destination[20 ];
137
+ printf (" Available cities: Dhaka, Chittagong, Khulna, Rajshahi, Sylhet\n " );
138
+ printf (" Enter your current city: " );
139
+ scanf (" %s" , source);
140
+ printf (" Enter your destination city: " );
141
+ scanf (" %s" , destination);
142
+
143
+ int src = getCityIndex (source);
144
+ int dest = getCityIndex (destination);
145
+
146
+ if (src == -1 || dest == -1 )
147
+ {
148
+ printf (" Invalid city name. Please enter a valid city from the list.\n " );
149
+ return 1 ;
150
+ }
151
+
152
+ dijkstra (graph, src, dest);
153
+
154
+ printf (" \n Smart Packing System:\n " );
155
+ printf (" Optimize the items you can carry in your trip.\n " );
156
+ int n, W;
157
+ printf (" Enter the number of items you want to carry: " );
158
+ scanf (" %d" , &n);
159
+ int val[n], wt[n];
160
+ printf (" Enter the value and weight of each item:\n " );
161
+ for (int i = 0 ; i < n; i++)
162
+ {
163
+ scanf (" %d %d" , &val[i], &wt[i]);
164
+ }
165
+ printf (" Enter your bag's maximum weight capacity: " );
166
+ scanf (" %d" , &W);
167
+ printf (" Optimal value of items to carry: %d\n " , knapsack (W, wt, val, n));
168
+
169
+ printf (" \n Smart Activity Scheduler:\n " );
170
+ printf (" Plan your activities efficiently to maximize your time.\n " );
171
+ int num_activities;
172
+ printf (" Enter the number of activities planned: " );
173
+ scanf (" %d" , &num_activities);
174
+ int start[num_activities], finish[num_activities];
175
+ // Inside main():
176
+ printf (" Enter the start and finish times of each activity:\n " );
177
+ for (int i = 0 ; i < num_activities; i++)
178
+ {
179
+ scanf (" %d %d" , &start[i], &finish[i]);
180
+ }
181
+
182
+ // Sort activities by finish time before selection
183
+ sortActivities (start, finish, num_activities);
184
+
185
+ activitySelection (start, finish, num_activities);
186
+
187
+ return 0 ;
188
+ }
0 commit comments