Poj 1125 Floyd

Source: Internet
Author: User

Question:

It takes the shortest time to transmit information from a vertex to another vertex. Number of the person who receives the information at the latest.

Algorithm:

1. the Dijkstra algorithm can only find the single-source shortest path at a time, while the Floyd algorithm can find the shortest path for each pair of vertices.

2. enumerate the maximum straight from each point to each point, and compare the maximum straight of each point to obtain the answer. The maximum direct time is the time required to receive the information at the latest.

View code

#include<stdlib.h>#include<string.h>#include<stdio.h>int mp[210][210];int N;const int inf = 0x7f7f7f7f;void Floyd( ){ for( int k = 1; k <= N; k++)   for( int i = 1; i <= N; i++)    for( int j = 1; j <= N; j++)        {            if( mp[k][j] != inf && mp[i][j] > mp[i][k] + mp[k][j] && mp[k][j] != inf )             mp[i][j] = mp[i][k] + mp[k][j];         }}void solve( ){  int maxn = inf, ans = 0;  int minx = 0;  for( int i = 1; i <= N; i++)  {      minx = 0;    for( int j = 1; j <= N; j++)    {      if( mp[i][j] > minx )         minx = mp[i][j];    }    if( minx < maxn )    {       maxn = minx;       ans = i;    }  }  if( ans == 0 )    puts("disjoint");  else    printf("%d %d\n", ans, maxn);}int main( ){  int M, a, b;  while( scanf("%d",&N), N )  {    for( int i = 1; i <= N; i++)    for( int j = 1; j <= N; j++)        mp[i][j] = ( i == j ) ? 0 : inf;    for( int i = 1; i <= N; i++)    {        scanf("%d", &M);        for( int j = 1; j <= M; j++)        {           scanf("%d%d", &a, &b);           mp[i][a] = b;        }    }    Floyd();    solve( );       }  return 0;}

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.