Cycling
Cycling |
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
Total Submission(s): 105 Accepted Submission(s): 49 |
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. 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. |
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 |
Sample Output
2 11 |
Source
bapc2007_pre
|
Recommend
lcy
|
/*
题意:小明上学,从1到n,每个点都有高度,让你找出来一条高度差最小,这个条件下的最短路 初步思路:将所有的高度差计算出来,然后排序,从小到大找到第一个,能找到最短路的输出 #问题:很奇怪计算高度差的时候还要考虑和自身的高度差
*/
#include<bits/stdc++.h>
using namespace std;
struct Point{
int low,high;
Point(){}
Point(int a,int b){
low=a;
high=b;
}
bool operator < (const Point & b) const{
return (high-low)<(b.high-b.low);
}
};
int t;
int n,m;
int h[];
Point difference_h[*];
int u,v,val;
int tol=;
/*****************************************************spaf模板*****************************************************/
const int MAXN = ;
const int INF = 0x3f3f3f3f;
struct Edge {
int v;
int cost;
Edge(int _v = , int _cost = ) :
v(_v), cost(_cost) {
}
};
vector<Edge> E[MAXN];
void addedge(int u, int v, int w) {
E[u].push_back(Edge(v, w));
E[v].push_back(Edge(u, w));
}
bool vis[MAXN]; //在队列标志
int cnt[MAXN]; //每个点的入队列次数
int dist[MAXN];
bool SPFA(int start, int n,int low,int high) {
memset(vis, false, sizeof(vis));
for (int i = ; i <= n; i++)
dist[i] = INF;
vis[start] = true;
dist[start] = ;
queue<int> que;
while (!que.empty())
que.pop();
que.push(start);//将第一个点放进队列
memset(cnt, , sizeof(cnt));
cnt[start] = ;//标记一下这个点第一次进入队列中
while (!que.empty()) {
int u = que.front();
que.pop();
if(h[u]<low||h[u]>high)//控制高度差
continue;
vis[u] = false;
for (int i = ; i < E[u].size(); i++) {//遍历他所有能链接到的边
int v = E[u][i].v;
if (h[v]>=low&&h[v]<=high&&dist[v] > dist[u] + E[u][i].cost) {//进行缩点
dist[v] = dist[u] + E[u][i].cost;
if (!vis[v]) {//这个点没有访问过
vis[v] = true;
que.push(v);
if (++cnt[v] > n)
return false; //cnt[i]为入队列次数,用来判定是否存在负环回路
}
}
}
}
return true;
}
/*****************************************************spaf模板*****************************************************/
void init(){
for(int i=;i<=n;i++){
E[i].clear();
}
tol=;
}
int main(){
// freopen("in.txt","r",stdin);
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
init();
for(int i=;i<=n;i++){
scanf("%d",&h[i]);
}
for(int i=;i<m;i++){
scanf("%d%d%d",&u,&v,&val);
//建图
addedge(u,v,val);
}
// cout<<"ok"<<endl;
for(int i=;i<=n;i++){
for(int j=i;j<=n;j++){
difference_h[tol].low=min(h[i],h[j]);
difference_h[tol++].high=max(h[i],h[j]);
}
}
// cout<<"ok"<<endl;
sort(difference_h,difference_h+tol);
for(int i=;i<tol;i++){
SPFA(,n,difference_h[i].low,difference_h[i].high);
if(dist[n]!=INF){
printf("%d %d\n",difference_h[i].high-difference_h[i].low,dist[n]);
break;
}
}
}
return ;
}
Cycling的更多相关文章
- URAL 1966 Cycling Roads 点在线段上、线段是否相交、并查集
F - Cycling Roads Description When Vova was in Shenzhen, he rented a bike and spent most of the ...
- Aquarium Cycling
http://www.fishyou.com/aquarium-cycling.php Aquarium Cycling Aquarium cycling actually refers to the ...
- [USACO2002][poj1946]Cow Cycling(dp)
Cow CyclingTime Limit: 1000MS Memory Limit: 30000KTotal Submissions: 2468 Accepted: 1378Description ...
- Cycling Label
Cycling Label 来源: github/BBCyclingLabel Licence: Apache 2.0 作者: Bruno de Carvalho 分类: 标签(Label) 平台: ...
- Cow Cycling 动态规划
1552: Cow Cycling 时间限制(普通/Java):1000MS/10000MS 内存限制:65536KByte总提交: 39 测试通过:20 描述 The ...
- Ural 1966 Cycling Roads
================ Cycling Roads ================ Description When Vova was in Shenzhen, he rented a ...
- URAL 1966 Cycling Roads 计算几何
Cycling Roads 题目连接: http://acm.hust.edu.cn/vjudge/contest/123332#problem/F Description When Vova was ...
- cycling -avoid the vicious cycle
‘Numerous' studies in the past appear to have shown a link between cycling and ED. The researchers a ...
- UVALive - 6268 Cycling 贪心
UVALive - 6268 Cycling 题意:从一端走到另一端,有T个红绿灯,告诉你红绿灯的持续时间,求最短的到达终点的时间.x 思路:
随机推荐
- oracle 常用函数汇总
一.字符函数字符函数是oracle中最常用的函数,我们来看看有哪些字符函数:lower(char):将字符串转化为小写的格式.upper(char):将字符串转化为大写的格式.length(char) ...
- [js高手之路] html5 canvas系列教程 - arcTo(弧度与二次,三次贝塞尔曲线以及在线工具)
之前,我写了一个arc函数的用法:[js高手之路] html5 canvas系列教程 - arc绘制曲线图形(曲线,弧线,圆形). arcTo: cxt.arcTo( cx, cy, x2, y2, ...
- GitHub使用(三) - GitHub安装及初步使用
1. 首先下载Windows版本“GitHubSetup.exe”如下,下载地址为:https://desktop.github.com/
- hdu3974 找上属的模拟
There is a company that has N employees(numbered from 1 to N),every employee in the company has a im ...
- HIT 1917 Peaceful Commission
这道题题意就是给你n对人,一对中编号为x,x+1,给你m对矛盾,表示这两个人不能同时选. 然后就是Two-Sat的模板题了,就是根据对称性,连边,加缩点,最后拓扑排序,求出一组可行解就可以了. #in ...
- 如何维护一个1000 IP的免费代理池
楔子 好友李博士要买房了, 前几天应邀帮他抓链家的数据分析下房价, 爬到一半遇到了验证码. 李博士的想法是每天把链家在售的二手房数据都抓一遍, 然后按照时间序列分析. 链家线上在交易的二手房数据大概有 ...
- Python实战之IO多路复用select的详细简单练习
IO多路复用 I/O多路复用指:通过一种机制,可以监视多个描述符,一旦某个描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写操作. select 它通过一个select()系统调用来 ...
- python celery 时区&结果(性能)的坑
本文主要介绍最近使用celery遇到的两个坑.关于时区,以及是否保留结果(celery使用rabbitmq). 先说结论:定时任务记得配置时区:丢弃结果对使用rabbitmq对celery来说,性能提 ...
- 知识树杂谈Android面试(3)
一.Activity生命周期? a. Activity四种状态? Running.Paused(透明无焦点).Stopped.killed. b. OnStart() OnRusume区分? 是否可以 ...
- iOS 记录近期遇到的几个bug
1. actionSheet与pickerView 不兼容 发生环境:ios 9以上,其他无测试. actionSheet与pickerView在一起使用时,当actionSheet弹出后,紧接着再弹 ...