HDU Travel for 20,661 people

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=2066



Analysis:

The shortest time for the city where the grass wants to go is the shortest distance from the home->a (neighboring city)->b (any one of several).

Since the end is from a starting point to an end point, careful analysis, it is not difficult to conclude that this is a single source shortest path problem (is a starting point, an end point).

So you can use Dijkstra.

The code is as follows:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#define MAXX 999999999
using namespace Std;
int T, S, D, N,u;
int map[1005][1005];
int star[1005];
int terminal[1005];
int _distance[1005];
int vis[1005];
void Djstl ()
{
memset (Vis, 0, sizeof (VIS));
for (int i = 0;i <= n;++i)
{
_distance[i] = Map[0][i];

This represents the distance from all points to 0 points; using _distance[i] to represent
}
Vis[0] = 1;

Mark 0 points have passed-------------------------------------
for (int i = 1;i <= n-1;++i)
{
int mins = Maxx;
for (int j = 1;j <= n;++j)
{
if (!vis[j] && mins > _distance[j])
{
mins = _distance[j];
U = j;
}
}
if (Vis[u])
Continue

The point closest to I is not tested, and goes directly to the next loop. (Note the position of the U definition)
Vis[u] = 1;
for (int j = 1;j <= n;++j)
{
if (map[u][j]! = Maxx)
if (Map[u][j] + _distance[u] < _distance[j])
_DISTANCE[J] = _distance[u] + map[u][j];

Relaxation operation, distance to U + distance from u to J, more than existing line to J, shorter, update
}
}
}
int main ()
{
while (scanf ("%d%d%d", &t, &s, &d) = = 3)
{
for (int i = 0;i < 1005;++i)
{
for (int j = 0;j < 1005;++j)
MAP[I][J] = Maxx;
Map[i][i] = 0;
}

Using adjacency matrix to save the graph--------------------------------------
int A, B, Tim;
n = 0;
for (int i = 0;i < T;++i)
{
scanf ("%d%d%d", &a, &b, &tim);
MAP[A][B] = Map[b][a] = Map[a][b]>tim? TIM:MAP[A][B];

Save the shortest path between two cities-----------------------------
n = max (max (A, n), b);

Save up to a few cities (save time)-------------------------
}
for (int i = 0;i < S;++i)
{
scanf ("%d", &star[i]);
Map[0][star[i]] = map[star[i]][0] = 0;

Equivalent to having multiple starting points---------------------------------------
}
Djstl ();
int mins = Maxx;
for (int i = 0;i < D;++i)
{
scanf ("%d", &terminal[i]);
if (_distance[terminal[i]] < mins)
mins = _distance[terminal[i]];

Find the shortest distance you can---------------------------------------
}
cout << mins << Endl;
}
}

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.