zz's Mysterious Present

Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1652    Accepted Submission(s): 371

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
 
Author
李光霞
题意: 有m个人,要到同一个地方(s)去获得一件东西,每个人都有个初始城市,每条边都是单向边,每个人都有个初始速度,要获得那件礼物的条件是:以到达时间最短者获胜,如果时间一样,那么则按照到s的距离,距离远者获胜,如果上诉条件都相同,那么编号大的人获得礼物.
题解:首先肯定是将边全部反向,然后以s点作为源点来进行最短路.求完最短路之后只要判断m个人所在的城市是否至少有一个<INF,不然都不可达,,我开始每想清,所有的城市
都去判断了,WA了好久.
AC代码:
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
const double eps = 1e-;
const int N = ;
const int INF = ;
int graph[N][N];
int p[N];
int speed[N];
int n,m,k;
int low[N];
bool vis[N];
double result[N];
int dijkstra(int s){
for(int i=;i<=n;i++){
low[i] = graph[s][i];
vis[i] = false;
}
low[s] = ;
vis[s] = true;
for(int i=;i<n;i++){
int Min = INF;
for(int j=;j<=n;j++){
if(Min>low[j]&&!vis[j]){
Min = low[j];
s = j;
}
}
vis[s] = true;
for(int j=;j<=n;j++){
if(low[j]>low[s]+graph[s][j]&&!vis[j]){
low[j] = low[s]+graph[s][j];
}
}
}
int flag = false;
///这里只要判断m个人就行了..
for(int i=;i<=m;i++){
if(low[p[i]]<INF) flag =true;
}
if(!flag) return INF;
int id = ;
for(int i=;i<=m;i++){
result[i] = low[p[i]]*1.0/speed[i];
if(result[id]>result[i]) id = i;
else if(fabs(result[id]-result[i])<eps){
if(low[p[id]]<=low[p[i]]) id = i;
}
}
return id;
}
int main()
{
while(scanf("%d%d%d",&n,&m,&k)!=EOF){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++) {
if(i==j) graph[i][j] = ;
else graph[i][j] = INF;
}
}
for(int i=;i<k;i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
graph[b][a] = min(c,graph[b][a]); ///全部反向
}
int s;
scanf("%d",&s);
for(int i=;i<=m;i++){
scanf("%d",&p[i]);
}
for(int i=;i<=m;i++){
scanf("%d",&speed[i]);
}
int id = dijkstra(s);
if(id>=INF) printf("No one\n");
else printf("%d\n",id);
}
}

hdu 2145(迪杰斯特拉)的更多相关文章

  1. hdu 1142(迪杰斯特拉+记忆化搜索)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  2. HDU 3339 In Action(迪杰斯特拉+01背包)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3339 In Action Time Limit: 2000/1000 MS (Java/Others) ...

  3. HDU 2544最短路 (迪杰斯特拉算法)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Time Limit: 5000/1000 MS (Java/Others)    Me ...

  4. HDU 3790(两种权值的迪杰斯特拉算法)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3790 最短路径问题 Time Limit: 2000/1000 MS (Java/Others)    ...

  5. HDU 1874畅通工程续(迪杰斯特拉算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others)     ...

  6. hdu 1595 find the longest of the shortest(迪杰斯特拉,减去一条边,求最大最短路)

    find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  7. hdu 3339 In Action(迪杰斯特拉+01背包)

    In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  8. HDU 2680 最短路 迪杰斯特拉算法 添加超级源点

    Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  9. hdu 1874 畅通工程续(迪杰斯特拉优先队列,floyd,spfa)

    畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

随机推荐

  1. Leetcode 872. 叶子相似的树

    题目链接 https://leetcode-cn.com/problems/leaf-similar-trees/description/ 题目描述 请考虑一颗二叉树上所有的叶子,这些叶子的值按从左到 ...

  2. SSH无密码登录及远程拷贝命令SCP的使用

    SSH无密码登录 1.生成密钥对(公钥和私钥) $ cd /home/cen/.ssh $ ssh-keygen -t rsa #生成密钥,使用rsa方式进行加密,四个回车 $ ssh-copy-id ...

  3. 设计模式之第3章-模板方法模式(Java实现)

    设计模式之第3章-模板方法模式(Java实现) "那个,上次由于我老婆要给我做饭,所以就没有说完就走掉了...这个那个".这次和以前一样,先来开场福利(工厂方法模式已被作者踹下场) ...

  4. IOS开发---菜鸟学习之路--(三)-数据解析

    第三篇 上一篇我们讲了如何通过NSURL类来获取数据, 这一章我们来讲下对于获取过来的数据如何解析. 好了直接进入正文吧. 正文: 上一篇讲了 我们获取过来的数据格式是JSON格式的 大家可以搜下对应 ...

  5. load_file()与into outfile函数详解

    load_file()函数的使用: 1.使用条件 ①有读取文件的权限 r and (select count(*) from mysql.user)>0 如果返回正常则说明有权限,反之没有 ②文 ...

  6. jmeter+ANT+Jekins性能自动生成测试报告脚本(模板),加入:Median TIme、90%、95%、99%、QPS、以及流量显示

    <?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/T ...

  7. Leetcode 654.最大二叉树

    最大二叉树 给定一个不含重复元素的整数数组.一个以此数组构建的最大二叉树定义如下: 二叉树的根是数组中的最大元素. 左子树是通过数组中最大值左边部分构造出的最大二叉树. 右子树是通过数组中最大值右边部 ...

  8. Spring框架配置beans.xml

    Spring学习笔记(一) 一.Spring 框架 Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时为 ...

  9. "R6002 floating point support not loaded"错误

    R6002 floating point support not loaded 错误,在Debug模式下会弹出如下错误: "floating point support not loaded ...

  10. 【bzoj3555】[Ctsc2014]企鹅QQ 字符串hash

    题目描述 PenguinQQ是中国最大.最具影响力的SNS(Social Networking Services)网站,以实名制为基础,为用户提供日志.群.即时通讯.相册.集市等丰富强大的互联网功能体 ...