You are not logged in. Please login at www.codechef.com to post your questions!

×

Whats wrong with this code?

I wrote this code to simply read the number of vertices 'n' and edges 'e'. I also read information about the e edges. Each edge is also given a cost. when i run it, it says the error is in line 26. Can some one explain whats the problem ? The code can be found here

Thanks in advance.

asked 20 Apr, 13:21

chari407's gravatar image

3★chari407
912
accept rate: 0%


in the line number 13,, your iterator type has to "vp" , not "vvp" ,,, :)

link

answered 20 Apr, 13:36

drexdelta's gravatar image

5★drexdelta
493
accept rate: 0%

There is one more implementation mistake,,, in the loop when you are taking edges, boundary condition has to be "i < e" , not "i < n" ...

(20 Apr, 13:43) drexdelta5★

Just check this out ... it might help you more ,,,

https://ideone.com/hMWrO5

(20 Apr, 13:46) drexdelta5★

Thanks a lot mate. Can you please explain why the iterator type should be vp and not vvp? I just cant get hold of the reason?

(20 Apr, 16:19) chari4073★

Because , you are iterating through the " vector of pairs(vp)" , not "vector of vector of pair(vvp)" .

In simple words , when you want to travel through edges of node 'i' , you will be travelling through "graph[i].begin()" to "graph[i].end()"[which is just vector of pair]

,,, not the entire "graph"(which is vector of (vector of pair))

(21 Apr, 11:49) drexdelta5★

many errors . you have taken iterator of vector<vector<pair<int,int>>> and g also as vector<vector<pair<int,int>>> . both should be vector<pair<int,int>>::iterator itr and vector<pair<int,int>> g[n] . iteration should be done upto e not n . P.S. :- you can refer to my code for this http://ideone.com/n0CDix

link

answered 20 Apr, 14:08

marshal_roxx's gravatar image

3★marshal_roxx
4156
accept rate: 4%

Well, yes when I tried it this way (vector<pair> thing), it worked fine. But as can be found here - https://www.topcoder.com/community/data-science/data-science-tutorials/power-up-c-with-the-standard-template-library-part-2/#dijkstra1 ... vector<vector<pair<>>> will also work. I might have implemented it wrong and i wanted to know where.

(20 Apr, 16:21) chari4073★

okay you got it the way i did . that's good . as far as getting it done by the way you are trying , iterator has to be for vp not for vvp . and then access the whole vector inside vvp in a separate vp and the iterate with the iterator in the way you did . But why do so much when an easier way is available . Cheers

(20 Apr, 19:58) marshal_roxx3★

How to ask questions? Don't have karma. What to do?

link

answered 20 Apr, 18:29

stevegeek123's gravatar image

0★stevegeek123
462
accept rate: 25%

@stevegeek123 You have to involve yourself in discussions. Answer questions. If your answers are right enough, people vote your answer. Once you get votes, you automatically get karma.

(20 Apr, 18:39) chari4073★

@chari407, thanks for the up vote. I'll try my best.

(20 Apr, 18:42) stevegeek1230★
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Tags:

×768

Asked: 20 Apr, 13:21

Seen: 157 times

Last updated: 21 Apr, 11:49