HDU 2145 zz's Mysterious Present
2013-10-31 08:49
357 查看
zz's Mysterious Present
Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 862 Accepted Submission(s): 174
[align=left]Problem Description[/align]
There are m people in n cities, and they all want to attend the party which hold by zz. They set out at the same time, and they all will choose the best way they think, but due to someone take a ride, someone drive, and someone take
a taxi, they have different speed. Can you find out who will get zz's mysterious present? The first one get the party will get the present . If there are several people get at the same time, the one who stay in the city which is farther from the city where
is zz at begin will get the present. If there are several people get at the same time and the distance from the city he is at begin to the city where zz is, the one who has the larger number will get the present.
[align=left]Input[/align]
The first line: three integers n, m and k. m is the total number of the people, and n is the total number of cities, and k is the number of the way.(0<n<=300, 0<m<=n, 0<k<5000)
The second line to the (k+1)th line: three integers a, b and c. There is a way from a to b, and the length of the way is c.(0<a,b<=n, 0<c<=100)
The (k+2)th line: one integer p(0<p<=n), p is the city where zz is.
The (k+3)th line: m integers. the ith people is at the place p[i] at begin.(0<p[i]<=n)
The (k+4)th line: m integers. the speed of the ith people is speed[i];(0<speed[i]<=100)
All the ways are directed.
[align=left]Output[/align]
For each case, output the one who get the present in one line. If no one can get the present, output "No one".
[align=left]Sample Input[/align]
3 1 3
1 2 2
1 3 3
2 3 1
3
2
1
[align=left]Sample Output[/align]
1
题目大意:m个人住在n个城市,他们都会选择最快的路去参加zz的聚会,已知m个人的起始点,速度和zz的地点,求出 最早到的人的序号,如果有同时到的,则取距离远的,如果距离相同则取序号大的.所有路是单向的
[align=left]思路:将终点当作起点然后一次Dijkstra,就可以求出所有点到终点的距离,因为对所有边做了反向处理,所以输入时也要反向输入.需要注意的是所有路是单项的.[/align]
[align=left]
[/align]
[align=left]
[/align]
#include<stdio.h> #define MAX 0xffffff int select[1000], dis[1000], map[1000][1000]; void ShortPath(int s, int n) //dij算法模版 { int i, j, k, min; for( i = 1; i <= n; i++) dis[i] = map[s][i]; dis[s] = 0; select[s] = 1; for( i = 2; i <= n; i++) { k = -1, min = MAX; for( j = 1; j <= n; j++) if(select[j] != 1 && dis[j] < min) { k = j; min = dis[j]; } if(k == -1) break; select[k] = 1; for( j = 1; j <= n; j++ ) if( select[j] != 1 && dis[k] + map[k][j] < dis[j]) dis[j] = dis[k] + map[k][j]; } } int main() { int n, m, k, p, place[1000]; int x, y, s, flag = 0, min; float speed[1000], result[1000]; while( scanf("%d%d%d", &n, &m, &k) != EOF) { int i, j; for( i = 1; i <= n; i++) { dis[i] = MAX; select[i] = 0; for( j = 1; j <= n; j++) { if(i == j) map[i][j] = 0; else map[i][j] = MAX; } } while(k--) { scanf("%d%d%d", &x, &y, &s); //反向并单向输入 if(map[y][x] > s) map[y][x] = s; } scanf("%d", &p); ShortPath( p, n); for( i = 1; i <= m; i++) scanf("%d", &place[i]); for( i = 1; i <= m; i++) scanf("%f", &speed[i]); min = 1; for( i = 1; i <= m; i++) //序号筛选 { result[i] = dis[place[i]]*1.0 / speed[i]; if(result[min] > result[i]) min = i; else if(result[min] == result[i]) { if(dis[place[min]] <= dis[place[i]]) min = i; } } if( dis[place[min]] == MAX) printf("No one\n"); else printf("%d\n", min); } return 0; }
相关文章推荐
- 简单的四则运算
- 数的奇偶性
- Dijkstra和floyd——求单源点最短路径
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- 求两个数的最大公约数【ACM基础题】
- 打印出二进制中所有1的位置
- 杭电题目---一只小蜜蜂
- zoj3549 快速幂
- NWERC2010 NKOJ2178 Stock Prices
- 2011ACM福州网络预选赛B题 HDU4062 Abalone
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- HDU 1405
- HDU 1297
- hdu 1205
- hdu 2087
- hdu 1016
- ACM常用算法