Light OJ 1002 Country Roads (Dijkstra)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.