hdu 2680
来源:互联网 发布:通达信软件官网 编辑:程序博客网 时间:2024/05/17 22:51
Choose the best route
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 15495 Accepted Submission(s): 5028
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.
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 51 2 21 5 31 3 42 4 72 5 62 3 53 5 14 5 122 34 3 41 2 31 3 42 3 211
Sample Output
1-1
题意:有n个站,m条路线,目的地是s点,出发点有w个,求到达目的地最短距离。
思路:典型的单源最短路径,直接套用spfa模板。
代码:
#include <iostream>#include <stdio.h>#include <string.h>#include <vector>#include <queue>#include <algorithm>using namespace std;#define inf 0x3f3f3f3fstruct edge{ int to,w;};vector<edge>g[1005];int dis[1005];bool flag[1005];int n;void spfa(int u){ int i; for(i=0;i<=n;i++) { dis[i]=inf; flag[i]=false; } dis[u]=0; flag[u]=true; queue<int>q; q.push(u); while(!q.empty()) { u=q.front(); flag[u]=false; q.pop(); for(i=0;i<g[u].size();i++) { int x=g[u][i].to; int y=g[u][i].w; if(dis[x]>dis[u]+y) { dis[x]=dis[u]+y; if(!flag[x]) { flag[x]=true; q.push(x); } } } }}int main(){ int m,k; while(scanf("%d %d %d",&n,&m,&k)!=EOF) { int i; for(i=0;i<=n;i++) g[i].clear(); for(i=0;i<m;i++) { int x,y,z; scanf("%d %d %d",&x,&y,&z); edge e; e.to=y; e.w=z; g[x].push_back(e); } int s; scanf("%d",&s); for(i=0;i<s;i++) { int a; scanf("%d",&a); edge e; e.to=a; e.w=0; g[0].push_back(e); } spfa(0); if(dis[k]==inf) { cout<<"-1"<<endl; } else cout<<dis[k]<<endl; } return 0;}
阅读全文
0 0
- HDU 2680
- HDU 2680
- HDU 2680
- hdu 2680
- hdu 2680 简单 dijstra
- 【spfa】 hdu 2680
- HDU 2680(spfa)
- HDU 2680 最短路
- hdu 2680 最短路
- hdu 2680(dijkstra)
- HDU 2680 最短路
- hdu 2680 最短路
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- 数据库表及其字段变量命名神器之codeIf
- Leetcode之Reshape the Matrix 问题
- 问答机器人的Python分类
- FME常用函数
- python字符串
- hdu 2680
- SQL函数总结
- POJ 3384 Feng Shui (半平面交)
- Servlet 获取ServletContext对象
- sublime 常用功能
- jvm 垃圾回收
- 欢迎使用CSDN-markdown编辑器
- 习题8.4 畅通工程之最低成本建设问题(30 分)
- 我的matlab最基本用法(1)