xxxxxxxxxx
#include <iostream>
using namespace std;
int findMinVertex(int* distance, bool* visited, int n)
{
int minVertex=-1;
for(int i=0; i<n; i++)
{
if(!visited[i] && (minVertex==-1 || distance[i]< distance[minVertex]))
{
minVertex=i;
}
}
return minVertex;
}
void disjkstra(int** edges, int n)
{
bool *visited=new bool[n];
int *distance= new int[n];
for(int i=0; i<n; i++)
{
visited[i]=false;
distance[i]=INT_MAX;
}
distance[0]=0;
for(int i=0; i<n-1; i++)
{
int minVertex=findMinVertex(distance, visited, n);
visited[minVertex]=true;
for(int j=0; j<n; j++)
{
if(edges[minVertex][j]!=0 && !visited[j])
{
int dist=distance[minVertex]+edges[minVertex][j];
if(dist<distance[j])
{
distance[j]=dist;
}
}
}
}
for(int i=0; i<n; i++)
{
cout<<i<<" -> "<<distance[i]<<"\n";
}
delete[] visited;
delete[] distance;
}
int main()
{
int n,e;
cin>>n;
cin>>e;
int **edges= new int*[n];
for(int i=0; i<n; i++)
{
edges[i]=new int[n];
for(int j=0; j<n; j++)
{
edges[i][j]=0;
}
}
for(int i=0; i<e; i++)
{
int f,s,weight;
cin>>f>>s>>weight;
edges[f][s]=weight;
edges[s][f]=weight;
}
cout<<endl;
disjkstra(edges,n);
for(int i=0; i<n; i++)
{
delete[] edges[i];
}
delete[] edges;
return 0;
/*
5 7
0 1 4
0 2 8
1 3 5
1 2 2
2 3 5
2 4 9
3 4 4
*/
}
xxxxxxxxxx
#include<iostream>
#include<climits>
using namespace std;
int miniDist(int distance[], bool Tset[]) // finding minimum distance
{
int minimum=INT_MAX,ind;
for(int k=0;k<6;k++)
{
if(Tset[k]==false && distance[k]<=minimum)
{
minimum=distance[k];
ind=k;
}
}
return ind;
}
void DijkstraAlgo(int graph[6][6],int src) // adjacency matrix
{
int distance[6]; // // array to calculate the minimum distance for each node
bool Tset[6];// boolean array to mark visited and unvisited for each node
for(int k = 0; k<6; k++)
{
distance[k] = INT_MAX;
Tset[k] = false;
}
distance[src] = 0; // Source vertex distance is set 0
for(int k = 0; k<6; k++)
{
int m=miniDist(distance,Tset);
Tset[m]=true;
for(int k = 0; k<6; k++)
{
// updating the distance of neighbouring vertex
if(!Tset[k] && graph[m][k] && distance[m]!=INT_MAX && distance[m]+graph[m][k]<distance[k])
distance[k]=distance[m]+graph[m][k];
}
}
cout<<"Vertex\t\tDistance from source vertex"<<endl;
for(int k = 0; k<6; k++)
{
char str=65+k;
cout<<str<<"\t\t\t"<<distance[k]<<endl;
}
}
int main()
{
int graph[6][6]={
{0, 1, 2, 0, 0, 0},
{1, 0, 0, 5, 1, 0},
{2, 0, 0, 2, 3, 0},
{0, 5, 2, 0, 2, 2},
{0, 1, 3, 2, 0, 1},
{0, 0, 0, 2, 1, 0}};
DijkstraAlgo(graph,0);
return 0;
}
xxxxxxxxxx
#include <bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define ll long long
#define endl '\n'
int DX[]= {0,0,1,-1};
int DY[]= {1,-1,0,0};
bool vis[105][105][2] {false};
const int N=1e5+10;
int dis[N];
vector<pair<int,int>>adj[N];
void init()
{
for(int i=0; i<N; i++)
{
dis[i]=1e9;
}
}
void Dijkstra(int src) // e log v
{
dis[src]=0;
//dis,node
priority_queue<pair<int,int>>Q;
Q.push({-dis[src],src});
while(Q.size())
{
// AUTO take best candidate
int u=Q.top().second;
int d=-Q.top().first;
Q.pop();
// cout<<u<<endl;
if(d!=dis[u])
{
continue;
}
// cout<<"pass "<<u<<endl;
// relaxation
for( auto x: adj[u])
{
// cout<<x.first<<" ";
int v=x.first;
int c=x.second;
int newCost=d+c;
// cout<<newCost<<" "<<dis[v]<<endl;
if(newCost<dis[v])
{
dis[v]=newCost;
Q.push({-newCost,v});
}
}
// cout<<endl;
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n,m,src,dist;
cin>>n>>m>>src>>dist;
for(int i=0;i<m;i++)
{
int f,t,c;
cin>>f>>t>>c;
adj[f].push_back({t,c});
adj[t].push_back({f,c});
}
init();
Dijkstra(src);
if(dis[dist]==1e9)
{
cout<<"NONE"<<endl;
}
else
{
cout<<dis[dist]<<endl;
}
}
}