现在的位置: 首页 > 综合 > 正文

HDU 2363 Cycling 最短路+枚举

2017年11月22日 ⁄ 综合 ⁄ 共 3517字 ⁄ 字号 评论关闭

Cycling

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

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
/*
HDU 2363 最短路+枚举 
小明从家里走到学校去考试, 路上有各个交叉点,它们有自己的海拔高度。 
小明从家里走到学校的路上,必然会经过不同的交叉点,因此会不断的走上走下,忐忐忑忑,这让他很不安,会影响他考试的发挥。因此,他要选择一条起伏最小的路去学校。所谓的“起伏最小”,是指这条路上海拔最高的点与海拔最低的点的差值最小。
在起伏最小的前提下,还要求出路程距离最短。

就是1走到n,高度差最小,高度差相同,最短路 
先求出所有的高度差,排序后枚举,每次都一次spfa,求出dist,
若dist[n]!=inf,说明是在最小高度差下找到了最短路径,break 
*/
#include<iostream>
#include<stdio.h>
#include<vector>
#include<queue> 
#include<algorithm>
using namespace std;
#define N 110
const int inf=1<<30;

struct node{
    int e,w;
};
vector<node>map[N];
struct point{
	int low,high;
};
point H[N*N];
int dis[N],vis[N],h[N];
int n,m;
bool cmp(point a,point b)
{
	return (a.high-a.low)<(b.high-b.low);
}
void spaf(int low,int high)//对每个高度差 spaf 
{
	int i,st,e,w;
	queue<int> q;
	for(i=1;i<=n;i++)//1到每个点的距离 
		dis[i]=inf;
	memset(vis,0,sizeof(vis));
	dis[1]=0;
	q.push(1);
	while(!q.empty())
	{
		st=q.front();
		q.pop();
		vis[st]=0;//出队列要要置为0,因为有可能再次进队列
		
		if(h[st]<low||h[st]>high)//控制高度差 
			continue;
		for(i=0;i<map[st].size();i++)//该点相邻的边 
		{
			e=map[st][i].e;
			w=map[st][i].w;
			if(h[e]>=low&&h[e]<=high&&dis[st]+w<dis[e])
			{
				dis[e]=dis[st]+w;
				if(!vis[e])
				{
					q.push(e);
					vis[e]=1;
				}
			}
		}
	}
} 
int main()
{
	int t,i,j,a,b,c,k;
	node p1,p2;
	
//	freopen("test.txt","r",stdin);
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		
		for(int i=1;i<=n;i++)map[i].clear();
        memset(H,0,sizeof(H));
        memset(h,0,sizeof(h));
        
		for(i=1;i<=n;i++)//每个点的高度 
			scanf("%d",&h[i]);
			
		for(i=0;i<m;i++)
		{
			scanf("%d%d%d",&a,&b,&c);
			p1.e=a,p1.w=c;//与a点相邻的边及值 
			p2.e=b,p2.w=c;
			map[a].push_back(p2);//每个点相邻的边 
			map[b].push_back(p1);
		}
		//枚举任意两个节点的高度差,排序 
		k=0;
		for(i=1;i<=n;i++)
			for(j=i;j<=n;j++)
			{
				H[k].low=min(h[i],h[j]);
				H[k++].high=max(h[i],h[j]);
			}
		sort(H,H+k,cmp);
		
		for(i=0;i<k;i++)
		{
			spaf(H[i].low,H[i].high);
			if(dis[n]!=inf)
				break;
		}
		printf("%d %d\n",H[i].high-H[i].low,dis[n]);
	}
	return 0;
} 

抱歉!评论已关闭.