HDU 2680 Choose the best route(多起点单终点最短路问题)题解
题意:小A要乘车到s车站,他有w个起始车站可选,问最短时间。
思路:用Floyd超时,Dijkstra遍历,但是也超时。仔细看看你会发现这道题目好像是多源点单终点问题,终点已经确定,那么我们可以直接转置邻接矩阵,从终点找最小的起点,转换成了单源最短路问题。
代码:
#include<cstdio>
#include<set>
#include<cmath>
#include<stack>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn = +;
const int INF = 0x3f3f3f3f;
int mp[maxn][maxn];
int dis[maxn];
int vis[maxn];
int n,m;
void dijkstra(int st){
memset(vis,,sizeof(vis));
memset(dis,INF,sizeof(dis));
dis[st] = ;
for(int i = ;i <= n;i++){
int Min = INF,k = ;
for(int j = ;j <= n;j++){
if(!vis[j] && dis[j] < Min){
Min = dis[j];
k = j;
}
}
vis[k] = ;
for(int j = ;j <= n;j++){
if(dis[j] > dis[k] + mp[k][j]){
dis[j] = dis[k] + mp[k][j];
}
}
}
} int main(){
int s;
while(scanf("%d%d%d",&n,&m,&s) != EOF){
memset(mp,INF,sizeof(mp));
while(m--){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
mp[v][u] = min(mp[v][u],w);
}
dijkstra(s);
int ans = INF;
scanf("%d",&m);
while(m--){
int u;
scanf("%d",&u);
ans = min(ans,dis[u]);
}
if(ans == INF) printf("-1\n");
else printf("%d\n",ans);
}
return ;
}
HDU 2680 Choose the best route(多起点单终点最短路问题)题解的更多相关文章
- hdu 2680 Choose the best route (dijkstra算法 最短路问题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Time Limit: 2000/1000 MS ( ...
- hdu 2680 Choose the best route
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Description One day , Kiki ...
- HDU 2680 Choose the best route 最短路问题
题目描述:Kiki想去他的一个朋友家,他的朋友家包括所有的公交站点一共有n 个,一共有m条线路,线路都是单向的,然后Kiki可以在他附近的几个公交站乘车,求最短的路径长度是多少. 解题报告:这道题的特 ...
- HDU 2680 Choose the best route(SPFA)
Problem DescriptionOne day , Kiki wants to visit one of her friends. As she is liable to carsickness ...
- hdu 2680 Choose the best route (dijkstra算法)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2680 /************************************************* ...
- hdu 2680 Choose the best route 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目意思:实质就是给定一个多源点到单一终点的最短路. 卑鄙题---有向图.初始化map时 千万不 ...
- HDU 2068 Choose the best route
http://acm.hdu.edu.cn/showproblem.php?pid=2680 Problem Description One day , Kiki wants to visit one ...
- hdoj 2680 choose the best route
Problem Description One day , Kiki wants to visit one of her friends. As she is liable to carsicknes ...
- HDU 2612 Find a way【多起点多终点BFS/两次BFS】
Find a way Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
随机推荐
- 手机QQ会员H5加速方案——sonic技术内幕
版权声明:本文由况鹰原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/141 来源:腾云阁 https://www.qclou ...
- mysql优化之explain备忘笔记
今天使用explain来查看sql执行情况的时候发现有的东西忘掉了,故作此篇文章来强化此知识点的记忆. 1.explain作用 exlain 执行结果显示了mysql 存储引擎如何使用索引来处理sel ...
- R中的各种概率统计分布
名称 名称 R对应的名字 附加参数 β分布 beta beta shape1, shape2, ncp 二项式分布 binomial binom size, prob 柯西分布 Cauchy cauc ...
- Linux系统下 Rsync 环境安装搭建
一.Rsync简介 1.认识 Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.Rsync使用所谓的“Rsync算法”来使本地和远 ...
- ubuntu win7双系统设置开机启动顺序
ctrl+alt+t打开命令终端 sudo chmod +w /boot/grub/grub.cfg (赋予该文件写权限) sudo gedit /boot/grub/grub.cfg 将set de ...
- Git之删除仓库
Github删除已有仓库步骤 在仓库页面点击设置 在新打开网页删除 输入仓库名点击删除即可
- poj3974 Palindrome【回文】【Hash】【二分】
Palindrome Time Limit: 15000MS Memory Limit: 65536K Total Submissions: 13157 Accepted: 5028 Desc ...
- 集成tomcat插件到eclipse
Eclipse中Tomcat的配置及简单例子 环境: 系统: Windows 7 64位专业版 Eclipse: Eclipse Java EE IDE for Web Developers. ...
- 您好,前端使用https,后端使用https是会有冲突的情况,所以默认后端都是http 负载均衡即可管理证书,不需要在后端ECS上绑定证书。
您前端使用https,那么前端就是加密的,后端使用https就是会访问出现问题的,目前阿里云负载均衡默认的配置前端使用https,后端默认就是http,也是无法更改的. 前端使用https,目前只有一 ...
- Fundamental theorem of arithmetic 为什么1不是质数
https://en.wikipedia.org/wiki/Fundamental_theorem_of_arithmetic In number theory, the fundamental th ...