A man's travels. Hdu Hangzhou Electric 2066 "Dijkstra algorithm | | SPFA "

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php?

pid=2066


Problem description Although grass son is a road fetish (that is, in Hangzhou electric for more than a year, unexpectedly still in the campus lost people.) Sweat ~), but the grass is still very fond of travel, because on the trip will meet a lot of people (Prince Charming. ^0^), very much, but also can enrich their experience. Also can see beautiful scenery ... The grass wants to go to very many places. She wants to see the night view of the Tokyo Tower, go to Venice to see a movie, and see the Taro on Yangmingshan. Go to New York and see the snow. Go to Paris to drink coffee and write letters. Go to Beijing to visit Meng Jiangnu ... See winter vacation soon, so a large period of time, can not be wasted ah, must give oneself good put a false, but also can not neglect training ah, so grass son decided in the shortest time to a place where they want to go!

As the grass's home is in a small town, there is no train passing by. So she can only go to the neighboring city by train (poor Ah ~).


Input data has more than one group, the first line of each group is three integers t,s and D, indicating there is a T road, and the grass home adjacent to the city has s. Where the grass wants to go there are D;
Then there's t-line. Each line has three integer a,b,time, indicating that the drive between A and B cities is time hours; (1=< (A, B) there may be multiple paths between <=1000;a,b)
The next t+1 line has s number. A city that is connected with the grass family;
The next line of T+2 has a number D, indicating that the grass wants to go to the place.


Output outputs the shortest time a grass can go to a favorite city.
Sample Input
6 2 31 3 51 4 72 8 123 8 44 9 129 10 21 28 9 10

Sample Output
9

Dijkstra

#include <stdio.h> #include <string.h> #define INF 0x3f3f3f3fint map[1010][1010];int dis[1010];bool used[ 1010];int n;int i,j;void Dijkstra () {int I,j;memset (used,0,sizeof (used)); for (i=0;i<=1000;++i) Dis[i]=inf;int pos; for (i=0;i<=1000;++i)//For the DIS assignment for the first time {dis[i]=map[0][i];} Dis[0]=0;used[0]=1;for (i=0;i<1000;++i)//execute up to n times {int min=inf;for (J=0;J&LT;=1000;++J) {if (!used[j]&&dis[ J]<min) {min=dis[j];p os=j;}} Used[pos]=1;dis[pos]=min;for (J=0;J&LT;=1000;++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];}}}} int main () {int m,t;int u,v,w;int temp;int a[1010],b[1010];while (~scanf ("%d%d%d", &n,&m,&t)) {for (i=0;i <=1000;++i) for (j=0;j<=i;++j) map[i][j]=map[j][i]=inf;for (i=1;i<=n;++i) {scanf ("%d%d%d", &u,&v, &AMP;W); if (map[u][v]>w) map[u][v]=map[v][u]=w;} for (i=1;i<=m;++i) {scanf ("%d", &temp); map[0][temp]=0;} Dijkstra (); int min=inf;for (i=1;i<=t;++i) {scanf ("%d", &temp); if (dis[temp]<min) min=dis[temp];} printf ("%d\n", Min);} return 0;}

SPFS


#include <cstdio> #include <cstring> #include <queue> #define MAXN 1100#define MAXM 10100#define INF 0x3f3f3f3fusing namespace Std;struct edge{int u, V, w;int next;//next structure variable subscript}edge[maxm];int head[maxn];//subscript as Start U,  The value for the corresponding struct subscript int vis[maxn];//infers whether to increase the queue int num;int low[maxn];//Save Shortest path void Add_edge (int u, int v, int w)//plus edge {edge e={u, V, W, HEAD[U]};//initialization Structure edge[num]=e;//direct assignment of head[u]=num++;} void SPFA (int s) {int i, j;queue<int> q;memset (Low, INF, sizeof); memset (Vis, 0, sizeof (VIS)); Vis[s] = 1;low[s]= 0; Q.push (s); while (! Q.empty ()) {int U=q.front (); Q.pop (); vis[u]=0;//out of the queue, not in the queue becomes 0 for (j = head[u]; j =-1; j = edge[j].next) {int v = edge[j].v;if (Low[v] > Low[u] + edg E[J].W) {Low[v] = Low[u] + edge[j].w;if (!vis[v]) {vis[v]=1; Q.push (v); }}}}}int Main () {int u, V, w;int s,e,m,n,s,e;while (~scanf ("%d%d%d", &n, &s,&e)) {memset (head,-1, sizeof (head )); num=0;for (int i=1; I <= N; ++i) {scanf ("%d%d%d", &u, &v, &w); Add_edge (U, V, W); Add_edge (V, U, W);No edge}while (s--) {scanf ("%d", &s); Add_edge (0,s,0);//build million Energy point}SPFA (0); int min=inf;while (e--) {scanf ("%d", &e), if (Min>low[e]) min=low[e];} printf ("%d\n", Min);} return 0;}


A man's travels. Hdu Hangzhou Electric 2066 "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.