hdu 2363(限制最短路 二分+枚举+最短路)

来源:互联网 发布:知鸟软件 编辑:程序博客网 时间:2024/05/21 12:42

Cycling

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


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
17 949133541 2 12 3 13 4 14 7 11 5 45 6 46 7 45 3 26 4 2
 

Sample Output
2 11
题意:求一个最高点高度与最低点高度之差最小的最短路。
解题思路:这道题容易想到之前做过的求最大边与最小边差最小的生成树问题。不过这道题是最短路。思路还是一样的,只不过我们需要枚举上限高度和下限高度,即找到某条路使得路上的所有点的高度在下限与上限之间。这里可以采用二分+枚举的方法,类似于之前的限制生成树问题。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;const int maxn = 105;const int inf = 0x3f3f3f3f;int n,m,h[maxn],map[maxn][maxn];int Hmax,Hmin,up,low,dis[maxn],order[maxn];bool inq[maxn];int spfa(int s){memset(inq,false,sizeof(inq));for(int i = 1; i <= n; i++) dis[i] = inf;dis[s] = 0;if(h[s] < low || h[s] > up) return inf;queue<int> q;q.push(s);while(!q.empty()){int u = q.front();q.pop();inq[u] = false;for(int i = 1; i <= n; i++)if(map[u][i] != inf && h[i] >= low && h[i] <= up)if(dis[i] > dis[u] + map[u][i]){dis[i] = dis[u] + map[u][i];if(inq[i] == false){inq[i] = true;q.push(i);}}}return dis[n];}int main(){int t,u,v,c;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);Hmax = 0, Hmin = inf;for(int i = 1; i <= n; i++){scanf("%d",&h[i]);Hmax = max(Hmax,h[i]);Hmin = min(Hmin,h[i]);order[i] = h[i];}sort(order+1,order+1+n);memset(map,inf,sizeof(map));for(int i = 1; i <= m; i++){scanf("%d%d%d",&u,&v,&c);map[u][v] = map[v][u] = min(map[u][v],c);}int l = 0,r = Hmax - Hmin,mid,tmp,ans = inf,minlen = inf;while(l <= r){mid = (l + r) >> 1;bool flag = false;for(int i = 1; i <= n; i++){low = order[i];up = order[i] + mid;tmp = spfa(1);if(tmp != inf){flag = true;break;}}if(flag){r = mid - 1;if(ans > mid){ans = mid;minlen = tmp;}else if(ans == mid && minlen > tmp)minlen = tmp;}else l = mid + 1;}printf("%d %d\n",ans,minlen);}return 0;}


0 0
原创粉丝点击