Topic Connection:
http://www.lightoj.com/volume_showproblem.php?problem=1002
Title Description:
There are n cities, starting from 0 to n-1 numbering, between n cities there is M-bar, the central city for T, which asks each city to the cost of the smallest path of the central city, the path cost size is defined as: The value of the largest side that is spent on a road.
Problem Solving Ideas:
Dijkstra of the deformation, the single-source path with the Dijkstra can ensure that each edge is optimal, so the shortest way to the longest side is the request.
1#include <algorithm>2#include <iostream>3#include <cstdio>4#include <cstring>5 using namespacestd;6 7 Const intMAXN =510;8 Const intINF =0x3f3f3f3f;9 intMAP[MAXN][MAXN], DIST[MAXN];Ten voidInit () One { A intI, J; - for(i=0; i<maxn; i++) - for(j=0; j<maxn; J + +) the if(i==j) -MAP[I][J] =0; - Else -MAP[I][J] =INF; + } - voidDijkstra (intSintN) + { A intI, J, Vis[maxn]; atmemset (Vis,0,sizeof(Vis)); - - for(i=0; i<n; i++) -Dist[i] =Map[s][i]; -Vis[s] =1; - in for(i=1; i<n; i++) - { to intMini =INF, index; + for(j=0; j<n; J + +) - if(!vis[j] && mini >Dist[j]) the { *Mini =Dist[j]; $index =J;Panax Notoginseng } - if(Mini = =INF) the return ; +Vis[index] =1; A for(j=0; j<n; J + +) the if(!Vis[j]) + { -Dist[j] = min (Dist[j], Max (Map[index][j],dist[index]));//Focus $ } $ } - } - intMain () the { - intT, N, m, L =1;Wuyiscanf ("%d", &T); the while(T--) - { Wu init (); -scanf ("%d%d", &n, &m); About while(M--) $ { - intu, V, W; -scanf (" %d%d%d", &u, &v, &W); - if(Map[u][v] > W)//with heavy edges, choose the best AMAP[U][V] = Map[v][u] =W; + } the intT; -scanf ("%d", &t); $ Dijkstra (t, n); theprintf ("Case %d:\n", L + +); the for(intI=0; i<n; i++) the if(Dist[i]! =INF) theprintf ("%d\n", Dist[i]); - Else inprintf ("impossible\n"); the } the return 0; About}
Light OJ 1002 Country Roads (Dijkstra)