HDU 1874 smooth engineering continued

Source: Internet
Author: User
Smooth engineering continued problem description a province has finally built many roads since the smooth engineering plan was implemented for many years. However, when there are too many roads, there are many ways to choose from every town to another town, some solutions are much shorter than others. This makes pedestrians very difficult.

Now you know the start and end points. Calculate the shortest distance from the start point to the end point. The input question contains multiple groups of data. Please process the data until the end of the file.
The first row of each data group contains two positive integers n and M (0 <n <1000, 0 <m <), representing the number of existing towns and the number of constructed roads respectively. The towns are 0 ~ N-1 number.
Next is m-line road information. Each row has three integers, A, B, x (0 <= A, B <n,! = B, 0 <x <10000) indicates that there is a two-way road with x length between town a and town B.
The next line has two integers, T (0 <= S, T <n), representing the start point and the end point respectively. For each group of data, output the shortest distance to walk in one row. If there is no route from S to T, output-1. Sample input3 30 1 10 2 31 2 10 10 1 11 2 Sample Output2-1

 

 

It's also the most short-circuit template question, but there are two pitfalls in this question. I 've done it many times. 1. during initialization, the distance from the user to the user is 0; 2. There are multiple routes from one point to the other, so the shortest path is required during input. Solve these two problems.

 

#include <stdio.h>#define maxn 0x3f3f3f3int map[205][205], dis[205], visited[205];void Dijkstra(int n, int start){    int mind, pre;    for(int i = 0; i<n; i++)    {        dis[i] = map[start][i];        visited[i] = 0;    }    visited[start] = 1;    for(int i = 0; i<n; i++)    {        mind = maxn;        for(int j = 0; j<n; j++)        {            if(mind > dis[j] && !visited[j])            {                mind = dis[j];                pre = j;            }        }        visited[pre] = 1;        for(int j = 0; j<n; j++)        {            if(dis[j] > dis[pre]+map[pre][j] && !visited[j])                dis[j] = dis[pre]+map[pre][j];        }    }}int main(){    int n, m;    int a, b, x;    int start, finish;    while(scanf("%d%d", &n, &m)!=EOF)    {        for(int i = 0; i<n; i++)        {            for(int j = 0; j<n; j++)                {                    if(i == j)                        map[i][j] = 0;                    else                        map[i][j] = maxn;                }        }        while(m--)        {            scanf("%d%d%d", &a, &b, &x);            if(map[a][b] > x || map[b][a] > x)            {                map[a][b] = x;                map[b][a] = x;            }        }        scanf("%d%d", &start, &finish);        Dijkstra(n, start);        if(!visited[finish])            printf("-1\n");        else            printf("%d\n", dis[finish]);    }    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.