zoukankan      html  css  js  c++  java
  • poj 3615 Cow Hurdles(floyd)

    又是一道改变松弛条件的题目,因为查询不是一组,所以用floyd比较快。

    解题过程:题目很容易理解,就是从一个点A到另一个点B的所有路中最大高度中的最小值。因为有多组查询,用floyd求任意两点间的最小值,改变一下map[i][j]里的存储,存储点i到点j的所有路中的最大高度的最小值就行了。

    代码:

    View Code
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <math.h>
    #include <queue>
    #define  N 305
    #define  INF 1000000000
    using namespace std ;
    
    int map[N][N] ;
    int n , m , t ;
    
    int main()
    {
        int i , j , x , y , z , k ;
    
        while ( scanf ( "%d%d%d" , &n , &m , &t ) != EOF )
        {
            for ( i = 1 ; i <= n ; i++ )
            for ( j = 1 ; j <= n ; j++ )
            map[i][j] = INF ;
    
            for ( i = 1 ; i <= m ; i++ )
            {
                scanf ( "%d%d%d" , &x , &y , &z );
                if ( map[x][y] > z )
                map[x][y] = z ;
            }
    
            for ( k = 1 ; k <= n ; k++ )
            for ( i = 1 ; i <= n ; i++ )
            for ( j = 1 ; j <= n ; j++ )
            if ( map[i][k] != INF && map[k][j] != INF )
            map[i][j] = min( max ( map[i][k] , map[k][j] ) , map[i][j] ) ;
    
            while ( t-- )
            {
                scanf ( "%d%d" , &x , &y ) ;
                if ( map[x][y] == INF )
                printf ( "-1\n" );
                else
                printf ( "%d\n" , map[x][y] );
            }
        }
        return 0 ;
    }
  • 相关阅读:
    Struts2拦截器
    Struts2 数据封装与值栈
    Struts2的环境搭配
    自学spring AOP
    小白学Maven第二篇配置Ecilpse
    小白学Maven第一篇配置
    web项目jsp出现The superclass javax.servlet.http.HttpServlet was not found on the Java Build Path错误
    软件测试复习(二)
    软件测试复习(一)
    白盒测试概述
  • 原文地址:https://www.cnblogs.com/misty1/p/2733113.html
Copyright © 2011-2022 走看看