Choose the best route hdu Hang Electric 2680 "Dijkstra algorithm | | SPFA "

Source: Internet
Author: User

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

Problem Descriptionone 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 your a map of the city ' s traffic route, and the stations which is near Kiki's home so, she can take. Suppose Kiki can change the bus at any station. Please find the least time Kiki needs to spend. To make it easy, if the city has n bus stations, the stations would been expressed as an integer 1,2,3...N.
Inputthere is 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 statio NS in this city and M stands for the number of directed ways between bus stations. (maybe there is several ways between, 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 and it'll 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.

Outputthe 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

Such a method can find the shortest path from multiple starting points. Save time

Dijkstra

#include <stdio.h> #include <string.h> #define INF 0x3f3f3f3fint map[1010][1010];int dis[20200];bool used[ 20200];int n;int e;int Dijkstra () {int I,j;memset (used,0,sizeof (used)); for (i=0;i<=n;++i) Dis[i]=inf;int pos;for (i= 0;i<=n;++i)//For the DIS assignment for the first time {dis[i]=map[0][i];} Dis[0]=0;used[0]=1;for (i=0;i<n;++i)//execute up to n times {int min=inf;for (J=0;J&LT;=N;++J) {if (!used[j]&&dis[j]< min) {min=dis[j];p os=j;}} Used[pos]=1;if (pos==e) return dis[pos];for (J=0;J&LT;=N;++J)//update the DIS array. Also called slack {if (!used[j]&&dis[j]>map[pos][j]+dis[pos]) {Dis[j]=map[pos][j]+dis[pos];}}} return-1;} int main () {int m,s,t;int u,v,w;int temp;int i,j;while (~scanf ("%d%d%d", &n,&m,&e)) {for (i=0;i<=n;++i) fo R (j=0;j<=i;++j) map[i][j]=map[j][i]=inf;for (i=1;i<=m;++i) {scanf ("%d%d%d", &u,&v,&w); if (map[u][ v]>w) Map[u][v]=w;} scanf ("%d", &t), for (I=1;i<=t;++i) {scanf ("%d", &temp), map[0][temp]=0;//0 points to the origin to be found}int Ans=dijkstra ();// Million Energy point 0 if (ans==-1) printf (" -1\n"), Else printf ("%d\n", ans);} return 0;}

Spfa


#include <cstdio> #include <cstring> #include <queue> #define MAXN 1100#define MAXM 22000#define INF 0x3f3f3f3fusing namespace Std;int map[maxn][maxn];int vis[maxn];//infers whether to increase the queue int num;int low[maxm];//Save Shortest Path int e;int M, N; void Spfa () {int I, j;queue<int> q;memset (Low, INF, sizeof), memset (Vis, 0, sizeof (VIS)); Vis[0] = 1;low[0] = 0;q . push (0); while (! Q.empty ()) {int u = q.front (); Q.pop (); Vis[u] = 0;//out of the queue. No queue becomes 0 for (i = 1; I <= N; ++i) {if (Low[i] > Low[u] + map[u][i]) {low[i] = Low[u] + map[u][i];if (!vis[i]) {vis[i]=1 ; Q.push (i); }}}}if (low[e] = = INF) printf (" -1\n"), Else printf ("%d\n", Low[e]);} int main () {int u, V, w;while (~scanf ("%d%d%d", &n, &m, &e)) {for (Int. i=0; i<=n;++i) for (int j=0;j<=i;++j) Map[i][j]=map[j][i]=inf;while (m--) {scanf ("%d%d%d", &u, &v, &w); if (map[u][v]>w)//must be sentenced to heavy map[u][v]=w;/ /map[u][v]=w;//map[v][u]=w;} int t,s;scanf ("%d", &t), while (t--) {scanf ("%d", &s), map[0][s]=0;//million Energy point}SPFA ();} return 0;}

Choose the best route hdu Hang Electric 2680 "Dijkstra algorithm | | SPFA "

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.