HDU2145 zz's Mysterious Present 解题报告 ——最短路dijkstra算法
2013-10-31 00:27
363 查看
zz's Mysterious Present
Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 854 Accepted Submission(s): 171
Problem Description
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.
Input
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.
Output
For each case, output the one who get the present in one line. If no one can get the present, output "No one".
Sample Input
3 1 3
1 2 2
1 3 3
2 3 1
3
2
1
Sample Output
1
这道题在最短路的基础上加入了速度和时间,可以直接将终点P看为起点,便可以用Dijkstra解题。
因为题目说明所以路是有向的,所以输入时应注意顺序。
#include<stdio.h> #include<string.h> const long inf =999999999; int map[301][301]; double time[301]; int speed[301]; int pla[301]; int p; long dis[301]; void Dijkstra(int p,int n){ bool used[301]={false}; long min,next; memset(dis,-1,sizeof(dis)); dis[p]=0; for(long i=1;i<=n;i++) { min=inf; for(long j=1;j<=n;j++) if(!used[j] && dis[j]!=-1 && dis[j]<min) { min=dis[j]; next=j; } if(min!=inf) { used[next]=true; for(long j=1;j<=n;j++) if(!used[j] && map[next][j]!=-1 && (dis[j]>map[next][j]+dis[next] || dis[j]==-1)) dis[j]=map[next][j]+dis[next]; } } } int main(void){ int n,m,k;//city,peo,way int o,p,q;//xunhuan int a,b,c;//input while(scanf("%d %d %d",&n,&m,&k)!=EOF){ for(o=1;o<=n;++o) for(p=1;p<=n;++p) map[o][p]=inf; for(o=1;o<=k;o++){ scanf("%d %d %d",&a,&b,&c); if(map[b][a]>c){ map[b][a]=c; } } scanf("%d",&p); Dijkstra(p,n); long min=1; for(o=1;o<=m;++o) scanf("%d",&pla[o]); for(o=1;o<=m;++o) scanf("%d",&speed[o]); for(o=1;o<=m;++o){ time[o]=((double)(dis[pla[o]])/(double)(speed[o])); if(time[min]>time[o]) min=o; else if(time[min]==time[o]){ if(dis[pla[min]]<=dis[pla[o]]) min=o; } } if(dis[pla[min]]==inf) printf("No one\n"); else printf("%d\n",min); } return 0; }
相关文章推荐
- HDU2145 zz's Mysterious Present(最短路)
- HDU - 2145 zz's Mysterious Present (最短路)
- HDU1874 畅通工程续 解题报告--最短路dijkstra算法
- HDU 2145 zz's Mysterious Present 【多源单汇 + 反向建图最短路转换】
- Hdu 2145 zz's Mysterious Present 解题分析
- 团体程序设计天梯赛-练习集 L3-007. 天梯地图 最短路 dijkstra 解题报告
- 【ACM解题报告】Find Your Present 2
- BZOJ 4152 最短路(SPFA) 解题报告
- pku2243马棋到达最短路解题报告
- 解题报告 之 HDU2544 最短路
- hdu 2544 最短路 解题报告
- 洛谷 2737 麦香牛块 最短路SPFA? 解题报告
- USACO 2006 nov POJ3255 严格次短路 解题报告
- HDU 3339 In Action 解题报告(最短路+背包)
- Pku acm 1062 昂贵的聘礼 数据结构题目解题报告(七)—单源最短路径:Dijkstra算法
- [zz][ZOJ Monthly]October 2008解题报告
- Hdu 3986 【枚举】【最短路】 解题报告
- [ZZ]最短路的算法---Dijkstra算法
- HDU 3339 In Action 解题报告(最短路+背包)
- luogu解题报告:P1119灾后重建【图论/最短路】