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 ...
随机推荐
- 在腾讯云服务器上体验Docker
版权声明:本文由姚俊刚原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/85 来源:腾云阁 https://www.qclou ...
- 学习POC框架pocsuite--编写hellowordPOC
在这里,首先向安全圈最大的娱乐公司,某404致敬. 参考博文 https://www.seebug.org/help/dev 向seebug平台及该文原作者致敬,虽然并不知道是谁 长话短说其实,可自由 ...
- EUI组件之HScrollBar VScrollBar (动态设置滑块图片)
一.常规使用 官网教程里没有这个组件的使用方法 这个组件配合Scroller使用 拖动一个scroller到exml上.scroller上已经默认存在了HScrollBar和VScrollBar 当图 ...
- c# 给button添加不规则的图片以及用pictureBox替代button响应点击事件
1.Flat button 用这个方法,前提是要把button的type设置为Flat button1.TabStop = false;button1.FlatAppearance.BorderSiz ...
- Thinkphp --- 去掉index.php
这里我使用的面板是宝塔,操作的 apche: 具体的配置可以参考这里: https://www.cnblogs.com/fangziffff123/p/7588782.html 首先是:Thinkph ...
- numpy基础入门
1.Numpy是什么 很简单,Numpy是Python的一个科学计算的库,提供了矩阵运算的功能,其一般与Scipy.matplotlib一起使用.其实,list已经提供了类似于矩阵的表示形式,不过nu ...
- yii2 的 Url::to() 和 Url::toRoute()
关于Url类的操作在这个页面http://www.yiichina.com/doc/guide/2.0/helper-url: Url::to() 和 toRoute() 非常类似.这两个方法的唯一区 ...
- Centos 系统Java环境安装
Java安装 安装SUN的JDK: 官网:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260. ...
- Linux入门之常用命令(15) lsof
查看磁盘空间: [root@ticketb ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 981M 203M 729M ...
- EA类图与代码同步
画了一段时间的图,愈发感觉到EA是一个强大的软件,而不不过一个绘图工具那么简单. . 随着学习时间的延长.如今写程序并不能像曾经一样随心所欲,想到什么就敲什么了,而是要先绘图(也就是理需求和思路的过程 ...