HDU 20,661 People travel "shortest way, multi-start multi-end, Dijkstra algorithm +SPFA algorithm"

Source: Internet
Author: User
Tags bool min time limit
a man's journeyTime limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 31420 Accepted Submission (s): 10804


Problem Description Although grass son is a road crazy (is in Hangzhou electric stay for more than a year, incredibly still will be lost in campus, sweat ~), but grass son still very much like travel, because in the journey will meet a lot of people (Prince Charming, ^0^), many things, but also enrich their experience, You can also see the beautiful scenery ... Grass wants to go to many places, she wants to go to the Tokyo Tower to see the night scene, to Venice to see movies, to see the Taro on Yangmingshan, go to New York pure snow, go to Paris to drink coffee letter, to Beijing to visit Meng Jiangnu ... See the winter vacation is coming, so a large period of time,

Related recommendations: Hdu Travel for 20,661 people (Dijkstra algorithm)

Description Although grass son is a road fetish (that is, in Hangzhou electric for more than a year, incredibly still will be lost in the campus, sweat ~), but the grass is still very fond of travel, because in the journey will meet a lot of people (Prince Charming, ^0^), a lot of things, can enrich their experience, but also can see beautiful scenery ... The grass wants to go to many places, she wants to go to the Tokyo Tower to see the night

Can not be wasted ah, be sure to give yourself a good vacation, but also can not neglect training ah, so the grass decided in the shortest possible time to go to a place where they want to go. Because the house of grass is in a small town, there is no train passing by, so she can only go to the neighboring city by train (poor Ah ~).
Input data has more than one group, the first line of each group is three integers t,s and D, indicating that there is a T road, and the grass home adjacent to the city of S, the grass wants to go where there are D;
Then there is a T-line, each line has three integer a,b,time, indicating that the drive between A and B cities is time hours; (1=< (A, B) there may be multiple paths between <=1000;a,b)
The next line of T+1 is the number of S, which indicates the city connected with the grass family;
The next line of T+2 has a number D, indicating that the grass wants to go to the place.
Output outputs the shortest time a grass can go to a favorite city.
Sample Input

  
   6 2 3
1 3 5
1 4 7 2 8 3
8 4
4 9
9 2 1 2 8 9 10
  

Sample Output

9

PS: Just set all the distance to the starting point to zero

Dijkstra algorithm AC code:

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace
Std
int a[1005][1005];
int dis[1005];
BOOL vis[1005];
const int inf=0x3f3f3f3f;
int n;
        void Dij () {for (int i=0;i<=n;i++) {dis[i]=a[0][i];
    Vis[i]=false;
    } vis[0]=true;
    dis[0]=0;
        for (int i=0;i<=n;i++) {int minn=inf;
        int p; for (int j=0;j<=n;j++) {if (!vis[j]&&dis[j]<minn) {minn=dis[
            P=J];
        }} vis[p]=true;
        if (minn==inf) break; for (int j=0;j<=n;j++) {if (!vis[j]&&dis[j]>dis[p]+a[p][j]) dis[j]=dis[p]
        +A[P][J];
    }}} int main () {int t,s,d;
    int x, y, Z;
        while (scanf ("%d%d%d", &t,&s,&d)!=eof) {n=0;
        memset (A,inf,sizeof (a)); while (t--) {scanf ("%d%d%d",&x,&y,&Z);
            if (Z<a[x][y]) a[x][y]=a[y][x]=z;
        N=max (N,max (x, y));
            } while (s--) {scanf ("%d", &x);
        a[0][x]=0;//assumes 0 as the starting point} dij ();
        int minn=inf;
            while (d--) {scanf ("%d", &x);
        Minn=min (Minn,dis[x]);
    } printf ("%d\n", Minn);
} return 0;
 }

SPFA algorithm AC code
#include <cstdio> #include <queue> #include <algorithm> #include <cstring> using namespace std;
int a[1005][1005];
int dis[1005];
BOOL vis[1005];
int start[1005];
int n;
int s;
const int inf=0x3f3f3f3f;
        void Spfa () {for (int i=1;i<=n;i++) {dis[i]=inf;
    Vis[i]=false;
    } queue<int>q;
        for (int i=0;i<s;i++) {int t=start[i];
        dis[t]=0;
        Q.push (t);
    Vis[t]=true;
        } while (!q.empty ()) {int P=q.front ();
        Q.pop ();
        Vis[p]=false; for (int i=1;i<=n;i++) {if (Dis[i]>dis[p]+a[p][i]) {dis[i]=dis[p]+a[
                P][i];
                    if (!vis[i]) {vis[i]=true;
                Q.push (i);
    }}}}} int main () {int t,d;
    int x, y, Z;
       while (scanf ("%d%d%d", &t,&s,&d)!=eof) {for (int i=0;i<1005;i++)     for (int j=0;j<1005;j++) A[i][j]=inf;
        n=0;
            while (t--) {scanf ("%d%d%d", &x,&y,&z);
            N=max (N,max (x, y));
            if (Z<a[x][y]) {a[x][y]=a[y][x]=z;
        }} for (int i=0;i<s;i++) {scanf ("%d", &start[i]);
        } SPFA ();
        int minn=inf;
            while (d--) {scanf ("%d", &x);
            printf ("%d\n", dis[x]);
        Minn=min (Minn,dis[x]);
    } printf ("%d\n", Minn);
} 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.