HDU-2363 Cycling

求海拔差最小的前提下的最短路

思路:暴力搜索 =。=

TL了无数次 只因没判重 欲哭无泪QAQ


#include "stdio.h"
#include "string.h"
#include "vector"
#include "queue"
#include "algorithm"
using namespace std;
const int maxn = 105;
const int inf = 1000000001;
int n,m;
int h[maxn];
int map[maxn][maxn];
bool vis[maxn];
struct node
{
    int v,dis;
    int Max,Min;
    bool operator <( const node &x )const
    {
        if( Min != x.Min )
            return Min < x.Min;
        if( Max != x.Max )
            return Max > x.Max;
        return dis > x.dis;
    }

}P[maxn];


void BFS( int s,int e )
{
    node cur,cnt;
    int ans = inf,dist = inf;
    priority_queue<node>que;
    for( int i = 1; i <= n; i ++ )
    {
        P[i].dis = P[i].Max = inf;
        P[i].Min = 0; P[i].v = i;
    }
    P[s].dis = 0; P[s].Max = P[s].Min = h[s];
    que.push(P[s]);
    while( !que.empty() )
    {
        cur = que.top();
        que.pop();
        if( cur.v == e )
        {
            if( cur.Max - cur.Min < ans )
            {
                ans =  cur.Max -  cur.Min;
                dist =  cur.dis;
            }
            else if( cur.Max - cur.Min == ans && dist > cur.dis )
            {
                dist =  cur.dis;
            }
        }
        for( int i = 1; i <= n; i ++ )
        {
            if( map[cur.v][i] != inf )
            {
                int Max = cur.Max > h[i]?cur.Max:h[i];
                int Min = cur.Min < h[i]?cur.Min:h[i];
                if( !( Max >= P[i].Max && Min <= P[i].Min && cur.dis + map[cur.v][i] >= P[i].dis ||  Max - Min > ans) )
                {
                    P[i].dis = cur.dis + map[cur.v][i];
                    P[i].Max = Max; P[i].Min = Min;
                    que.push(P[i]);
                }
            }
        }
    }
    printf("%d %d\n",ans,dist);
}

int main()
{
    //freopen( "1.txt","r",stdin );
    int t,u,v,w;
    scanf("%d",&t);
    while( t-- )
    {
        scanf("%d%d",&n,&m);
        for( int i = 1; i <= n; i ++ )
            for( int j = 1; j <= n; j ++ )
                map[i][j] = inf;
        for( int i = 1; i <= n; i ++ )
            scanf("%d",&h[i]);
        for( int i = 1; i <= m; i ++ )
        {
            scanf("%d%d%d",&u,&v,&w);
			if( map[u][v] > w )
	            map[u][v] = map[v][u] = w;
        }
        BFS(1,n);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值