deepak2015's blog

By deepak2015, 11 years ago, In English

vector<pair<int, int> > adj[105];

I am assigning the adjacent vertex of u : v and the weight of edge uv : w by adj[u].push_back(make_pair(v,w)). Now how to access the elements( first and second of pair). Plz expain this in detail as I did not get much useful on net.

  • Vote: I like it
  • +1
  • Vote: I do not like it

| Write comment?
»
11 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

adj[u].first; adj[u].second;

»
11 years ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

Here, adj[u] is a vector of pairs.

So to get weight of an edge uv, you need to iterate through this vector, find v and get the weight.

for(typeof(adj[u].begin()) it = adj[u].begin(); it!=adj[u].end(); it++){
    if(it->first==v){required =  it->second;}
}