求海拔差最小的前提下的最短路
思路:暴力搜索 =。=
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;
}