10543 - Traveling Politician

All about problems in Volume 105. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 »

I used two queues, both of size 52. I don't know why you need one that's much bigger than that.
shamim
A great helper
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Bozeman, Montana, USA

Post by shamim »

I used two queues, both of size 52. I don't know why you need one that's much bigger than that.
One thing I don't understand is, if a small size queue works, then why shouldn't a bigger one.

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. :-?
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 »

It depends on how you're using your queue.. maybe queue is the wrong term for what I did.. a better term would be having used two min cost arrays.. one for current depth, one for next depth..
Rajib
New poster
Posts: 28
Joined: Tue Nov 04, 2003 6:45 am
Location: Bangladesh

Post by Rajib »

I don't understand how you use two '52 size' Que to solve the problem :o . It can happen that there is a loop in the graph and after traversing the loop for savarel times we can reach the destination city.

Can you explain how you use those two Que.
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 »

After some number of speeches my array holds a 1 if the city can be reached, and a 0 if it cannot, and thus can traverse from there. You only need to know whether a city can be reached w/ some number of speeches before it.
Abednego
A great helper
Posts: 281
Joined: Tue Sep 10, 2002 5:14 am
Location: Mountain View, CA, USA
Contact:

Post by Abednego »

I don't think there is a bug like that in the judge's solution. ;-) It doesn't use any queues or BFS at all.- just 3 arrays of size 50x50.
If only I had as much free time as I did in college...
anupam
A great helper
Posts: 405
Joined: Wed Aug 28, 2002 6:45 pm
Contact:

Post by anupam »

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.
"Everything should be made simple, but not always simpler"
anupam
A great helper
Posts: 405
Joined: Wed Aug 28, 2002 6:45 pm
Contact:

Post by anupam »

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.
"Everything should be made simple, but not always simpler"
Nemo
New poster
Posts: 16
Joined: Thu Apr 14, 2005 10:40 am

Post by Nemo »

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);
	}

}
Nemo
New poster
Posts: 16
Joined: Thu Apr 14, 2005 10:40 am

10543 - Travelling politician

Post by Nemo »

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); 
   } 

} 
jurajz
Learning poster
Posts: 69
Joined: Sat Sep 02, 2006 7:30 pm
Location: Slovakia

Post by jurajz »

Thank you for this thread, little joey!

I am Pascal programmer, and I had many WA's. I wrote the same code from Pascal to C and have AC, with the first submit.
Post Reply

Return to “Volume 105 (10500-10599)”