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. Mysql相关子查询&&MySQL获取分组后的TOP N记录

    小燕子,哈哈哈哈~~~~~~~~~~ 相关子查询是指引用了外部查询列的子查询,即子查询会对外部查询的每行进行一次计算. 举个例子 root:test> show create table tes ...

  2. git 使用规范

    git使用资料: https://github.com/peak-c/my-git 公司内部使用开发规范: 一. 代码库介绍 个人开发库(git@gitlab.adrd.sohuno.com:sper ...

  3. Python 信号处理 signal 模块

    Table of Contents 1. signal模块简介 1.1. signal简单示例 1.2. signal说明 1.2.1. 基本的信号名 1.2.2. 常用信号处理函数 2. signa ...

  4. 几条 ffmpeg 的命令

    1,获取视频的信息   ffmpeg -i video.avi 2,将图片序列合成视频   ffmpeg -f image2 -i image%d.jpg video.mpg   上面的命令会把当前目 ...

  5. Asp.net自定义控件开发任我行(7)-注册自定义事件

    摘要 前面我们已经把嵌入资源讲完了,不知道大家有没有得到收益,本章主要讲自定义事件,也就是给TextBox注册一个点击事件. 引言 不知道道上的朋友有没有注意到TextBox控件没有点击事件,就连网上 ...

  6. IOS开发学习笔记009-OC基本知识

    开始学习OC,时间不等人啊,要抓紧了. OC基本知识 新建一个以.m结尾的文件,这既是oc的程序文件.在oc程序中完全兼容C语言.编译好链接类似. oc包含头文件是使用#import <> ...

  7. python - 接口自动化测试 - TestRecharge - 充值接口测试用例

    # -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: test_recharge.py @ide: PyChar ...

  8. 《HTTP协议详解》读书笔记---请求篇之消息报头

    不管是请求消息还是响应消息都包含消息报头,那么消息报头包含哪些内容?他们都代表什么含义呢?以下将带着 这些问题去学习消息报头. http消息(不管是请求消息还是响应消息)都是由开始行,消息报头(可选) ...

  9. python 中输入一个字符串,判断这个字符串中有多少个字符、数字、空格、特殊字符

    # -*- coding: utf8 -*- # Author:wxq #python 2.7 #首先定义一个字符串 str1 = raw_input('请输入一个字符:') #初始化字符.数字.空格 ...

  10. jquery的html、text、val的用法

    .html()用为读取和修改元素的HTML标签 .text()用来读取或修改元素的纯文本内容 .val()用来读取或修改表单元素的value值. 这三个方法功能上的对比 .html(),.text() ...