Cycling

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

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
 
Source
 题意:
n个点,m条路,每个点有一个权值,求从1点出发到n点,经过的点的权值最大与最小之差最小的一条最短路。
代码:
//这题气炸了,用dijk怎么做怎么不对,改了spfa才过的。要求最小差值的最短路可以把所有的点之间的差值
//算出来,按照差值从小到大排序,从小到大枚举每一个差值所对应的高度上下界,在这个范围之内求
//最短路,求到的第一个就是结果。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
using namespace std;
const int inf=0x7fffffff;
int dis[],vis[],hig[];
int up,low,t,n,m,cnt;
struct Lu
{
int x,y,w;
}L[];
bool cmp(Lu x,Lu y) {return x.w<y.w;}
struct node{
int to,value;
};
vector<node>g[];
int spfa()
{
int s=;
for(int i=;i<=n;i++)
dis[i]=inf;
memset(vis,,sizeof(vis));
vis[s]=;
dis[s]=;
queue<int>q;
q.push(s);
while(!q.empty()){
int cur=q.front();
q.pop();
vis[cur]=;
if(hig[cur]<low||hig[cur]>up) continue; //起始点也不例外
for(int i=;i<(int)g[cur].size();i++){
int k=g[cur][i].to;
if(hig[k]<low||hig[k]>up) continue; //在范围之中
if(dis[k]>dis[cur]+g[cur][i].value){
dis[k]=dis[cur]+g[cur][i].value;
if(!vis[k]){
vis[k]=;
q.push(k);
}
}
}
}
return dis[n];
}
int main()
{
int x,y,z,ans1,ans2;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
cnt=;ans2=inf;
for(int i=;i<=n;i++){
g[i].clear(); //记住。
scanf("%d",&hig[i]);
}
for(int i=;i<=n;i++){
for(int j=i;j<=n;j++){ //有可能起点等于终点所以j从i开始
L[cnt].x=min(hig[i],hig[j]);
L[cnt].y=max(hig[i],hig[j]);
L[cnt].w=L[cnt].y-L[cnt].x;
cnt++;
}
}
for(int i=;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
node no;
no.to=y;
no.value=z;
g[x].push_back(no);
no.to=x;
g[y].push_back(no);
}
sort(L,L+cnt,cmp);
int flag=,tmp;
for(int i=;i<cnt;i++){
if(flag&&tmp<L[i].w) break;//出现高度差一样,最短路不同的情况
low=L[i].x;up=L[i].y;
int ans=spfa();
if(ans!=inf){
ans1=L[i].w;
ans2=min(ans2,ans);
flag=;
tmp=L[i].w;
}
}
printf("%d %d\n",ans1,ans2);
}
return ;
}

