Cycling

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

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
 

题意:有n个点m条边,每个点都有一个高度,问在保证高度之差最小的情况下从1点到第n点,最小高度差和最短路分别是多少?

题解:这题做的时候完全没思路,一直在想两点之间的高度问题,怎样才能从子问题递推到父亲问题找到最优的解,后面还是不会,,,然后看了题解发现自己的思维太死板了。。这个题只要枚举所有的高度差,然后按照高度差排序,当在某个高度差的限制下我们能够达到第n点(当然这个时候路径上每个点都应该在low和high之间),那么这个结果就是我们要的结果。
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <queue>
#include <string.h>
using namespace std;
typedef long long ll;
const int N = ;
const int INF = ;
struct Node { ///枚举高度差所需要用到的结构体
int low,high;
}node[N*N];
struct Edge{
int v,w,next;
}edge[N*N];
int head[N];
ll h[N];
int graph[N][N];
int n,m;
int cmp(Node a,Node b){
return (a.high-a.low)<(b.high-b.low);
}
bool vis[N];
int d[N];
void addEdge(int u,int v,int w,int &k){
edge[k].v = v,edge[k].w = w;
edge[k].next = head[u],head[u]=k++;
}
void spfa(int s,int low,int high){
queue<int > q;
for(int i=;i<=n;i++){
d[i] = INF;
vis[i] = false;
}
d[s] = ;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
if(h[u]>high||h[u]<low) continue;
for(int k = head[u];k!=-;k=edge[k].next){
int v = edge[k].v,w = edge[k].w;
if(h[v]>high||h[v]<low) continue;
if(d[v]>d[u]+w){
d[v] = d[u]+w;
if(!vis[v]){
vis[v]=true;
q.push(v);
}
}
}
}
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--){
memset(head,-,sizeof(head));
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%lld",&h[i]);
}
int tot = ;
for(int i=;i<=m;i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
addEdge(a,b,c,tot);
addEdge(b,a,c,tot);
}
int k = ;
for(int i=;i<=n;i++){
for(int j=i;j<=n;j++){
if(h[i]<h[j]){
node[k].low = h[i]; node[k++].high = h[j];
}
else {
node[k].low = h[j];
node[k++].high = h[i];
}
}
}
sort(node,node+k,cmp);
for(int i=;i<k;i++){
spfa(,node[i].low,node[i].high);
if(d[n]<INF){
printf("%d %d\n",node[i].high-node[i].low,d[n]);
break;
}
}
}
}
 

hdu 2363(枚举+最短路好题)的更多相关文章

  1. HDU 5521.Meeting 最短路模板题

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  2. POJ 4046 Sightseeing 枚举+最短路 好题

    有n个节点的m条无向边的图,节点编号为1~n 然后有点权和边权,给出q个询问,每一个询问给出2点u,v 输出u,v的最短距离 这里的最短距离规定为: u到v的路径的所有边权+u到v路径上最大的一个点权 ...

  3. poj1511/zoj2008 Invitation Cards(最短路模板题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Invitation Cards Time Limit: 5 Seconds    ...

  4. HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)

    HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...

  5. HDU 2802 F(N)(简单题,找循环解)

    题目链接 F(N) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  6. hdu 4568 Hunter 最短路+dp

    Hunter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  7. hdu-3790最短路刷题

    title: hdu-3790最短路刷题 date: 2018-10-20 14:50:31 tags: acm 刷题 categories: ACM-最短路 概述 一道最短路的水题,,,尽量不看以前 ...

  8. [poj2449]Remmarguts' Date(K短路模板题,A*算法)

    解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...

  9. 牛客小白月赛6 I 公交线路 最短路 模板题

    链接:https://www.nowcoder.com/acm/contest/136/I来源:牛客网 题目描述 P市有n个公交站,之间连接着m条道路.P市计划新开设一条公交线路,该线路从城市的东站( ...

随机推荐

  1. 爬虫之Scarpy.Request

    一 .Request 1.request Scarpy中的HTTP请求对象 1.1.Requse的构造 #我们ctrl+左键可以看到Scarpy.Request的代码 class Request(ob ...

  2. PSTR、LPSTR等宏原型

    1.首先介绍char.wchar_t ,宽字符wchar_t和窄字符char. 窄字符char了,大家都很清楚,就是8bit表示的byte,长度固定.char字符只能表示ASII码表中的256个字符, ...

  3. Spark MLlib(下)--机器学习库SparkMLlib实战

    1.MLlib实例 1.1 聚类实例 1.1.1 算法说明 聚类(Cluster analysis)有时也被翻译为簇类,其核心任务是:将一组目标object划分为若干个簇,每个簇之间的object尽可 ...

  4. 有三个线程T1 T2 T3,如何保证他们按顺序执行

    T3先执行,在T3的run中,调用t2.join,让t2执行完成后再执行t3 在T2的run中,调用t1.join,让t1执行完成后再让T2执行 public class JoinTest {     ...

  5. Diycode开源项目 TopicContentActivity分析

    1.效果预览以及布局分析 1.1.实际效果预览 左侧话题列表的布局是通过TopicProvider来实现的,所以当初分析话题列表就没有看到布局. 这里的话题内容不是一个ListView,故要自己布局. ...

  6. Jetty,Tomcat对MIME协议的配置参数说明

      此处做一下小的汇总,针对Jetty容器内,存在excel的xlsx文件直接通过链接的方式下载的时候,如果是在Chrome浏览器时,则直接触发浏览器的下载行为,但是在IE11的浏览器上,则浏览器会直 ...

  7. C语言编程题001

    有一颗树,一年两个生长周期,春天它长高一倍,夏天长高1m,问N个周期后树有多高?假设从春天开始树高为1m,第0个周期树高为1m. 要求:1.可以同时输入多个生长周期 如:3//表示下面有几个生长周期 ...

  8. ADO之密码验证--3次错误就锁定

    这个程序是那vs2010下写的,C#语言.数据库是sql server 2008 首先在数据库中新建一个数据库Test1,在数据库中新建一个表用来保存用户名和密码USERINFO, CREATE TA ...

  9. python - 接口自动化测试 - MysqlUtil - 数据库操作封装

    # -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: mysql_util.py @ide: PyCharm C ...

  10. application.properties 详解

    mvc spring.mvc.async.request-timeout设定async请求的超时时间,以毫秒为单位,如果没有设置的话,以具体实现的超时时间为准,比如tomcat的servlet3的话是 ...