The journey of a manTime
limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 18044 Accepted Submission (s): 6266
Problem description Although the grass is a road fetish (is in Hangzhou electric for more than a year. Even the people who get lost on campus, sweat ~), but the grass is still very fond of travel. Because you will meet a lot of people on the road (Prince Charming. ^0^), very much, but also can enrich their experience, but also can see beautiful scenery ... The grass wants to go to very many places, she wants to go to the Tokyo Tower to see the night view. Go to Venice to see a movie, to see the Taro on Yangmingshan, go to New York pure snow, go to Paris to drink coffee letter, go 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 go to a place where they want to go. As the grass son's home 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 set, and the first line of each group is three integer T. S and D, indicating that there is a T road, and the grass home adjacent to the city of S, where the grass wants to go 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 to 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 31 3 51 4 72 8 123 8 44 9 129 10 21 28 9 10
Sample Output
9
This problem is also the shortest, this question I was the starting point to look at the home of grass, her home to the adjacent city spent time of 0, and will not affect the results, so also avoid multiple loops. Causes a timeout.
Code:
#include <stdio.h>
#include <string.h>
#define INF 1 << 30
int a = 0, b = 0, time = 0;
int dis[1001];//used to record time and update
int map[1001][1001];//used to record the distance between two points
int c[1001];//used to record the end point
int max = 0; To record the biggest label.
int used[1001]; Used to mark
Input function, co-diagram
void input (int t,int s,int D)
{
int i = 0, j = 0;
for (i = 0; i < 1001; i++)
{
for (j = 0; J < 1001; J + +)
{
MAP[I][J] = INF;
}
Dis[i] = INF;
Used[i] = 0;
}
for (i = 0; i < T; i++)
{
scanf ("%d%d%d", &a,&b,&time);
if (Map[a][b] > Time)
{
MAP[A][B] = time;
Map[b][a] = time;
}
max = max > A? max:a;
max = max > B? Max:b;
}
for (i = 0; i < S; i++)
{
int x = 0;
scanf ("%d", &x);
Map[0][x] = 0;
}
for (i = 0; i < D; i++)
scanf ("%d", &c[i]);
}
void Dijstra (int n)
{
int i = 0, j = 0;
BOOL R;
int c = 0;
for (i = 0; I <= N; i++)
{
R = false;
int min = INF;
Find the point that takes the shortest time
for (j = 0; J <= N; j + +)
{
if (!used[j] && min > Dis[j])
{
R = True;
min = Dis[j];
C = j;
}
}
Assuming that the point is not found, the shortest circuit that represents all the points has been exhausted
if (!r)
return;
Otherwise mark the point
USED[C] = 1;
Slack operation
for (j = 0; J <= N; j + +)
{
if (!used[j] && dis[j] > Dis[c] + map[c][j])
DIS[J] = Dis[c] + map[c][j];
}
}
}
int main ()
{
int T, S, D;
while (~SCANF ("%d%d%d", &t, &s, &d))
{
if (D = = 0)
printf ("0\n");
int i = 0, j = 0;
max = 0;
int m = INF;
Input (T, S, D);
for (i = 0; i < D; i++)
{
Dis[0] = 0;
Dijstra (max);
if (M > Dis[c[i]])
m = Dis[c[i]];
}
printf ("%d\n", m);
memset (map,0,sizeof (map));
Memset (C,0,sizeof (c));
}
return 0;
}
Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.
Hangzhou Electric 20,661 Person's travel