Advertisement
iamyeasin

Dijkstra

May 1st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.81 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define     sf      scanf
  3. #define     pf      printf
  4. #define     inf     INT_MAX
  5. #define     dbg     cout << "Debug" << endl;
  6.  
  7. using namespace std;
  8.  
  9. void clr();
  10. void printGrid();
  11.  
  12. vector < int > adjList[1024];
  13. vector < int > ans;
  14. priority_queue  < int > q;
  15.  
  16. int par[1234];
  17.  
  18. int x1,x2,nodes,start,edges,n,a,b,c,last ;
  19. double times;
  20. int visited[1025] ,d[1024];
  21. int cost[1024][1024];
  22.  
  23. void BFS( int s ){ /// Implementation of Dijsktra Algorithm
  24.  
  25.     for( int i=0; i<=nodes; i++ ) d[i] = inf;
  26.     q.push( s );
  27.     d[s] = 0;
  28.  
  29.     while ( !q.empty() ){
  30.         int u = q.top(); q.pop();
  31.         int ucost = d[u];
  32.         int sz = adjList[u].size();
  33.  
  34.         for( int i=0; i<sz; i++ ){
  35.             int v = adjList[u][i];
  36.             int vcost = ucost + cost[u][v];
  37.  
  38.             if( vcost < d[v] ){
  39.                 d[v] = vcost;
  40.                 par[v] = u;
  41.                 q.push( v );
  42.             }
  43.  
  44.         }
  45.     }
  46. }
  47.  
  48. void solve(){
  49.     x1 = x2 = inf;
  50.     int mx = INT_MIN;
  51.     for( int i=1; i<=nodes; i++ ){
  52.         mx = max( d[i], mx );
  53.     }
  54.  
  55.     for( int u=2; u<=nodes; u++ ){
  56.         int sz = adjList[u].size();
  57.         for( int j=0; j<sz; j++ ){
  58.             int v = adjList[u][j];
  59.             if( par[u] != v && ((d[v]) < d[u] + cost[v][u]) ){
  60.                 cout << u << " " << v << " " << cost[v][u] << endl;
  61.             }
  62.         }
  63.     }
  64. }
  65.  
  66.  
  67. int main(){
  68.     #ifndef ONLINE_JUDGE
  69.         freopen("in.txt","rt",stdin);
  70.         freopen("outs.txt","wt",stdout);
  71.     #endif
  72.     clr();
  73.     int kase = 0;
  74.  
  75.     while( sf ("%d %d",&nodes, &edges) ,nodes + edges ){
  76.         for( int i=1; i<=edges; i++ ){
  77.             sf("%d %d %d",&a, &b, &c);
  78.             adjList[a].push_back( b );
  79.             adjList[b].push_back( a );
  80.             cost[a][b] = cost[b][a] = c;
  81.         }
  82.  
  83.         BFS(1);
  84.         solve();
  85.         puts("");
  86.        
  87.         clr();
  88.     }
  89.  
  90.  
  91.     return 0;
  92. }
  93.  
  94. void clr(){
  95.     for( int i=0; i<=nodes; i++ ){
  96.         adjList[i].clear();
  97.         visited[i] = 0;
  98.     }
  99.     memset( cost, 0, sizeof cost );
  100.     for( int i=0; i<=nodes; i++ )d[i] = inf;
  101.  
  102. }
  103.  
  104. void printGrid(){
  105.     for( int i=1; i<=nodes; i++ ){
  106.         for( int j=1; j<=nodes; j++ ){
  107.             printf("%d ", cost[i][j] );
  108.         }
  109.         puts("");
  110.     }
  111.  
  112. }
  113.  
  114. /*
  115. 3 3
  116. 1 2 1
  117. 1 3 2
  118. 2 3 8
  119.  
  120. 3 3
  121. 1 2 1
  122. 1 3 2
  123. 2 3 13
  124.  
  125. 3 3
  126. 1 2 5
  127. 1 3 10
  128. 2 3 5
  129.  
  130.  
  131. 3 3
  132. 1 2 5
  133. 2 3 5
  134. 1 3 5
  135.  
  136.  
  137. 12 13
  138. 1 3 1
  139. 3 8 2
  140. 8 9 3
  141. 2 10 3
  142. 10 11 1
  143. 11 12 1
  144. 2 4 2
  145. 1 2 5
  146. 4 5 1
  147. 5 12 1
  148. 2 6 3
  149. 6 7 2
  150. 7 5 3
  151.  
  152. 121 156
  153. 108 52 58
  154. 24 108 89
  155. 61 108 11
  156. 106 61 73
  157. 113 52 67
  158. 44 24 22
  159. 8 113 12
  160. 80 108 39
  161. 39 61 70
  162. 41 113 41
  163. 16 41 5
  164. 5 39 66
  165. 31 52 85
  166. 34 52 37
  167. 47 34 80
  168. 56 31 44
  169. 98 80 84
  170. 99 16 39
  171. 86 16 70
  172. 13 44 48
  173. 22 13 32
  174. 11 106 77
  175. 104 52 41
  176. 28 39 74
  177. 9 16 68
  178. 77 86 29
  179. 70 86 50
  180. 62 52 5
  181. 105 47 46
  182. 33 86 76
  183. 18 61 13
  184. 6 9 86
  185. 3 86 25
  186. 38 104 60
  187. 64 56 89
  188. 111 41 52
  189. 69 104 64
  190. 100 69 30
  191. 45 52 17
  192. 96 11 31
  193. 121 11 82
  194. 35 86 32
  195. 14 16 19
  196. 115 96 54
  197. 92 100 78
  198. 95 33 26
  199. 37 105 31
  200. 112 95 40
  201. 81 121 82
  202. 89 16 2
  203. 60 81 17
  204. 21 28 12
  205. 50 13 6
  206. 116 69 98
  207. 65 98 82
  208. 36 9 88
  209. 26 41 6
  210. 83 104 70
  211. 20 92 13
  212. 17 69 16
  213. 107 39 93
  214. 71 107 42
  215. 32 5 75
  216. 90 13 52
  217. 109 36 99
  218. 55 21 77
  219. 1 41 43
  220. 119 24 20
  221. 48 38 70
  222. 68 44 50
  223. 57 121 28
  224. 103 112 58
  225. 43 33 32
  226. 67 48 6
  227. 2 31 57
  228. 51 100 16
  229. 117 38 100
  230. 59 20 34
  231. 25 81 16
  232. 84 109 98
  233. 54 37 90
  234. 114 38 15
  235. 120 109 80
  236. 12 25 14
  237. 72 108 39
  238. 15 52 80
  239. 85 111 31
  240. 91 108 74
  241. 19 54 42
  242. 40 52 58
  243. 49 33 18
  244. 76 9 59
  245. 118 99 83
  246. 78 55 22
  247. 97 32 91
  248. 82 31 36
  249. 75 68 97
  250. 27 31 83
  251. 87 43 54
  252. 42 14 41
  253. 93 60 63
  254. 29 27 2
  255. 53 72 20
  256. 66 72 53
  257. 79 69 51
  258. 46 93 35
  259. 30 99 68
  260. 73 60 80
  261. 58 64 2
  262. 94 42 25
  263. 110 61 50
  264. 88 94 34
  265. 4 40 94
  266. 74 119 13
  267. 63 108 56
  268. 10 66 12
  269. 7 29 41
  270. 102 21 73
  271. 23 64 87
  272. 101 67 37
  273. 62 91 46
  274. 49 13 74
  275. 34 86 86
  276. 118 24 72
  277. 111 120 26
  278. 57 28 5
  279. 27 91 81
  280. 120 76 78
  281. 121 100 29
  282. 15 19 10
  283. 79 13 70
  284. 113 33 79
  285. 17 18 53
  286. 102 10 75
  287. 77 118 83
  288. 4 39 60
  289. 3 99 22
  290. 99 73 75
  291. 54 50 71
  292. 17 121 68
  293. 9 86 80
  294. 111 9 33
  295. 77 84 29
  296. 101 52 8
  297. 96 13 29
  298. 116 13 56
  299. 49 51 77
  300. 113 32 67
  301. 70 119 98
  302. 102 11 39
  303. 11 64 31
  304. 83 40 26
  305. 34 69 19
  306. 32 8 17
  307. 113 59 1
  308. 105 81 28
  309. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement