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. Ueditor百度编辑器插件的安装

    目录 插件下载地址: 1. 引入 2. 编辑器显示处 id="content" 3.底部 4.ueditor编辑器按钮配置方法 定制工具栏图标 修改配置项的方法: 插件下载地址: ...

  2. docker时区正常,但java获得的时间早了8小时解决方法

    我解决容器时区的方法是挂载宿主机的/etc/localtime 到容器的/etc/localtime,这时输入date命令容器时区显示正常,但是跑在容器中的java项目取到的时间却早了8小时. 查阅相 ...

  3. 初学js之qq聊天展开实例

    实现这样的效果. 直接看代码,html部分: <body> <div class="box"> <div class="lists" ...

  4. django处理上传文件配置

    1.sttings中配置 'django.template.context_processors.media' TEMPLATES = [ { 'BACKEND': 'django.template. ...

  5. 文件的特殊权限(SUID,SGID,SBIT)

    文件的一般权限:r w x  对应 421  文件的特殊权限:SUID SGID SBIT对应 421  文件的隐藏权限:chattr设置隐藏权限,lsattr查看文件的隐藏权限. 文件访问控制列表: ...

  6. Go语言之并发编程(一)

    轻量级线程(goroutine) 在编写socket网络程序时,需要提前准备一个线程池为每一个socket的收发包分配一个线程.开发人员需要在线程数量和CPU数量间建立一个对应关系,以保证每个任务能及 ...

  7. js数据类型的检测总结,附面试题--封装一个函数,输入任意,输出他的类型

    一.javascript 中有几种类型的值 1.基本数据类型 : 包括 Undefined.Null.Boolean.Number.String.Symbol (ES6 新增,表示独一无二的值) 特点 ...

  8. 配置网络策略中的 NAP 条件

    TechNet 库 Windows Server Windows Server 2008 R2 und Windows Server 2008 按类别提供的 Windows Server 内容 Win ...

  9. leetcode 【 Search Insert Position 】python 实现

    题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...

  10. IntelliJ IDEA 视频教程

    相关视频教程: Intellij IDEA视频教程 最新版Intellij IDEA视频教程