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 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 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[110];
Point difference_h[110*110];
int u,v,val;
int tol=0;
/*****************************************************spaf模板*****************************************************/
const int MAXN = 1010;
const int INF = 0x3f3f3f3f;
struct Edge {
int v;
int cost;
Edge(int _v = 0, int _cost = 0) :
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 = 1; i <= n; i++)
dist[i] = INF;
vis[start] = true;
dist[start] = 0;
queue<int> que;
while (!que.empty())
que.pop();
que.push(start);//將第一個點放進隊列
memset(cnt, 0, sizeof(cnt));
cnt[start] = 1;//標記一下這個點第一次進入隊列中
while (!que.empty()) {
int u = que.front();
que.pop();
if(h[u]<low||h[u]>high)//控制高度差
continue;
vis[u] = false;
for (int i = 0; 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=0;i<=n;i++){
E[i].clear();
}
tol=0;
}
int main(){
// freopen("in.txt","r",stdin);
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
init();
for(int i=1;i<=n;i++){
scanf("%d",&h[i]);
}
for(int i=0;i<m;i++){
scanf("%d%d%d",&u,&v,&val);
//建圖
addedge(u,v,val);
}
// cout<<"ok"<<endl;
for(int i=1;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=0;i<tol;i++){
SPFA(1,n,difference_h[i].low,difference_h[i].high);
if(dist[n]!=INF){
printf("%d %d
",difference_h[i].high-difference_h[i].low,dist[n]);
break;
}
}
}
return 0;
}
總結
- 上一篇: 使用Puppeteer进行数据抓取(一)
- 下一篇: 安装CentOS-7-x86_64-Mi