10543 - Traveling Politician
Moderator: Board moderators
One thing I don't understand is, if a small size queue works, then why shouldn't a bigger one.I used two queues, both of size 52. I don't know why you need one that's much bigger than that.
I remember solving this problem using STL(C++) queue, I got Memory Limit Exceeded. I couldn't understand why. Now STL queue dynamically sizes it self, so the queue should never get big enough to cause MLE.
Later I used a circular queue of MAX size 1000000 and I got AC.

I have solved the problem withuot using any queue or bfs at all.
I used three arrays.
Let the adj matrix is a[1..N][1..N] where a[j]=1 if it is given in input otherwise it is 0.
Then
A^n=A^(n-1)*A given you the a matrix where each entry denotes the number of way to reach the destination from the source with n length path.
SO check for only such entry.
I used three arrays.
Let the adj matrix is a[1..N][1..N] where a[j]=1 if it is given in input otherwise it is 0.
Then
A^n=A^(n-1)*A given you the a matrix where each entry denotes the number of way to reach the destination from the source with n length path.
SO check for only such entry.
"Everything should be made simple, but not always simpler"
I have solved the problem withuot using any queue or bfs at all.
I used three arrays.
Let the adj matrix is a[1..N][1..N] where a[j]=1 if it is given in input otherwise it is 0.
Then
A^n=A^(n-1)*A given you the a matrix where each entry denotes the number of way to reach the destination from the source with n length path.
SO check for only such entry.
I used three arrays.
Let the adj matrix is a[1..N][1..N] where a[j]=1 if it is given in input otherwise it is 0.
Then
A^n=A^(n-1)*A given you the a matrix where each entry denotes the number of way to reach the destination from the source with n length path.
SO check for only such entry.
"Everything should be made simple, but not always simpler"
what's wrong with this code? gets WA.....
Code: Select all
#include <iostream.h>
#include <stdio.h>
#include <math.h>
void main()
{
int n, m, k, u, v, p, q, r, i;
scanf(" %d %d %d ", &n, &m, &k);
while (n || m || k) {
int g[50][50] = {0};
int temp1[50][50] = {0};
int temp2[50][50] = {0};
for (i = 0; i < m ; i++) {
scanf(" %d %d ", &u, &v);
g[u][v]++;
temp1[u][v]++;
}
if (k == 2 && g[0][n - 1]) {
printf("2\n");
scanf(" %d %d %d ", &n, &m, &k);
continue;
}
for (i = 2; i < 20 ; i++) {
for (p = 0; p < n ; p++)
for (q = 0; q < n ; q++)
if (i % 2 == 0) {
temp2[p][q] = 0;
for (r = 0; r < n ; r++)
temp2[p][q] += temp1[p][r] * g[r][q];
}
else {
temp1[p][q] = 0;
for (r = 0; r < n ; r++)
temp1[p][q] += temp2[p][r] * g[r][q];
}
if (i % 2 == 0)
if (temp2[0][n - 1] && i >= k - 1)
break;
else
if (temp1[0][n - 1] && i >= k - 1)
break;
}
if (i == 20)
printf("%s\n", "LOSER");
else
if (i % 2 == 0)
if (temp2[0][n - 1] && i >= k - 1)
printf("%d\n", i + 1);
else
printf("%s\n", "LOSER");
else
if (temp1[0][n - 1] && i >= k - 1)
printf("%d\n", i + 1);
else
printf("%s\n", "LOSER");
scanf(" %d %d %d ", &n, &m, &k);
}
}
10543 - Travelling politician
What's wrong with this? gets WA.......
Code: Select all
#include <iostream.h>
#include <stdio.h>
#include <math.h>
void main()
{
int n, m, k, u, v, p, q, r, i;
scanf(" %d %d %d ", &n, &m, &k);
while (n || m || k) {
int g[50][50] = {0};
int temp1[50][50] = {0};
int temp2[50][50] = {0};
for (i = 0; i < m ; i++) {
scanf(" %d %d ", &u, &v);
g[u][v]++;
temp1[u][v]++;
}
if (k == 2 && g[0][n - 1]) {
printf("2\n");
scanf(" %d %d %d ", &n, &m, &k);
continue;
}
for (i = 2; i < 20 ; i++) {
for (p = 0; p < n ; p++)
for (q = 0; q < n ; q++)
if (i % 2 == 0) {
temp2[p][q] = 0;
for (r = 0; r < n ; r++)
temp2[p][q] += temp1[p][r] * g[r][q];
}
else {
temp1[p][q] = 0;
for (r = 0; r < n ; r++)
temp1[p][q] += temp2[p][r] * g[r][q];
}
if (i % 2 == 0)
if (temp2[0][n - 1] && i >= k - 1)
break;
else
if (temp1[0][n - 1] && i >= k - 1)
break;
}
if (i == 20)
printf("%s\n", "LOSER");
else
if (i % 2 == 0)
if (temp2[0][n - 1] && i >= k - 1)
printf("%d\n", i + 1);
else
printf("%s\n", "LOSER");
else
if (temp1[0][n - 1] && i >= k - 1)
printf("%d\n", i + 1);
else
printf("%s\n", "LOSER");
scanf(" %d %d %d ", &n, &m, &k);
}
}