HDU2363 最短路+贪心的更多相关文章

  1. Codeforces Round #303 (Div. 2) E. Paths and Trees 最短路+贪心

    题目链接: 题目 E. Paths and Trees time limit per test 3 seconds memory limit per test 256 megabytes inputs ...

  2. Codeforces 1076D Edge Deletion 【最短路+贪心】

    <题目链接> 题目大意: n个点,m条边的无向图,现在需要删除一些边,使得剩下的边数不能超过K条.1点为起点,如果1到 i 点的最短距离与删除边之前的最短距离相同,则称 i 为 " ...

  3. 【CF1076D】Edge Deletion 最短路+贪心

    题目大意:给定 N 个点 M 条边的无向简单联通图,留下最多 K 条边,求剩下的点里面从 1 号顶点到其余各点最短路大小等于原先最短路大小的点最多怎么构造. 题解:我们可以在第一次跑 dij 时直接采 ...

  4. Codeforces 545E. Paths and Trees[最短路+贪心]

    [题目大意] 题目将从某点出发的所有最短路方案中,选择边权和最小的最短路方案,称为最短生成树. 题目要求一颗最短生成树,输出总边权和与选取边的编号.[题意分析] 比如下面的数据: 5 5 1 2 2 ...

  5. Forethought Future Cup - Elimination Round D 贡献 + 推公式 + 最短路 + 贪心

    https://codeforces.com/contest/1146/problem/D 题意 有一只青蛙,一开始在0位置上,每次可以向前跳a,或者向后跳b,定义\(f(x)\)为青蛙在不跳出区间[ ...

  6. Codeforces Round #303 (Div. 2)(CF545) E Paths and Trees(最短路+贪心)

    题意 求一个生成树,使得任意点到源点的最短路等于原图中的最短路.再让这个生成树边权和最小. http://codeforces.com/contest/545/problem/E 思路 先Dijkst ...

  7. [CSP-S模拟测试]:任务分配(最短路+贪心+DP)

    题目传送门(内部题149) 输入格式 每个测试点第一行为四个正整数$n,b,s,m$,含义如题目所述. 接下来$m$行,每行三个非负整数$u,v,l$,表示从点$u$到点$v$有一条权值为$l$的有向 ...

  8. UOJ244 短路 贪心

    正解:贪心 解题报告: 传送门! 贪心真的都是些神仙题,,,以我的脑子可能是不存在自己想出解这种事情了QAQ 然后直接港这道题解法趴,,, 首先因为这个是对称的,所以显然的是可以画一条斜右上的对角线, ...

  9. 【AT2434】JOI 公園 (JOI Park) 最短路+贪心

    题解 我的歪解 我首先想的是分治,我想二分肯定不行,因为它是没有单调性的. 我想了一下感觉它的大部分数据应该是有凸性的(例如\(y=x^2\)的函数图像),所以可以三分. 下面是我的三分代码(骗了不少 ...

随机推荐

  1. LightOJ 1248 Dice (III)

    期望,$dp$. 设$dp[i]$表示当前已经出现过$i$个数字的期望次数.在这种状态下,如果再投一次,会出现两种可能,即出现了$i+1$个数字以及还是$i$个数字. 因此 $dp[i]=dp[i]* ...

  2. MVC源码分析 - 路由匹配

    上一篇 说到了路由事件注册以及路由表的生成, 前面 也解析到了, 管道事件的建立, 那么接下来, 肯定就是要调用执行这些事件了, 这些就不表了, 我已经得到我想要的部分了, 接下来, 在执行这些管道事 ...

  3. git 查看某个文件的历史修改版本

    [git status 查看修改的文件路径] git log --follow -p routes/admin/contract_operation.js

  4. 有哪些可以将网页另存图片的chrome插件?功能类似网页截图

    说实话chrome插件哪些,想找出最好最强的还真不容易,各有千秋.今天就概括性介绍下一些网页打印,图片另存为等常见需求的chrome插件.1.Awesome ScreenshotAwesome scr ...

  5. OpenCV备忘

    都是转来的内容的,算是整理一下 OpenCV备忘 深度和通道的理解 CV_8UC1 是指一个8位无符号整型单通道矩阵, CV_32FC2是指一个32位浮点型双通道矩阵 CV_8UC1 CV_8SC1 ...

  6. codevs3027线段覆盖2(DP)题解

    题目描述 Description 数轴上有n条线段,线段的两端都是整数坐标,坐标范围在0~1000000,每条线段有一个价值,请从n条线段中挑出若干条线段,使得这些线段两两不覆盖(端点可以重合)且线段 ...

  7. PHP数组关于数字键名的问题

    以下是对PHP数组数字键名的几点总结: 键名长度只能在 int 长度范围内,超过int 范围后将会出现覆盖等混乱情况 在键名长度为 int 范围内存取值时,PHP会强制将数字键名转换为 int 数值型 ...

  8. VS2012及以上版本 程序打包部署详解

    引用:  http://blog.csdn.net/zhang_xinxiu/article/details/9099757 程序编写测试完成后接下来我们要做的是打包部署程序,但VS2012让人心痛的 ...

  9. cocoaPods安装成功终端代码(期间报error: RPC failed; result=56, HTTP code = 200)

    Last login: Sat Oct 15 23:30:24 on ttys002 Sivek_lindeMacBook-Pro:~ Sivek_lin$ sudo gem update --sys ...

  10. 《Intel汇编第5版》 汇编拷贝字符串

    一.字符串定义 二.dup指令 三.调用Writestring过程 四.代码以及效果 TITLE String Copy INCLUDE Irvine32.inc includelib Irvine3 ...