10740 - Not the Best

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

Moderator: Board moderators

liulike
Learning poster
Posts: 52
Joined: Wed Jul 30, 2003 10:56 am

10740 - Not the Best

Post by liulike » Tue Oct 19, 2004 8:41 am

WA ing

:wink:

Thanks !

sozu
New poster
Posts: 4
Joined: Wed Sep 29, 2004 3:22 pm
Location: Seoul
Contact:

^^

Post by sozu » Tue Oct 19, 2004 10:23 am

I got WA, too.

My solution is BFS with Priority Queue. Is Right? :(

liulike
Learning poster
Posts: 52
Joined: Wed Jul 30, 2003 10:56 am

Post by liulike » Tue Oct 19, 2004 1:11 pm

My input:

Code: Select all

5 6
1 5 2
1 2 1
2 5 10
1 3 1
3 5 10
1 4 1
4 5 10

5 6
1 5 3
1 2 1
2 5 10
1 3 1
3 5 10
1 4 1
4 5 10

5 6
1 5 4
1 2 1
2 5 10
1 3 1
3 5 10
1 4 1
4 5 10

5 6
1 5 5
1 2 1
2 5 10
1 3 1
3 5 10
1 4 1
4 5 10
3 3

1 3 4

1 3 3

1 2 4

2 3 5

5 6

5 2 5

1 2 2

2 5 4

3 2 3

4 3 1

5 1 3

5 4 2

2 2

1 2 3

1 2 5

2 2 2


5 6

5 2 2

1 2 2

2 5 4

3 2 3

4 3 1

5 1 3

5 4 2
5 6

5 2 3

1 2 2

2 5 4

3 2 3

4 3 1

5 1 3

5 4 2
5 6

5 2 4

1 2 2

2 5 4

3 2 3

4 3 1

5 1 3

5 4 2
5 6

5 2 5

1 2 2

2 5 4

3 2 3

4 3 1

5 1 3

5 4 2
5 6

5 2 6

1 2 2

2 5 4

3 2 3

4 3 1

5 1 3

5 4 2
5 6

5 2 7

1 2 2

2 5 4

3 2 3

4 3 1

5 1 3

5 4 2
5 6

5 2 8

1 2 2

2 5 4

3 2 3

4 3 1

5 1 3

5 4 2
5 6

5 2 9

1 2 2

2 5 4

3 2 3

4 3 1

5 1 3

5 4 2
5 6

5 2 10

1 2 2

2 5 4

3 2 3

4 3 1

5 1 3

5 4 2

0 0


My output:

Code: Select all

11
11
-1
-1
-1
15
9
6
14
15
15
16
23
24
24
24
PLZ point out my errors,

Thanks !

..
A great helper
Posts: 454
Joined: Thu Oct 18, 2001 2:00 am
Location: Hong Kong

Post by .. » Tue Oct 19, 2004 3:05 pm

My AC program gives same output.
Have you considered more than one edge from between 2 vertices?
I am not sure if the judge input has such case, but the problem statment doesn't avoid that.
My signature:
  • Please make discussion about the algorithm BRFORE posting source code.
    We can learn much more in discussion than reading source code.
  • I HATE testing account.
  • Don't send me source code for debug.

User avatar
abishek
Experienced poster
Posts: 131
Joined: Mon Dec 15, 2003 5:41 am

Post by abishek » Tue Oct 19, 2004 3:32 pm

how about the case when the weight of the edge is 0. the problem says the weights are >=0;

liulike
Learning poster
Posts: 52
Joined: Wed Jul 30, 2003 10:56 am

Post by liulike » Tue Oct 19, 2004 3:41 pm

.. wrote:My AC program gives same output.
Have you considered more than one edge from between 2 vertices?
I am not sure if the judge input has such case, but the problem statment doesn't avoid that.
The judge input hasn't such case , becase I had checked it :wink:

liulike
Learning poster
Posts: 52
Joined: Wed Jul 30, 2003 10:56 am

Post by liulike » Tue Oct 19, 2004 3:53 pm

abishek wrote:how about the case when the weight of the edge is 0. the problem says the weights are >=0;
what's the speciality of this case?

User avatar
abishek
Experienced poster
Posts: 131
Joined: Mon Dec 15, 2003 5:41 am

Post by abishek » Tue Oct 19, 2004 8:27 pm

there is nothing special about that case. I was just wondering if you had made that assumption some where else in the code.

liulike
Learning poster
Posts: 52
Joined: Wed Jul 30, 2003 10:56 am

Post by liulike » Wed Oct 20, 2004 7:31 am

I changed my algorithm to BFS and got AC now

Thanks !

User avatar
danielrocha
New poster
Posts: 44
Joined: Sun Apr 27, 2003 3:17 am
Location: Rio Grande do Norte - Brazil
Contact:

Help me!

Post by danielrocha » Wed Oct 20, 2004 10:04 pm

I'm using a very naive algorithm: a simple BFS + priority queue (without checking if a node has been visited, always inserting in the queue). However, my algorithm doesn't seem to work. I was getting TLE, but now I'm getting WA. I realized that there can be zero-weighted cycles, which can cause my algo to infinite loop.

I could use some tips, inputs, or some reference to a classic kth shortest path algorithm.
Daniel
UFRN HDD-1
Brasil

sozu
New poster
Posts: 4
Joined: Wed Sep 29, 2004 3:22 pm
Location: Seoul
Contact:

^^

Post by sozu » Thu Oct 21, 2004 5:03 am

TLE...Test Case
4 4
1 4 10
1 2 1
2 3 1
3 1 1
1 4 10000
BFS with Priority Queue can fall in local loop.

User avatar
danielrocha
New poster
Posts: 44
Joined: Sun Apr 27, 2003 3:17 am
Location: Rio Grande do Norte - Brazil
Contact:

Some ideas

Post by danielrocha » Thu Oct 21, 2004 12:20 pm

Hello sozu,

Thank you for the test case. As I said in my previous post, a zero-sized loop could be even worse :)
4 4
1 4 10
1 2 0
2 3 0
3 1 0
1 4 10000
I tried some ideas like only inserting a node K times in the queue, but that didn't work. Could someone give some references to Kth shortest paths algorithms? I couldn't find anything really useful on the web. I would also appreciate some ideas on how to solve this problem (it's not like I want someones code, but just some ideas that could lead me to the right direction :D )
Daniel
UFRN HDD-1
Brasil

User avatar
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey » Thu Oct 21, 2004 12:45 pm

I did a simple and not too fast exhaustive search (dfs) keeping a top-k of distances from the source per node. The break-off condition for the search is when a distance outside the top-k is reached. At the end of the search the answer is in k-th place in the top-k of the target node.
A resonable speed-up (but not spectacular) was made by searching the nodes closest to the current node first (sorting the adjacency list). You can, off course, visit a node more than once during dfs.

BiK
Experienced poster
Posts: 104
Joined: Tue Sep 23, 2003 5:49 pm

Post by BiK » Sat Oct 23, 2004 3:20 pm

Isn't there a reference for the kth shortest path after all?

User avatar
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba » Sat Oct 23, 2004 10:34 pm

What reference do you need? Isn't slightly modified Dijkstra (with counting the k-th shortest path to all the verticles) good enough?

Post Reply

Return to “Volume 107 (10700-10799)”