#include<iostream>
#include<queue>
using namespace std;
class graph
{
public:
graph(){}
graph(int v)
{
CreateGraph(v);
}
bool CreateGraph(int v)
{
if(v<=0)
return 0;
MyGraph=new int*[v];
for(int i=0;i<v;i++)
{
MyGraph[i]=new int[v];
}
for(int i=0;i<v;i++)
for(int j=0;j<v;j++)
{
MyGraph[i][j]=0;
cout<<MyGraph[i][j];
if(j==v-1)
cout<<endl;
}
for(int i=0;i<v;i++)
{
cout<<“please Enter the vertix1 and Vertix2 and weight”<<endl;
cin>>Vertix1 >>Vertix2 >>Weight;
cout<<Vertix1<<endl<<Vertix2<<endl<<Weight<<endl;
MyGraph[Vertix1-1][Vertix2-1]=Weight;
}
for(int i=0;i<v;i++)
for(int j=0;j<v;j++)
{
//MyGraph[i][j]=0;
cout<<MyGraph[i][j];
if(j==v-1)
cout<<endl;
}
BreadBFS(MyGraph,v);
return 1;
}
void BreadBFS(int **g,int v)
{
queue<int>que;
bool bBreak=false;
for(int i=0;i<v;i++)
{
for(int j=0;j<v;j++)
{
if(MyGraph[i][j]!=0)
{
que.push(j);
//cout<<que.front()<<endl;
}
//bBreak=true;
if(i==3)
{
bBreak=true;
break;
}
}
if(bBreak==true)
break;
}
//cout<<“1111″<<endl;
//cout<<que.size()<<endl;
int popk=0;
while(!que.empty())
{
//cout<<“2222″<<endl;
popk=que.front();
que.pop();
cout<<popk<<endl;
if(popk==0)
break;
for(int i=0;i<v;i++)
{
if(MyGraph[popk][i]!=0)
que.push(i);
}
}
}
private:
int Vertix1;
int Vertix2;
int Weight;
int **MyGraph;
};
int main(int argc,char **argv)
{
graph a(4);
}