HDU 2680Choose the best route (SPFA)
来源:互联网 发布:部落冲突防空升级数据 编辑:程序博客网 时间:2024/06/10 14:42
转载请注明出处:http://blog.csdn.net/u012860063
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680
Choose the best route
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6285 Accepted Submission(s): 2067
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
Author
dandelion
Source
2009浙江大学计算机研考复试(机试部分)——全真模拟
Recommend
lcy | We have carefully selected several similar problems for you: 2112 1217 2066 1142 1385
注意:此题道路是单向的!
SPFA+反向寻找!
代码如下:
#include <cstdio>#include <cmath>#include <cstring>#include <cstdlib>#include <climits>#include <malloc.h>#include <ctype.h>#include <queue>#include <stack>#include <vector>#include <deque>#include <set>#include <map>#include <iostream>#include <algorithm>using namespace std;#define pi acos(-1.0)#define INF 0xffffff#define N 1047#define M 20047int n,m,s,pp,ww,k,Edgehead[M],dis[M];struct{int v,w,next;}Edge[2*M];bool vis[M];void Addedge(int u,int v,int w){Edge[k].next = Edgehead[u];Edge[k].w = w;Edge[k].v = v;Edgehead[u] = k++;}void SPFA( int start){queue<int>Q;for(int i = 1 ; i <= N ; i++ )dis[i] = INF;dis[start] = 0;memset(vis,false,sizeof(vis));Q.push(start);while(!Q.empty())//直到队列为空{int u = Q.front();Q.pop();vis[u] = false;for(int i = Edgehead[u] ; i!=-1 ; i = Edge[i].next)//注意{int v = Edge[i].v;int w = Edge[i].w;if(dis[v] > dis[u] + w){dis[v] = dis[u]+w;if( !vis[v] )//防止出现环,也就是进队列重复了 {Q.push(v);vis[v] = true;}}}}}int main(){int u,v,w;while(~scanf("%d%d%d",&n,&m,&s)){k = 1;memset(Edgehead,-1,sizeof(Edgehead));for(int i = 1 ; i <= m ; i++ ){scanf("%d%d%d",&u,&v,&w);//Addedge(u,v,w);Addedge(v,u,w);//因为是反向寻找的而且道路是单向的,所以此处也要方向}SPFA(s);scanf("%d",&pp);int min = INF; for(i = 0 ; i < pp ;i++){scanf("%d",&ww);if(min > dis[ww])min = dis[ww];}if(min == INF)min = -1;printf("%d\n",min);}return 0;}
//有时正向不对,不防反向试试(生活亦如此)!
再附一份dijstra:
#include<stdio.h>#define max 1001#define INF 100000000int N;int v[max];int dis[max];int g[max][max];void dijk(){int i,j,mark,mindis;for(i=0;i<=N;i++){v[i] = 0;dis[i] = INF;}dis[0] = 0;v[0] = 1;for(i=0;i<=N;i++){mindis = INF;mark = 0;for(j=0;j<=N;j++)if(!v[j] && dis[j] < mindis){mindis = dis[j];mark = j;}v[mark] = j;for(j=0;j<=N;j++)if(!v[j] && dis[j] > dis[mark] + g[mark][j])dis[j] = dis[mark] + g[mark][j];}}int main(){int W,M,Q,i,j,d,a,b;while(~scanf("%d%d%d",&N,&M,&Q)){for(i=0;i<max;i++)for(j=0;j<max;j++){if(i==j)g[i][j] = 0;elseg[i][j] = INF;}while(M--){scanf("%d%d%d",&a,&b,&d);if(d < g[a][b])g[a][b] = d;}scanf("%d",&W);while(W--){scanf("%d",&a);g[0][a] = 0;}dijk();if(dis[Q] == INF)printf("-1\n");elseprintf("%d\n",dis[Q]);}}
0 0
- HDU-2680 Choose the best route(SPFA)
- HDU-2680-Choose the best route【spfa】
- HDU 2680Choose the best route (SPFA)
- hdu 2680 Choose the best route(spfa/dijkstra)
- hdu 2680 Choose the best route(SPFA或者Dijkstra)
- HDU 2680:Choose the best route【Dijkstra & SPFA】
- hdu 2680 Choose the best route (SPFA算法)
- #HDU 2680 Choose the best route 【SPFA最短路】
- HDU 2680 Choose the best route【多源最短路,Dijkstra+spfa】
- Choose the best route(HDU-2680)
- HDOJ 题目2680 Choose the best route(spfa)
- hdu 2680 Choose the best route (最短路问题 dijkstra | spfa)
- HDU 2680 Choose the best route
- HDU-2680 choose the best route
- Hdu-2680 Choose the best route
- hdu 2680 Choose the best route
- HDU 2680 Choose the best route
- HDU 2680-Choose the best route
- 使用Java Base64解密算对openssl的base64加密字符串进行解密
- Android中onTouch返回值含义
- 计算机视觉 资源 链接
- <PY><core python programming笔记>C6 序列:字符串和列表和元组(二)
- Java 高新技术(反射)
- HDU 2680Choose the best route (SPFA)
- Android使用MID声音资源
- C++多线程编程
- Java基本数据类型的【取值范围】【占内存大小】【默认值】
- 在Ubuntu上价格SVN服务器
- vi批量替换文本中的字符串
- 数据结构学习笔记之堆栈
- SSH+Oracle 整合错误之5 问题好像吃了炫迈 ,根本停不下来 java.util.NoSuchElementException
- java-IO流