Cod sursa(job #625188)

Utilizator thesilverhand13FII Florea Toma Eduard thesilverhand13 Data 23 octombrie 2011 23:09:39
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.46 kb
 # include <fstream>
 # include <vector>
 # include <queue>
 
 # define dim 102
 # define inf 999999
 # define pb push_back
 
 using namespace std;
 
 
 ifstream f("royfloyd.in");
 ofstream g("royfloyd.out");
 
 struct belman
 {
	 int nod,cost;
 };
 
 vector < belman > a[ dim ];
 queue < int > q;
 
 int c[ dim ][ dim ];
 int n;
 
 void citire()
 {
	 int i, j, x;
	 f >> n;
	 for ( i = 1 ; i <= n ; i++ )
		 for ( j = 1 ; j <= n ; j++ )
		 {
			 f >> x;
			 if ( x != 0  )
			 
				 a[ i ].pb( ( belman ) { j, x  } ); 
				 c[ i ][ j ] = inf;
			 
		 }
 }
 
 void dijkstra( int x )
 {
	 int i, xx;
	 
	 c[ x ][ x ] = 0;
	 q.push( x );
	 
	 while ( !q.empty() )
	 {
		 xx = q.front();
		 for ( i = 0 ; i < a[ xx ].size() ; i++ )
			 if ( c[ x ][ a[ xx ][ i ].nod ] > c[ x ][ xx ] + a[ xx ][ i ].cost )
			 {
				  c[ x ][ a[ xx ][ i ].nod ] = c[ x ][ xx ] + a[ xx ][ i ].cost;
				  q.push( a[ xx ][ i ].nod );
			 }
			 q.pop();
	 }
	 
 }
 
 void rezolva()
 {
	 int i;
	 for ( i = 1 ; i <= n ; i++ )
		 dijkstra( i );
 }
 
 void afisare()
 {
	 int i, j;
	 for ( i = 1 ; i <= n ; i++ )
	 {
		 for ( j = 1 ; j <= n ; j++ )
			 g << c[ i ][ j ] << " ";
		 g << "\n";
	 }
	 
	/* g << "\n";
	 for ( i = 1 ; i <= n ; i++ )
	 {
		 for ( j = 0 ; j < a[ i ].size() ; j++ )
			 g << a[ i ][ j ].nod << " " ;
		 g << "\n" ;
	 }
	 */
 }
 
 int main()
 {
	 citire();
	 rezolva();
	 afisare();
	 return 0;
 }