Dijkstra algorithm Preliminary-maze problem

Source: Internet
Author: User

before you come to a maze. The maze consists of a number of rooms, each with a score, and the first time you enter this room, you can get this score. There are several bidirectional roads linking these rooms, and it takes some time for you to walk from one room to another in these roads. The game prescribes your starting and ending rooms, your first goal is to reach the end point as soon as possible from the beginning, and in the premise of satisfying the primary goal, make your score as large as possible. Now that's the problem, given the room, the road, the score, the starting point, and the end point, can you calculate the maximum score for you to leave the maze as soon as possible? Input
The first line is 4 integers n (<=500), M, start, end. n represents the number of rooms, the room number from 0 to (n-1), m for the number of roads, any two rooms with a maximum of one road, start and end represents the start and end of the room number. The second line contains n spaces separated by a positive integer (no more than 600), which represents your score into each room. Next m line, each line of 3 spaces separated by the integer x, y, Z (0<z<=200) represents the road, representing the road from room X to room y (bidirectional), note that at most only one road is connected to two rooms, you need time for Z. The input guarantees that there is at least one path from start to end.
One line, two space-delimited integers, the first representing the minimum amount of time you need, and the second representing the maximum score you can get on the minimum time premise.
Input Example
3 2 0 21 2 30 1 101 2 11
Output Example
21 6

---------------------------------------------------I'm the split line ^_^-------------------------------------------------

This is titled Dijkstra Algorithm of the basic problem, used to understand the single source shortest path of the method, in fact, the beginning of the introduction are in
"Ah ha!" Algorithm in this book, the shortest Path problem, which can be done using adjacency tables and priority queues, but
I'm useless, I used a two-dimensional array of inefficient, but there is also a place to be stumped, is to calculate the maximum score
, was stuck for a few hours to find the error later found that the original is because the same situation is superfluous, to pay attention to the most
Small maximum and longest shortest of some restrictions, when writing later to remind yourself to note = =.

#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<vector>#include<queue>usingNamespaceStd;#define INT __int64#define INF0x3f3f3f3fConstint MAXN=555;int n+ J, SE;int Maze[MAXN][maxn];int dis[MAXN];BOOL Vis[MAXN];INT score[MAXN];int ans[MAXN];voidInit(){For(int I=0; I< n; I++){For(Int J=0; J< n; J++){If(I= = J) Maze[I][j]=0;Else Maze[I][j]= INF;}}Memset(Dis,0,sizeof(Dis));Memset(Vis, False,sizeof(Vis));Memset(ans, False,sizeof(ans));}voidDijkstra(){For(int I=0; I< n; I++){scanf("%d",&score[I]);}int U+ Q+ P;For(int I=0; I< m; I++){scanf("%d%d%d",&u,&v,&w);If(W< Maze[u][v]){Maze[u][v]= W; Maze[V][u]= W;}}For(int I=0; I< n; I++){Dis[I]= Maze[s][i];If(I! = S) ans[I]= Score[s]+ Score[IWhen];//is initialized, it must be judged equal to the situation.else ans[I]= Score[I];}For(int I=0; I< n; I++){int POS, Min= INF;For(int a=0; A< n; A++){If(Vis[A])Continue;If(Dis[A]< Min){Min= Dis[A]; Pos= A;}} Vis[POS]= True;For(int a=0; A< n; A++){If(Maze[POS][a]< INF){If(Dis[A]> Dis[POS]+ Maze[POS][a]){Dis[A]= Dis[POS]+ Maze[POS][a]; Ans[A]= ans[POS]+ Score[A];}ElseIf(Dis[A]= = Dis[POS]+ Maze[POS][a])&&(ans[A]< ans[POS]+ Score[A])&& POS! = A){/* is the right place to judge if it doesn't work. */ans[A]= ans[POS]+ Score[A];}Else{Continue;}}}}}IntMain(){Freopen("Input.txt","R"Stdin);While(scanf("%d%d%d%d",&n,&m&s&e) != eof) Span class= "Sh-cbracket" >{init (); dijkstra (); printf ( "%d%d \n "[e [e);  return 0;< Span class= "Sh-cbracket" >             

Dijkstra algorithm Preliminary-maze problem

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.