Hiho week 1 --- Question 1: Shortest Path & # 23rd; 1. hiho week 1

Source: Internet
Author: User

Hiho week 1 --- Question 1: Shortest Path 1, hiho week 1

Question 1: Shortest Path-1

Time Limit: milliseconds ms single-point time limit: 256 Ms memory limit: MB
Description

On Halloween morning, after an hour of debate, Xiao Hi and Xiao Ho finally decided how to spend such a meaningful day-they decided to go to the haunted house!

After several hours of queuing at the entrance of the haunted house, The Little Hi and the little Ho who just entered the haunted house were quite hungry, so they decided to use the map they received before entering the room, find the shortest path to the end.

There are a total of N locations in the haunted house, numbered 1 .. n. There are some road connections between the N locations. There may be multiple road connections between the two locations, but there is no road on both ends of the two locations. So how long does Xiao Hi and Xiao Ho have to go out of the haunted house to eat?

Tip: Order! Order is the key. Input

Each test point (input file) has only one set of test data.

In a group of test data:

1st behavior 4 integers N, M, S, and T represent the number of locations in the haunted house and the number of roads, respectively, the number of entrances (also a location, exit Number (also a location.

In the next M row, each line describes a path: the I-th behavior contains three integers: u_ I, v_ I, length_ I, it indicates that there is a road with the length of length_ I between the location numbered u_ I and the location numbered v_ I.

For 100% of data, N <= 10 ^ 3, M <= 10 ^ 4, 1 <= length_ I <= 10 ^ 3, 1 <= S, T <= N, and S is not equal to T.

For 100% of the data, there is always a way for small Hi and small Ho to reach the exit through the road marked on the map from the entrance.

Output

For each group of test data, an integer Ans is output, indicating that the small Hi and small Ho must at least walk out of the haunted house.

Sample Input
5 23 5 41 2 7082 3 1123 4 7214 5 3395 4 9601 5 8492 5 981 4 992 4 252 1 2003 1 1463 2 1061 4 8604 1 7955 4 4795 4 2803 4 3411 4 6224 2 3622 3 4154 1 9042 1 7162 5 575
Sample output
123



Analysis: the bare Shortest Path is dijkstra, but you must note that the starting point is given by the data, rather than fixed.



AC code:

#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>using namespace std;#define INF 123456789int a[1005][1005], dis[1005], v[1005];void dijkstra(int s, int t, int n){for(int i=1; i<=n; i++)  dis[i] = a[s][i];memset(v, 0, sizeof(v));dis[s] = 0;for(int i=1; i<=n; i++){int mark = -1, mdis = INF;for(int j=1; j<=n; j++)if(!v[j] && dis[j] < mdis){mdis = dis[j];mark = j;}v[mark] = 1;for(int j=1; j<=n; j++)if(!v[j])  dis[j] = min( dis[j], dis[mark] + a[mark][j]);}printf("%d\n", dis[t]);}int main(){//freopen("in.txt", "r", stdin);int n, m, s, t, x, y, dist;while(scanf("%d%d%d%d", &n, &m, &s, &t)!=EOF){for(int i=1; i<=n; i++)for(int j=1; j<=n; j++)a[i][j] = INF;while(m--){scanf("%d%d%d", &x, &y, &dist);if(a[x][y] > dist){a[x][y] = dist;a[y][x] = dist;} }dijkstra(s, t, n);}return 0;}



Dijkstra has many different implementation versions. It feels good to select one. Generally, this type of question can be used.



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.