http://acm.hdu.edu.cn/showproblem.php?pid=2363

本文介绍了一种寻找从起点到终点最小海拔差的骑行路线算法。该算法首先对所有可能经过的点按海拔高度进行排序,然后通过Dijkstra算法找到在限定海拔范围内的最短路径,最终确定出既短且海拔起伏最小的路线。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Cycling

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 636    Accepted Submission(s): 221


Problem Description
You want to cycle to a programming contest. The shortest route to the contest might be over the tops of some mountains and through some valleys. From past experience you know that you perform badly in programming contests after experiencing large differences in altitude. Therefore you decide to take the route that minimizes the altitude difference, where the altitude difference of a route is the difference between the maximum and the minimum height on the route. Your job is to write a program that finds this route.
You are given:

the number of crossings and their altitudes, and

the roads by which these crossings are connected.
Your program must find the route that minimizes the altitude difference between the highest and the lowest point on the route. If there are multiple possibilities, choose the shortest one.
For example:



In this case the shortest path from 1 to 7 would be through 2, 3 and 4, but the altitude difference of that path is 8. So, you prefer to go through 5, 6 and 4 for an altitude difference of 2. (Note that going from 6 directly to 7 directly would have the same difference in altitude, but the path would be longer!)
 

Input
On the first line an integer t (1 <= t <= 100): the number of test cases. Then for each test case:

One line with two integers n (1 <= n <= 100) and m (0 <= m <= 5000): the number of crossings and the number of roads. The crossings are numbered 1..n.

n lines with one integer hi (0 <= hi <= 1 000 000 000): the altitude of the i-th crossing.

m lines with three integers aj , bj (1 <= aj , bj <= n) and cj (1 <= cj <= 1 000 000): this indicates that there is a two-way road between crossings aj and bj of length cj . You may assume that the altitude on a road between two crossings changes linearly.
You start at crossing 1 and the contest is at crossing n. It is guaranteed that it is possible to reach the programming contest from your home.
 

Output
For each testcase, output one line with two integers separated by a single space:

the minimum altitude difference, and

the length of shortest path with this altitude difference.
 

Sample Input
1 7 9 4 9 1 3 3 5 4 1 2 1 2 3 1 3 4 1 4 7 1 1 5 4 5 6 4 6 7 4 5 3 2 6 4 2
 

Sample Output
2 11
 
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
const int INF=1000000005;
struct Node
{
    int h ;
    int num ;
} node[110] ;
bool with[110];
int map[110][110] ;
int T ,N,M;
bool vis[110] ;
int dis[110] ;
int min_h , min_d ;
bool comp(Node n1,Node n2)
{
    return n1.h<n2.h;
}
void dijkstra()
{
    for(int i=0;i<=N;i++)
    {
        dis[i]=INF;
        vis[i]=false;
    }
    dis[1]=0;
    for(int i=1;i<=N;i++)
    {
        int u=0;
        int min=INF;
        for(int j=1;j<=N;j++)
        {
            if(with[j]==1&&!vis[j]&&min>dis[j])
            {
                min=dis[j];
                u=j;
            }
        }
        vis[u]=true;
        for(int j=1;j<=N;j++)
        {
            if(with[j]==1&&map[u][j]!=INF&&!vis[j])
            {
                if(dis[j]>dis[u]+map[u][j])
                {
                    dis[j]=dis[u]+map[u][j];
                }
            }
        }
    }
}
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d",&N,&M);
        for(int i=1; i<=N; i++)
        {
            scanf("%d",&node[i].h);
            node[i].num = i ;
        }
        for(int i=1; i<=N; i++)
        {
            for(int j=1; j<=N; j++)
            {
                if(i == j)	map[i][j] = 0 ;
                else		map[i][j] = INF ;
            }
        }
        int a,b,c;
        for(int i=1; i<=M; i++)
        {
            scanf("%d %d %d",&a,&b,&c);
            if(map[a][b] > c)
            {
                map[a][b] = map[b][a] = c ;
            }
        }
        sort(node+1, node+1+N , comp);
        min_h = INF ;
        min_d = INF ;
        for(int i=1; i<=N; i++)
        {
            for(int j=i; j<=N; j++)
            {
                memset(with,0,sizeof(with));
                int min_len = node[i].h ;
                int max_len = node[j].h ;
                for(int k=1; k<=N; k++)
                {
                    if(node[k].h >= min_len && node[k].h<=max_len)
                    {
                        with[node[k].num] = 1;
                    }
                }
                if(with[1]==0 || with[N]==0)	continue ;
                dijkstra() ;
                //printf("%d %d %d %d\n",i,j,max_len - min_len,dis[N]);
                if(dis[N] != INF)
                {
                    if(max_len - min_len < min_h)
                    {
                        min_h = max_len  - min_len ;
                        min_d = dis[N] ;
                    }
                    else if(max_len - min_len == min_h)
                    {
                        if(min_d > dis[N])
                            min_d = dis[N] ;
                    }
                }
            }
        }
        printf("%d %d\n",min_h,min_d);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值