Блог пользователя deepak2015

Автор deepak2015, 9 лет назад, По-английски

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.

  • Проголосовать: нравится
  • +1
  • Проголосовать: не нравится

»
9 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

»
9 лет назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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