Skip to content

Instantly share code, notes, and snippets.

@growlnx
Last active February 17, 2018 20:48
Show Gist options
  • Select an option

  • Save growlnx/339f5800f402832f52fe51110964a79f to your computer and use it in GitHub Desktop.

Select an option

Save growlnx/339f5800f402832f52fe51110964a79f to your computer and use it in GitHub Desktop.
#include "iostream"
#include "vector"
using namespace std;
class Grafo{
private:
// matrix de adjacencia
vector<vector<int>>matrixAdj;
public:
// constroi uma matrix quadrada nula
Grafo(unsigned quantVertice);
// adiciona uma aresta ao grafo
void set(unsigned x,unsigned y,int valor);
// pega o valor de uma aresta
int get(unsigned x,unsigned y);
};
Grafo::Grafo(unsigned quantVertice){
for(unsigned cont=0;cont<quantVertice;cont++)
this->matrixAdj.push_back(vector<int>(quantVertice,0));
}
void Grafo::set(unsigned x,unsigned y,int valor){
this->matrixAdj[x][y]=valor;
}
int Grafo::get(unsigned x,unsigned y){
return this->matrixAdj[x][y];
}
int main(){
// criando o grafo
Grafo grafo(4);
// adicionando arestas
grafo.set(0,1,10);
grafo.set(1,2,20);
grafo.set(2,3,30);
// pegando valores das arestas
cout<<grafo.get(0,1)<<endl;
cout<<grafo.get(1,2)<<endl;
cout<<grafo.get(2,3)<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment