HDU 2680 Choose the best route(SPFA)
Problem Description
One day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arrive at her friend’s home as soon as possible . Now give you a map of the city’s traffic route, and the stations which are near Kiki’s home so that she can take. You may suppose Kiki can change the bus at any station. Please find out the least time Kiki needs to spend. To make it easy, if the city have n bus stations ,the stations will been expressed as an integer 1,2,3…n.
Input
There are several test cases.
Each case begins with three integers n, m and s,(n<1000,m<20000,1=<s<=n) n stands for the number of bus stations in this city and m stands for the number of directed ways between bus stations .(Maybe there are several ways between two bus stations .) s stands for the bus station that near Kiki’s friend’s home.
Then follow m lines ,each line contains three integers p , q , t (0<t<=1000). means from station p to station q there is a way and it will costs t minutes .
Then a line with an integer w(0<w<n), means the number of stations Kiki can take at the beginning. Then follows w integers stands for these stations.
Output
The output contains one line for each data set : the least time Kiki needs to spend ,if it’s impossible to find such a route ,just output “-1”.
Sample Input
5 8 5
1 2 2
1 5 3
1 3 4
2 4 7
2 5 6
2 3 5
3 5 1
4 5 1
2
2 3
4 3 4
1 2 3
1 3 4
2 3 2
1
1
Sample Output
1
-1
题意
kiki想坐公交车去朋友家,给你公交车线路图(有向),kiki有w个地方可以坐共交车,问到朋友家的最短路,若不能到输出-1
题解
这题就是kiki从w个起点坐到终点的最短路,算是单源最短路,可以每个起点跑一遍SPFA(这样太费时间了)
假如我们把kiki家看成出发点0,可以到w个距离为0的公交站,这样我们就把多个起点归1了,跑一遍SPFA就行了
代码
- #include<bits/stdc++.h>
- using namespace std;
- #define INF 0x3f3f3f3f
- typedef pair<int,int> pii;
- vector<pii> G[];
- int Vis[],Dist[];
- void spfa()
- {
- memset(Vis,,sizeof(Vis));
- memset(Dist,INF,sizeof(Dist));
- queue<int> Q;
- Q.push();
- Dist[]=;//从家里0出发
- while(!Q.empty())
- {
- int u=Q.front();Q.pop();
- Vis[u]=;
- for(int i=;i<G[u].size();i++)
- {
- int v=G[u][i].first,w=G[u][i].second;
- if(Dist[v]>Dist[u]+w)
- {
- Dist[v]=Dist[u]+w;
- if(Vis[v]==)
- {
- Vis[v]=;
- Q.push(v);
- }
- }
- }
- }
- }
- int main()
- {
- int n,m,s,k,u,v,w;
- while(scanf("%d%d%d",&n,&m,&s)!=EOF)
- {
- for(int i=;i<m;i++)
- {
- scanf("%d%d%d",&u,&v,&w);
- G[u].push_back(pii(v,w));
- }
- scanf("%d",&k);
- for(int i=;i<k;i++)
- {
- scanf("%d",&v);
- G[].push_back(pii(v,));//从家到公交站距离为0
- }
- spfa();
- printf("%d\n",Dist[s]==INF?-:Dist[s]);
- for(int i=;i<n;i++)
- G[i].clear();
- }
- return ;
- }
HDU 2680 Choose the best route(SPFA)的更多相关文章
- 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 (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 (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 2680 Choose the best route 最短路问题
题目描述:Kiki想去他的一个朋友家,他的朋友家包括所有的公交站点一共有n 个,一共有m条线路,线路都是单向的,然后Kiki可以在他附近的几个公交站乘车,求最短的路径长度是多少. 解题报告:这道题的特 ...
- HDU 2680 Choose the best route(多起点单终点最短路问题)题解
题意:小A要乘车到s车站,他有w个起始车站可选,问最短时间. 思路:用Floyd超时,Dijkstra遍历,但是也超时.仔细看看你会发现这道题目好像是多源点单终点问题,终点已经确定,那么我们可以直接转 ...
- 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 ...
- Choose the best route(最短路)dijk
http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Time Limit: 2000/1000 MS (Java/ ...
随机推荐
- Boolean类型
Boolean类型是与布尔值对应的引用类型.如果要创建Boolean对象,语法如下: var booleanObject = new Boolean(true); Boolean类型的实例重写了val ...
- java web项目使用IDEA打成war包
步骤: 1.点击 File -->Project Structure...如下图: 2.出现如下界面后点击 Artifacts--> 绿色加号-->Web Application:A ...
- JQuery中bind和unbind函数与onclick绑定事件区分
JQuery中bind和unbind函数转载: https://blog.csdn.net/liucheng417/article/details/51131982 页面代码: <body& ...
- Crash 文件调试
Xcode目录下执行 find . -name symbolicatecrash 找到symbolicatecrash位置,将其拷贝到debug用的文件夹下 执行命令 export DEVELOPER ...
- Linux:写一个简单的服务器
开始了新篇章:Linux网络编程. 基础知识: 套接字概念 Socket本身有"插座"的意思,在Linux环境下,用于表示进程间网络通信的特殊文件类型.本质为内核借助缓冲区形成的伪 ...
- 发现一个好办法-有问题可以到UNITY论坛搜索
特别专业的问题,较新技术,可以到UNITY论坛搜索或发问,那里,或许会有UNITY的官方技术支持回答 https://forum.unity.com/threads/remote-deep-profi ...
- cvc-complex-type.2.4.a: Invalid content was found starting with element 'async-supported'. One of '{"http://java.sun.com/xml/ns/javaee":init-param}' is expected.
第一种方案: 将 "http://java.sun.com/xml/ns/javaee" 换为 "http://java.sun.com/xml/ns/j2ee& ...
- 使用karma做多浏览器的UI测试
avalon1.6开发得差不多,这次使用先进的开发理念进行开发,比如模块化,单元测试什么... ui测试是重要的一环,之前用阿里的totoro,但打开浏览器不方便.于是从webdrieverio, n ...
- fopen函数出现段错误
昨天写代码的时候突然发现了一个问题,当使用fopen("<filepath>", "r")时,如果filepath不存在,那么fopen函数并不是像 ...
- PL/SQL编码规范的一些建议
由于业务的复杂多变,我们编写完的程序,在后期肯定要被修改,而且修改的人很可能不是自己.这种情况我们都遇到过. 而且,看别人的代码可能会觉得很痛苦:为什么他要这样写相关逻辑?为什么变量名称要这样定义?换 ...