The journey of a man

Source: Internet
Author: User

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, can not be wasted ah, must give yourself good put a fake, but also can not waste training ah, so grass son decided in the shortest time 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 INPUT6 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 Output9 idea: This is to find the shortest path problem, we can be the grass son's possessions into No. 0 city, and then constitute a No. 0 number as the starting point of the figure, the end of the program output, and then began to calculate the shortest path through the Dijkstra algorithm, thus calculating the shortest path to each city. Code

#include <iostream>
#include <string.h>
#include <algorithm>
using namespace Std;
const int inf = 0X3F3F3F3F;

int t,s,d,n;
int map[1111][1111];
int vis[1111],cast[1111];
int s[1111],e[1111];

void Dijkstra ()
{
int i,j,minn,pos;
memset (vis,0,sizeof (VIS));
Vis[0] = 1;
for (i = 0; i<=n; i++)
Cast[i] = Map[0][i];
for (i = 1; i<=n; i++)
{
Minn = INF;
for (j = 1; j<=n; j + +)
{
if (Cast[j]<minn &&!vis[j])
{
pos = j;
Minn = cast[j];
}
}
Vis[pos] = 1;
for (j = 1; j<=n; j + +)
{
if (Cast[pos]+map[pos][j]<cast[j] &&!vis[j])
CAST[J] = Cast[pos]+map[pos][j];
}
}
}

int main ()
{
int i,j,x,y,z;
while (Cin >> T >> S >> D)
{
n = 0;
for (i = 0; i<1111; i++)
{
for (j = 0; j<1111; j + +)
MAP[I][J] = inf;
Map[i][i] = 0;
}
while (t--)
{
Cin >> x >> y >> z;
n = max (max (n,x), y);
if (Z<map[x][y])
Map[x][y] = map[y][x] = Z;
}
int minn = INF;
for (i = 0; i<s; i++)
{
CIN >> S[i];
Map[0][s[i]] = map[s[i]][0] = 0;
}
for (i = 0; i<d; i++)
CIN >> E[i];
Dijkstra ();
for (i = 0; i<d; i++)
Minn = min (Minn,cast[e[i]]);
cout << Minn << Endl;
}

return 0;
}

The journey of a man

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.