deepak2015's blog

By deepak2015, 10 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?
»
10 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    10 years ago, # ^ |
    Rev. 2   Vote: I like it -11 Vote: I do not like it

    Deleted

    • »
      »
      »
      10 years ago, # ^ |
      Rev. 3   Vote: I like it +5 Vote: I do not like it

      because adj[u] itself is a vector of pairs so this must work adj[u][v].first and adj[u][v].second

      Note that:vector<pair<int,int> > v[100] is like saying vector<vector<pair<int,int> > > v(100);

      • »
        »
        »
        »
        10 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Can u plz elaborate as adj[u][v].first is giving me garbage value. Can u tell me what adj[u][v].first and adj[u][v].second should produce according to the input pattern specified in my blog.

        • »
          »
          »
          »
          »
          10 years ago, # ^ |
            Vote: I like it +1 Vote: I do not like it

          adj[u][v].first is the (v+1)th (zero-based) vertex connected to u.And adj[u][v].second is the weight of the edge uv.

    • »
      »
      »
      10 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it
»
10 years ago, # |
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;}
}