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 ...
随机推荐
- vscode中的vue文件中emmet进行tab键不起作用
文件--首选项---设置 搜索: emmet.includeLanguages在右边修改 "emmet.triggerExpansionOnTab": true, "em ...
- bootstrap之辅助类
类 描述 .pull-left 元素浮动到左边 .pull-right 元素浮动到右边 .center-block 设置元素为 display:block 并居中显示 .clearfix 清除浮动 . ...
- 【BZOJ2957】楼房重建 分块
[BZOJ2957]楼房重建 Description 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子 ...
- Css--input输入框点击时去掉外框outline:medium;(chrome)
.search input[type='search']{ background:no-repeat 0 0 scroll #EEEEEE; border:none; outline:medium; ...
- kafka简介【转】
一.为什么需要消息系统 () 解耦 在项目启动之初来预测将来项目会碰到什么需求,是极其困难的.消息系统在处理过程中间插入了一个隐含的.基于数据的接口层,两边的处理过程都要实现这一接口.这允许你独立的扩 ...
- 基于JDK1.8的String源码学习笔记
String,可能是学习Java一上来就学习的,经常用,但是却往往只是一知半解,甚至API有时也得现查.所以还是老规矩,倒腾源码. 一.java doc 这次首先关注String的doc,因为其实作为 ...
- pta 习题集 5-2 找出不是两个数组共有的元素 (5分)
给定两个整型数组,本题要求找出不是两者共有的元素. 输入格式: 输入分别在两行中给出两个整型数组,每行先给出正整数NN(≤20≤20),随后是NN个整数,其间以空格分隔. 输出格式: 在一行中按照数字 ...
- python redis基本概念简单操作
转自:http://www.cnblogs.com/melonjiang/p/5342383.html 一.redis redis是一个key-value存储系统.和Memcached类似,它支持存储 ...
- Django - 常用配置
一.logging配置 Django项目常用的logging配置 settings.py LOGGING = { 'version': 1, 'disable_existing_loggers': F ...
- Tickets---hdu1260 (简单dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1260 题意就是有n个人排队买票,每个人需要的时间是a[i] (1=< i <=N),但是现 ...