HDU 2066 one-person trip [Dijkstra 12-level freshman training-graph theory E]

Source: Internet
Author: User
Travel by one person

Time Limit: 1000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 12881 accepted submission (s): 4364


Problem description although caoer is a luchi (that is, the person who has been in Hangzhou for more than a year will still be lost on campus, Khan ~), However, caoer still enjoys traveling, because he will meet many people (Prince Charming, ^ 0 ^) on the road. Many things can enrich his experience, you can also see beautiful scenery ...... Caoer wants to go to many places. She wants to go to the Tokyo Tower to see the night view, go to Venice to see the movie, go to Yangmingshan to see the taro, go to New York to see the pure snow scene, go to Paris to drink coffee and write, visit Meng jiangnv in Beijing ...... The winter vacation is approaching. You can't waste it for such a long period of time. You must give yourself a good vacation, but you can't waste your training, therefore, caoer decided to go to a desired place in the shortest time! Because caoer's home is in a small town without passing by train, she can only take a train to the neighboring city ~).


There are multiple groups of input data. The first line of each group is three integers t, s, and D, indicating that there are t routes and S are adjacent to cao'er's city, there are d places to go;
Then there are t rows. Each row has three integers A, B, and time, indicating that the driving distance between cities A and B is time hour. (1 = <(A, B) <= 1000; there may be multiple routes between A and B)
The next line t + 1 contains the number of S, indicating the city connected to cao'er's home;
The next line T + 2 has the number D, which indicates that the grass wants to go to the place.


Output outputs the shortest time for 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
 


Authorgrass


Sourcerpg exercise session


Recommendlcy algorithm: Dijkstra train of thought: when multiple start points are set for d [], all d [start points] = 0 (PSD [I] indicates the distance from the start point to I) you can select multiple endpoints. (That is, adding a for loop before the output)

E Accepted 4236 KB 93 MS C ++ 1422 B 2013-05-23 21:42:58
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int maxn = 1000+10;const int INF = 100000000;int map[maxn][maxn];bool vis[maxn];int start[maxn];int end[maxn];int d[maxn];int T,S,D;void Dijkstra(){    for(int i = 1; i < maxn; i++)    {        bool flag = true;        for(int j = 0; j < S; j++)        if(i == start[j]) flag = false;        if(flag) d[i] = INF;        else d[i] = 0;    }    memset(vis, false, sizeof(vis));    for(int i = 0; i < maxn; i++)    {        int x,m = INF;        for(int y = 1; y < maxn; y++) if(!vis[y] && d[y] <= m) m = d[x=y];        vis[x] = true;        for(int y = 1; y < maxn; y++) d[y] = min(d[y], d[x]+map[x][y]);    }}int main(){    while(scanf("%d%d%d", &T,&S,&D) != EOF)    {        for(int i = 1; i < maxn; i++)        {            for(int j = 1; j < maxn; j++)            map[i][j] = (i == j ? 0 : INF);        }        int x,y,w;        while(T--)        {            scanf("%d%d%d", &x,&y,&w);            if(w < map[x][y])            {                map[x][y] = w;                map[y][x] = w;            }        }        for(int i = 0; i < S; i++) scanf("%d", &start[i]);        for(int i = 0; i < D; i++) scanf("%d", &end[i]);        Dijkstra();        int ans = INF;        for(int i = 0; i < D; i++) ans = min(ans, d[end[i]]);        printf("%d\n", ans);    }    return 0;}

Same as orz

2013-07-08 19:01:09 Accepted 2066 78 Ms 4260 K 1670 B C ++ Freeze

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int maxn = 1000+10;const int INF = 100000000;int map[maxn][maxn];bool vis[maxn];int start[maxn];int end[maxn];int d[maxn];int T,S,D;void Dijkstra(){    for(int i = 1; i < maxn; i++)    {        d[i] = INF;    }    for(int i = 0; i < S; i++)    {        d[start[i]] = 0;    }    memset(vis, false, sizeof(vis));    for(int i = 0; i < maxn; i++)    {        int x,m = INF;        for(int y = 1; y < maxn; y++) if(!vis[y] && d[y] <= m) m = d[x=y];        vis[x] = true;        for(int y = 1; y < maxn; y++) d[y] = min(d[y], d[x]+map[x][y]);    }}int main(){    while(scanf("%d%d%d", &T,&S,&D) != EOF)    {        for(int i = 1; i < maxn; i++)        {            for(int j = 1; j < maxn; j++)            map[i][j] = (i == j ? 0 : INF);        }        int x,y,w;        while(T--)        {            scanf("%d%d%d", &x,&y,&w);            if(w < map[x][y])            {                map[x][y] = w;                map[y][x] = w;            }        }        for(int i = 0; i < S; i++) scanf("%d", &start[i]);        for(int i = 0; i < D; i++) scanf("%d", &end[i]);        Dijkstra();        int ans = INF;        for(int i = 0; i < D; i++) ans = min(ans, d[end[i]]);        printf("%d\n", ans);    }    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.