/***
Md. Namzul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size) for(int i=0 ; i<size ; i++) cin>>array[i] ;
#define new_line pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647
int node, edge ;
vector<int>G[10001] ;
int visited[10001] ;
int dis[10001] ; /// This array will help to find distance from source node to destination node
int src, des ;
int BFS()
{
queue<int> q ;
memset(visited,0,sizeof(visited)) ;
memset(dis,0,sizeof(dis)) ;
q.push(src) ;
visited[src] =1 ;
dis[src] =0 ;
while(!q.empty())
{
int u = q.front() ;
q.pop() ;
for(int i=0 ; i<G[u].size(); i++)
{
int v = G[u][i] ;
if(!visited[v])
{
visited[v] =1 ;
dis[v] = dis[u] +1 ;
q.push(v) ;
if(v==des)
return dis[v] ;
}
}
}
}
int main()
{
sc("%d %d",&node,&edge) ;
for(int i=1 ; i<=edge ; i++)
{
int u ,v ;
sc("%d %d",&u,&v) ;
G[u].pb(v) ;
G[v].pb(u) ;
}
sc("%d %d",&src,&des) ;
int m = BFS() ;
if(m<0)
pf("Sorry the destination is not in the graph") ;
else
pf("Minimum distance from %d to %d is %d",src,des,m) ;
return 0;
}
Md. Namzul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size) for(int i=0 ; i<size ; i++) cin>>array[i] ;
#define new_line pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647
int node, edge ;
vector<int>G[10001] ;
int visited[10001] ;
int dis[10001] ; /// This array will help to find distance from source node to destination node
int src, des ;
int BFS()
{
queue<int> q ;
memset(visited,0,sizeof(visited)) ;
memset(dis,0,sizeof(dis)) ;
q.push(src) ;
visited[src] =1 ;
dis[src] =0 ;
while(!q.empty())
{
int u = q.front() ;
q.pop() ;
for(int i=0 ; i<G[u].size(); i++)
{
int v = G[u][i] ;
if(!visited[v])
{
visited[v] =1 ;
dis[v] = dis[u] +1 ;
q.push(v) ;
if(v==des)
return dis[v] ;
}
}
}
}
int main()
{
sc("%d %d",&node,&edge) ;
for(int i=1 ; i<=edge ; i++)
{
int u ,v ;
sc("%d %d",&u,&v) ;
G[u].pb(v) ;
G[v].pb(u) ;
}
sc("%d %d",&src,&des) ;
int m = BFS() ;
if(m<0)
pf("Sorry the destination is not in the graph") ;
else
pf("Minimum distance from %d to %d is %d",src,des,m) ;
return 0;
}
No comments:
Post a Comment