题意:小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(多起点单终点最短路问题)题解的更多相关文章

  1. 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 ( ...

  2. hdu 2680 Choose the best route

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Description One day , Kiki ...

  3. HDU 2680 Choose the best route 最短路问题

    题目描述:Kiki想去他的一个朋友家,他的朋友家包括所有的公交站点一共有n 个,一共有m条线路,线路都是单向的,然后Kiki可以在他附近的几个公交站乘车,求最短的路径长度是多少. 解题报告:这道题的特 ...

  4. HDU 2680 Choose the best route(SPFA)

    Problem DescriptionOne day , Kiki wants to visit one of her friends. As she is liable to carsickness ...

  5. hdu 2680 Choose the best route (dijkstra算法)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2680 /************************************************* ...

  6. hdu 2680 Choose the best route 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目意思:实质就是给定一个多源点到单一终点的最短路. 卑鄙题---有向图.初始化map时 千万不 ...

  7. HDU 2068 Choose the best route

    http://acm.hdu.edu.cn/showproblem.php?pid=2680 Problem Description One day , Kiki wants to visit one ...

  8. hdoj 2680 choose the best route

    Problem Description One day , Kiki wants to visit one of her friends. As she is liable to carsicknes ...

  9. 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 ...

随机推荐

  1. ubuntu 关闭显示器的命令

    sleep 2s ; gnome-screensaver-command -a或sleep 2s ; xset dpms force off2秒后关闭显示器

  2. JQuery事件e参数的方法preventDefault()取消默认行为

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  3. virgo-tomcat访问日志的详细配置

    Tomcat 日志信息分为两类:1.运行中的日志,它主要记录运行的一些信息,尤其是一些异常错误日志信息.2.访问日志信息,它记录的访问的时间.IP.访问的资料等相关信息. 关于tomcat访问日志的产 ...

  4. jquery收集表单数组及去掉字符串最后的逗号!

    jquery收集表单数组: <input type='text' name='one[]' value='' /><br> <input type='text' name ...

  5. [Android] 配置安卓模拟器,使得dex文件不被优化成odex

    最近做一个模块,需要将apk里面加载的dex文件dump出来,所以需要配置让dalvik不要把dex文件优化成odex. 1. 配置build.prop 主要是通过修改文件/system/build. ...

  6. ASP.NET的页面执行过程

    对于ASP.NET来说,用户访问的页面,都由服务器IIS处理,具体的处理过程如下图: 对于用户模块还是有很多的东西没有写,未完待续...

  7. 40个DBA日常维护的SQL脚本

    1.查询碎片程度高的表 条件为什么block>100,因为一些很小的表,只有几行数据实际大小很小,但是block一次性分配就是5个(11g开始默认一次性分配1M的block大小了,见create ...

  8. Create an Index

    db.collection.createIndex( { name: -1 } ) Indexes — MongoDB Manual https://docs.mongodb.com/manual/i ...

  9. 设计模式之——Composite模式

    composite模式又叫做组合模式/复合模式. 它是一种能够使容器与内容具有一致性,创造出递归结构的模式. 示例程序是列出文件夹以及其内部文件与文件夹一览的功能: 可以由示例图看出,有一个电影文件夹 ...

  10. 如何在window的location使用target

    在页面中window的location跳转时,指定页面在框架中跳转 1. 如果你要让最顶层的框架跳转,就是整个页面,相当于用traget指向顶层 window.top.location   =   & ...