Using an adjacent table or vector to implement edge storage and calling the [TEMPLATE]

Source: Internet
Author: User

Save edge;

For the list of neighboring tables implemented by pointers:

Struct edge {int from, next, to, W;} e [maxn]; int head [maxn], TOT = 0; // head is initialized to-1; void add (int x, int y, int Z) {e [++ tot]. from = x; // header node E [tot]. to = y; // connect to the next node E [tot]. W = z; // Edge Weight E [tot]. next = head [X]; // connection pointer head [x] = tot; // pointer pointing to tot, you can use head [x] As the subscript, using e}

For vector:

#include <cstdio>#include <vector>struct edge{int v,w;edge(int _v,int _w){v=_v;w=_w;}};vector <edge> g[maxn];void add(int x,int y,int z){g[x].push_back(edge(y,z));}

 

Specific call:

For example, we need to find all the edges connected by u in spfa;

Adjacent table:

Void use (int x) {int u = x; // X indicates the header node; For (INT I = head [u]; I! =-1; I = E [I]. Next) {v = E [I]. ;}}

Vector:

void use(int x){int ans=g[x].size();for(int i=0;i<ans;i++){int v=g[x][i].v;} }

Note that the vector is saved from 0;

 

In general, vector is relatively easy to use, but it is too weak> <, sometimes STL is not used, so be careful ~~

 

Using an adjacent table or vector to implement edge storage and calling the [TEMPLATE]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.