http://acm.hdu.edu.cn/showproblem.php?pid=2363
来源:互联网 发布:穆雅斓的淘宝店怎么了 编辑:程序博客网 时间:2024/06/06 12:43
Cycling
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 636 Accepted Submission(s): 221
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!)
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.
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.
the minimum altitude difference, and
the length of shortest path with this altitude difference.
Sample Input
17 949133541 2 12 3 13 4 14 7 11 5 45 6 46 7 45 3 26 4 2
Sample Output
2 11
#include<iostream>#include<stdio.h>#include<algorithm>#include<string.h>#include<queue>using namespace std;const int INF=1000000005;struct Node{ int h ; int num ;} node[110] ;bool with[110];int map[110][110] ;int T ,N,M;bool vis[110] ;int dis[110] ;int min_h , min_d ;bool comp(Node n1,Node n2){ return n1.h<n2.h;}void dijkstra(){ for(int i=0;i<=N;i++) { dis[i]=INF; vis[i]=false; } dis[1]=0; for(int i=1;i<=N;i++) { int u=0; int min=INF; for(int j=1;j<=N;j++) { if(with[j]==1&&!vis[j]&&min>dis[j]) { min=dis[j]; u=j; } } vis[u]=true; for(int j=1;j<=N;j++) { if(with[j]==1&&map[u][j]!=INF&&!vis[j]) { if(dis[j]>dis[u]+map[u][j]) { dis[j]=dis[u]+map[u][j]; } } } }}int main(){ scanf("%d",&T); while(T--) { scanf("%d %d",&N,&M); for(int i=1; i<=N; i++) { scanf("%d",&node[i].h); node[i].num = i ; } for(int i=1; i<=N; i++) { for(int j=1; j<=N; j++) { if(i == j)map[i][j] = 0 ; elsemap[i][j] = INF ; } } int a,b,c; for(int i=1; i<=M; i++) { scanf("%d %d %d",&a,&b,&c); if(map[a][b] > c) { map[a][b] = map[b][a] = c ; } } sort(node+1, node+1+N , comp); min_h = INF ; min_d = INF ; for(int i=1; i<=N; i++) { for(int j=i; j<=N; j++) { memset(with,0,sizeof(with)); int min_len = node[i].h ; int max_len = node[j].h ; for(int k=1; k<=N; k++) { if(node[k].h >= min_len && node[k].h<=max_len) { with[node[k].num] = 1; } } if(with[1]==0 || with[N]==0)continue ; dijkstra() ; //printf("%d %d %d %d\n",i,j,max_len - min_len,dis[N]); if(dis[N] != INF) { if(max_len - min_len < min_h) { min_h = max_len - min_len ; min_d = dis[N] ; } else if(max_len - min_len == min_h) { if(min_d > dis[N]) min_d = dis[N] ; } } } } printf("%d %d\n",min_h,min_d); } return 0;}
- http://acm.hdu.edu.cn/showproblem.php?pid=2363
- http://acm.hdu.edu.cn/showproblem.php?pid=1005
- http://acm.hdu.edu.cn/showproblem.php?pid=1333
- http://acm.hdu.edu.cn/showproblem.php?pid=2710
- http://acm.hdu.edu.cn/showproblem.php?pid=2866
- http://acm.hdu.edu.cn/showproblem.php?pid=1041
- http://acm.hdu.edu.cn/showproblem.php?pid=1719
- http://acm.hdu.edu.cn/showproblem.php?pid=2136
- http://acm.hdu.edu.cn/showproblem.php?pid=1166
- http://acm.hdu.edu.cn/showproblem.php?pid=3519
- http://acm.hdu.edu.cn/showproblem.php?pid=2446
- http://acm.hdu.edu.cn/showproblem.php?pid=1709
- http://acm.hdu.edu.cn/showproblem.php?pid=1874
- http://acm.hdu.edu.cn/showproblem.php?pid=1863
- http://acm.hdu.edu.cn/showproblem.php?pid=1879
- http://acm.hdu.edu.cn/showproblem.php?pid=1875
- http://acm.hdu.edu.cn/showproblem.php?pid=1102
- http://acm.hdu.edu.cn/showproblem.php?pid=1162
- 一种三分频电路的实现与仿真
- c:foreach 详解
- 使用W5200 SPI和SPI+DMA模式下Arduino DUE以太网插板的测试对比
- 设置Android窗口透明
- GEF 进阶,第二部分: Router
- http://acm.hdu.edu.cn/showproblem.php?pid=2363
- C语言之排序算法---选择法排序
- Integrating Oracle OBIEE Content using GO URL
- 当JNI遇到多线程--java对象如何被C++中的多个线程访问?
- android布局常用属性
- Extjs给TextField赋值
- Hello World.
- jdbc获取自动生成键
- java sychronized