HDU 2066 —— a personal trip

Problem Description

Although Cao Er is a road addict (even if he stayed in Hangzhou Telecom for more than a year, he would still be lost in the campus, sweat ~), but Cao Er still likes traveling very much, because he will meet many people (Prince Charming, 0), many things, but also enrich his experience, and can see beautiful scenery... Cao Er wants to go to many places. She wants to go to the Tokyo Tower to see the night scenery, to Venice to see the movies, to Yangming Mountain to see the sea taro, to New York to see the snow scenery, to Paris to drink coffee and write letters, to Beijing to visit Meng Jiangnu... Seeing that winter vacation is coming, we can't waste such a long period of time. We must give ourselves a good holiday, but we can't abandon training. So Cao Er decided to go to a place he wanted to go in the shortest time. Because Cao Er's family is in a small town, there is no train passing by, so she can only go to the nearby city by train (pitiful ah ~).

Input

There are many groups of input data, the first line of each group is three integers T, S and D, indicating that there are T roads, S cities adjacent to Cao'er's family, and D places where Cao'er wants to go.
Then there are T lines, each line has three integers a,b, time, indicating that the distance between cities a and B is time hours; (1=<(a,b)<=1000; there may be many roads between cities a and b)
The next T+1 line has the number of S, indicating the city connected with the Cao Er family.
The next T+2 line has D numbers, indicating that the grass wants to go somewhere.

Output

The shortest time to export grass to a preferred city.

Sample Input
6 2 3
1 3 5
1 4 7
2 8 12
3 8 4
4 9 12
9 10 2
1 2
8 9 10

Sample Output

9
Let's talk about the way to solve the problem first. The most important thing of this problem is to build a map. Once the map is built, then there will be no difficulty. So the problem arises. How to build a map? In fact, it is also very simple to think that we can regard Cao'er's family as node 0, and all the nodes adjacent to Cao'er's family are given weights of 0 so that we can run dijsktra once.

AC code

#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>

using namespace std;

const int maxn = 1e4 + 5;
const int inf = 0x3f3f;

int n, m, sum, cnt = 1, S, D, T, a, b, time, head[maxn], visit[maxn], dis[maxn];

struct Edge
{
   int to, w, next;
   bool operator < (const Edge a) const
   {
       return a.w < w;
   }

}edg[maxn], now, Next;

void add(int u, int v, int w)
{
   edg[cnt].w = w;
   edg[cnt].to = v;
   edg[cnt].next = head[u];
   head[u] = cnt++;
}

void init()
{
   memset(dis, inf, sizeof(dis));
   memset(head, -1, sizeof(head));
   memset(visit, 0, sizeof(visit));
   cnt = 1;
   sum = 0;
}

priority_queue<Edge> q;

void dijsktra()
{
   now.to = 0;
   dis[0] = 0;
   q.push(now);
   while(!q.empty())
   {
       now = q.top();
       q.pop();
       if(visit[now.to] == 1)
           continue;
       visit[now.to] = 1;
       for(int j = head[now.to]; j != -1; j = edg[j].next)
       {
           int u = edg[j].to;
           if(edg[j].w + dis[now.to] < dis[u] && !visit[u])
           {
               dis[u] = edg[j].w + dis[now.to];
               Next.to = u;
               Next.w = dis[u];
               q.push(Next);
           }
       }
   }
}

int main()
{
   while(scanf("%d %d %d", &T, &S, &D) == 3)
   {
       init();
       add(0, 0, 0);
       for(int i = 1; i <= T; i++)
       {
           scanf("%d %d %d", &a, &b, &time);
           add(a, b, time);
           add(b, a, time);
       }
       for(int i = 1; i <= S; i++)
       {
           scanf("%d", &n);
           add(0, n, 0);
       }
       dijsktra();
       int minn = inf;
       for(int i = 1; i <= D; i++)
       {
           scanf("%d", &m);
           minn = min(minn, dis[m]);
       }
       printf("%d\n", minn);
   }
}

Original reprint, please indicate the source

Posted by yashvant on Sat, 13 Apr 2019 00:18:31 -0700