51nod 1459 Maze Game (shortest way)

Source: Internet
Author: User
Tags min

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, you can calculate the maximum score on the premise of leaving 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 space-delimited integers x, y, Z (0< z<=200) represents the road, representing the road from room X to room y (bidirectional), note that at most one road is connected to two rooms, and you need time for Z.
The input guarantees that there is at least one path from start to end.

Output

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 2
1 2 3
0 1 10
1 2 11

Output example

21 6

The shortest, I use Dijkstra, as long as the time spent in the same situation to update the value of the money.

#include <iostream> #include <cstring> using namespace std;
#define INF 0x3f3f3f3f int vis[1005],a[1005],p[1005][1005],d[1005],ans[1005];
int n,m,sta,end,u,v,w;
    void Dij () {for (int i=0;i<n;i++) d[i]=p[sta][i];
    d[sta]=0;
    Ans[sta]=a[sta];
        for (int i=0;i<n;i++) {int min=inf,k=sta;
        for (int j=0;j<n;j++) if (!vis[j]&&d[j]<min)//Find shortest path min=d[k=j];
        Vis[k]=1;
                for (int j=0;j<n;j++) {if (D[j]>d[k]+p[k][j]) {d[j]=d[k]+p[k][j];
            ANS[J]=ANS[K]+A[J];   } else if (D[j]==d[k]+p[k][j]) Ans[j]=max (Ans[j],ans[k]+a[j]);
        In the same amount of time, update the value of the more Money}}} int main () {while (cin>>n>>m>>sta>>end) {
        memset (P,inf,sizeof (p));
        memset (vis,0,sizeof (VIS));
        memset (ans,0,sizeof (ans)); for (int i=0;i<n;i++) cin>&Gt;a[i];
            for (int i=0;i<m;i++) {cin>>u>>v>>w;
        P[u][v]=p[v][u]=w;
        } dij ();
    cout<<d[end]<< "" <<ans[end]<<endl;
} return 0; }

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.