Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- struct node{
- int cost , x;
- node(){
- }
- node( int a , int b ){
- x = a;
- cost = b;
- }
- bool operator < ( const node& R )
- const{
- if( cost > R.cost )
- return true;
- if( cost == R.cost && R.x < x )
- return true;
- return false;
- }
- };
- vector < node > G[ 1005 ];
- bool visited[ 10005 ];
- int cost[10005];
- void _ucs( node src )
- {
- priority_queue < node > Q;
- Q.push( src );
- visited[ src.x ] = 1;
- cost[ src.x ] = 0;
- while( !Q.empty() )
- {
- node u = Q.top();
- Q.pop();
- cout << u.x << " ( " << u.cost << " ) -> ";
- for( auto it : G[ u.x ] )
- {
- if( !visited[ it.x ] )
- {
- visited[ it.x ] = 1;
- cost[ it.x ] = cost[ u.x ] + it.cost;
- Q.push( { it.x , cost[ it.x ] } );
- }
- }
- // priority_queue < node > T = Q;
- // cout << "QUEUE\n";
- // while( !T.empty() )
- // {
- // cout << T.top().x << " " << T.top().cost << "\n";
- // T.pop();
- // }
- // cout << "---\n";
- }
- }
- int main(){
- freopen("in.txt" , "r" , stdin );
- int node , edge;
- cin >> node >> edge;
- for( int i = 0; i < edge; i++ ){
- int u , v , c;
- cin >> u >> v >> c;
- G[ u ].push_back( { v , c } );
- G[ v ].push_back( { u , c } );
- }
- _ucs( { 1 , 0 } );
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement