题目描述
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!)输入格式
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.输出格式
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.输入样例
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输出样例
2 11
分析
题目大意是有n个点和m条边,每个点都有相应的高度,求最下高度差下的最短路。
本题主要通过二分思想,枚举高度差,再统计以某个点为基础的高度范围内的最短路,若不存在最短路,那么说明这个高度差还太小,如果存在最短路,那么说明还可以继续枚举更小的高度差,注意枚举的高度范围内要判断1号点和n号点是否存在。
以下是分别用dijkstra、SPFA和SPFA(SLF优化)算法写成的源码。
源程序
Dijkstra算法
#include <bits/stdc++.h>
#define MAXN 105
#define INF 0x3f3f3f3f
using namespace std;
struct Edge{
int v,w,next;
Edge(){};
Edge(int _v,int _w,int _next){
v=_v,w=_w,next=_next;
};
bool operator <(const Edge a)const{
return w>a.w;
}
}edge[MAXN*MAXN];
int EdgeCount,head[MAXN];
int t,n,m,imax,imin,point[MAXN],tmp[MAXN],dis[MAXN];
bool used[MAXN];
bool cmp(int a,int b)
{
return a<b;
}
void addEdge(int u,int v,int w)
{
edge[++EdgeCount]=Edge(v,w,head[u]);
head[u]=EdgeCount;
}
void dijkstra(int low,int up)
{
priority_queue<Edge> q;
memset(dis,0x3f,sizeof(dis));
memset(used,false,sizeof(used));
dis[1]=0;
q.push(Edge{1,0,0});
while(!q.empty()){
int u=q.top().v;q.pop();
if(used[u])continue;
used[u]=true;
for(int i=head[u];i;i=edge[i].next){
int v=edge[i].v,w=edge[i].w;
if(point[v]<low||point[v]>up)continue; //超过枚举高度差
if(dis[v]>dis[u]+w){
dis[v]=dis[u]+w;
q.push(Edge{v,dis[v],0});
}
}
}
}
int main()
{
scanf("%d",&t);
while(t--){
memset(head,0,sizeof(head)); //初始化
EdgeCount=0;
imin=INF,imax=-INF;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&point[i]);
tmp[i]=point[i];
imin=min(imin,point[i]);
imax=max(imax,point[i]);
}
for(int i=1;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addEdge(u,v,w);
addEdge(v,u,w);
}
sort(tmp+1,tmp+n+1,cmp); //按从低到高排序
int l=0,r=imax-imin+1,ans1=INF,ans2=INF;
while(l<r){ //二分高度差
int mid=(l+r)>>1,d=INF;
bool flag=false;
for(int i=1;i<=n;i++){
int low=tmp[i],up=tmp[i]+mid;
if(point[1]<low||point[1]>up)continue;
dijkstra(low,up);
if(dis[n]!=INF){
flag=true;
d=min(d,dis[n]);
break;
}
}
if(flag){
r=mid;
if(mid<ans1){ //高度差更小
ans1=mid;
ans2=d;
}
}
else l=mid+1;
}
printf("%d %d\n",ans1,ans2);
}
}
SPFA算法
#include <bits/stdc++.h>
#define MAXN 105
#define INF 0x3f3f3f3f
using namespace std;
struct Edge{
int v,w,next;
Edge(){};
Edge(int _v,int _w,int _next){
v=_v,w=_w,next=_next;
};
}edge[MAXN*MAXN];
int EdgeCount,head[MAXN];
int t,n,m,imax,imin,point[MAXN],tmp[MAXN],dis[MAXN];
bool ven[MAXN];
bool cmp(int a,int b)
{
return a<b;
}
void addEdge(int u,int v,int w)
{
edge[++EdgeCount]=Edge(v,w,head[u]);
head[u]=EdgeCount;
}
void SPFA(int low,int up)
{
queue<int> q;
memset(dis,0x3f,sizeof(dis));
memset(ven,false,sizeof(ven));
dis[1]=0;
ven[1]=true;
q.push(1);
while(!q.empty()){
int u=q.front();q.pop();
ven[u]=false;
for(int i=head[u];i;i=edge[i].next){
int v=edge[i].v,w=edge[i].w;
if(point[v]<low||point[v]>up)continue; //超过枚举高度差
if(dis[v]>dis[u]+w){
dis[v]=dis[u]+w;
if(!ven[v]){
q.push(v);
ven[v]=true;
}
}
}
}
}
int main()
{
scanf("%d",&t);
while(t--){
memset(head,0,sizeof(head)); //初始化
EdgeCount=0;
imin=INF,imax=-INF; //最低、最高高度
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){ //记录点高度
scanf("%d",&point[i]);
tmp[i]=point[i];
imin=min(imin,point[i]);
imax=max(imax,point[i]);
}
for(int i=1;i<=m;i++){ //建图
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addEdge(u,v,w);
addEdge(v,u,w);
}
sort(tmp+1,tmp+n+1,cmp); //高度按从低到高排序
int l=0,r=imax-imin+1,ans1=INF,ans2=INF;
while(l<r){ //二分高度差
int mid=(l+r)>>1,d=INF;
bool flag=false;
for(int i=1;i<=n;i++){
int low=tmp[i],up=tmp[i]+mid; //高度范围
if(point[1]<low||point[1]>up)continue; //高度范围内没有1号点
SPFA(low,up);
if(dis[n]!=INF){ //存在路径
flag=true;
d=min(d,dis[n]);
break;
}
}
if(flag){ //找到最小路径
r=mid;
if(mid<ans1){ //高度差更小
ans1=mid;
ans2=d;
}
}
else l=mid+1; //不存在路径
}
printf("%d %d\n",ans1,ans2);
}
}
SPFA算法之SLF优化
#include <bits/stdc++.h>
#define MAXN 105
#define INF 0x3f3f3f3f
using namespace std;
struct Edge{
int v,w,next;
Edge(){};
Edge(int _v,int _w,int _next){
v=_v,w=_w,next=_next;
};
}edge[MAXN*MAXN];
int EdgeCount,head[MAXN];
int t,n,m,imax,imin,point[MAXN],tmp[MAXN],dis[MAXN];
bool ven[MAXN];
bool cmp(int a,int b)
{
return a<b;
}
void addEdge(int u,int v,int w)
{
edge[++EdgeCount]=Edge(v,w,head[u]);
head[u]=EdgeCount;
}
void SPFA(int low,int up)
{
deque<int> q;
memset(dis,0x3f,sizeof(dis));
memset(ven,false,sizeof(ven));
dis[1]=0;
ven[1]=true;
q.push_back(1);
int cnt;
while(cnt=q.size()){
int u=q.front();q.pop_front();
ven[u]=false;
for(int i=head[u];i;i=edge[i].next){
int v=edge[i].v,w=edge[i].w;
if(point[v]<low||point[v]>up)continue; //超过枚举高度差
if(dis[v]>dis[u]+w){
dis[v]=dis[u]+w;
if(!ven[v]){
if(cnt>1&&dis[v]>dis[q.front()])q.push_front(v);
else q.push_back(v);
ven[v]=true;
}
}
}
}
}
int main()
{
scanf("%d",&t);
while(t--){
memset(head,0,sizeof(head)); //初始化
EdgeCount=0;
imin=INF,imax=-INF; //最低、最高高度
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){ //记录点高度
scanf("%d",&point[i]);
tmp[i]=point[i];
imin=min(imin,point[i]);
imax=max(imax,point[i]);
}
for(int i=1;i<=m;i++){ //建图
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addEdge(u,v,w);
addEdge(v,u,w);
}
sort(tmp+1,tmp+n+1,cmp); //高度按从低到高排序
int l=0,r=imax-imin+1,ans1=INF,ans2=INF;
while(l<r){ //二分高度差
int mid=(l+r)>>1,d=INF;
bool flag=false;
for(int i=1;i<=n;i++){
int low=tmp[i],up=tmp[i]+mid; //高度范围
if(point[1]<low||point[1]>up)continue; //高度范围内没有1号点
SPFA(low,up);
if(dis[n]!=INF){ //存在路径
flag=true;
d=min(d,dis[n]);
break;
}
}
if(flag){ //找到最小路径
r=mid;
if(mid<ans1){ //高度差更小
ans1=mid;
ans2=d;
}
}
else l=mid+1; //不存在路径
}
printf("%d %d\n",ans1,ans2);
}
}