hdu 2145(迪杰斯特拉)

本文介绍了一个关于寻找最快路径以获得神秘礼物的算法问题。该问题涉及多名参赛者从不同起点出发,通过单向路径到达目的地,依据最先抵达且满足特定条件者获胜。文章详细阐述了解题思路及实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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-8;
const int N = 305;
const int INF = 99999999;
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=1;i<=n;i++){
        low[i] = graph[s][i];
        vis[i] = false;
    }
    low[s] = 0;
    vis[s] = true;
    for(int i=1;i<n;i++){
        int Min = INF;
        for(int j=1;j<=n;j++){
            if(Min>low[j]&&!vis[j]){
                Min = low[j];
                s = j;
            }
        }
        vis[s] = true;
        for(int j=1;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=1;i<=m;i++){
        if(low[p[i]]<INF) flag =true;
    }
    if(!flag) return INF;
    int id = 1;
    for(int i=1;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=1;i<=n;i++){
            for(int j=1;j<=n;j++) {
                if(i==j) graph[i][j] = 0;
                else graph[i][j] = INF;
            }
        }
        for(int i=0;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=1;i<=m;i++){
            scanf("%d",&p[i]);
        }
        for(int i=1;i<=m;i++){
            scanf("%d",&speed[i]);
        }
        int id = dijkstra(s);
        if(id>=INF) printf("No one\n");
        else printf("%d\n",id);
    }
}

 

转载于:https://www.cnblogs.com/liyinggang/p/5502922.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值