[Algorithm] Shortest Path--Hdu2066 Traveling Alone

A person's travel


Problem Description
Although Cao'er is a road idiot (that is, someone who has stayed in Hangdian for more than a year, but still gets lost in the campus, Khan~), but Cao'er still likes to travel, because she will meet many people during the journey (Prince Charming, ^0^), a lot of things, but also enriching her own experience and seeing beautiful scenery... Cao'er wants to go to many places, she wants to go to Tokyo Tower to see the night view, go to Venice to watch movies, and go to Yangmingshan to see the sea Taro, go to New York just to watch the snow scene, go to Paris to drink coffee and write a letter, go to Beijing to visit Meng Jiangnu... As the winter vacation is coming soon, such a long time cannot be wasted, you must give yourself a good vacation, but also You can't waste training, so Cao'er decided to go to a place she wants to go in the shortest possible time! Because Cao'er's home is in a small town, there is no train passing by, so she can only go to the neighboring city to take the train (so pitiful~).
 
Input
There are multiple groups of input data, the first line of each group is three integers T, S and D, indicating that there are T roads, S in the city adjacent to Cao'er's house, and D in the place that Cao'er wants to go. ;
Then there are T lines, each line has three integers a, b, time, indicating that the drive between cities a and b is time hours; (1=<(a,b)<=1000; there may be Multiple roads)
The next line T+1 has S numbers, indicating the city connected to Cao'er's house;
the next line T+2 has D numbers, indicating the place Cao'er wants to go.
 
Output
Output the shortest time that Cao'er can go to a favorite 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

Topic Analysis:

This is a problem of finding the shortest path. It is a multi-source and multi-node type. When using dijkstra, a double for loop is used, and the result is TLE. After considering that the dijkstra() of each source node can search all destination nodes, the second for loop is put into dijkstra(), and the result is AC.

There is one thing to pay attention to in this question, (1=<(a,b)<=1000; there may be multiple paths between a and b), this sentence should pay attention to taking the smallest path between a and b to remove duplicates.

Code:

// hdu 2066 travel alone

// Dijkstra -- the shortest path problem
// The shortest distance from the source vertex to each vertex

#include<iostream>
#include<vector>
#include<stdio.h>
#include<string.h>  //memset
#include<math.h>  // max

#define INT_MAX 0x7fffffff
#define Alone   -1;
using namespace std;


/*
function: Dijkstra(int v,int n,vector<int> &dist,vector<int> &pre,vector<vector<int>> &d)
param: int v: start node
		int n: number of nodes
		dist : distance
		pre : parent node
		d : figure
return: void

Note: 1. Does the subscript start from 0 or 1? Determined n/s(n)/s(n+1)
	  2. How to deal with isolated nodes
		 
*/

int Dijkstra(int v,int n,int* dist,int* pre,vector<vector<int> > d,int D,int *b )
{
	int result=INT_MAX;
	//vector<bool> s(n+1); // black nodes are initialized to 0-white
	bool s[n+1];
	memset(s,false,sizeof(s));
	for(int i=1;i<=n;i++)  //初始化 dist,pre
	{
		dist[i]=d[v][i];
		if(dist[i] < INT_MAX)
		{
			for [i] = v;
		}
		else pre[i]=Alone; //Parent not found
	}
	dist[v]=0; //The distance of the start node is set to 0
	s[v]=true; //Start node black
	
	for(int i=2;i<=n;i++)  // n-1 次循环
	{
		int best=v;
		int temp=INT_MAX;
		for(int j=2;j<=n;j++) //find the minimum distance
		{
			if(!s[j] && dist[j]<temp)
			{
				temp=dist[j];
				best=j;
			}
		}
		s[best]=true;
		for(int k=2;k<=n;k++)
		{
			if(! s[k] && d[best][k] !=INT_MAX) //k node is white, and best->k has a link (connected)
			{
				int new_dist=dist[best]+d[best][k];
				if(new_dist < dist[k])
				{
					dist[k]=new_dist;
					pre[k]=best;
				}	
			}
		}
	}
	for(int i=0;i<D;i++)
	{
		if(result > dist[b[i]]) result=dist[b[i]];
	}
	return result;
}

intmain()
{
 	//freopen("2.txt","r",stdin);
	vector<vector<int> > d(1001,vector<int>(1001)) ; //map path map (n*n)
	int T,S,D; //T edges, S source nodes, D endpoints
	while(scanf("%d%d%d",&T,&S,&D) !=EOF)
	{
	//initialize the graph
	for(int i=1;i<=1000;i++) //The subscript starts from 0, undirected graph
	{
		for(int j=1;j<=1000;j++)
		{
			d[i][j]=INT_MAX;
		}
	}
	
	int p,q,value; // p->q
	int max_point=0;
	for(int i=0;i<T;i++ )
	{
		cin>>p>>q>>value;
		if(max_point <= max(p,q))
			max_point=max(p,q);
		if(value< d[p][q] )
		{
			d[p][q]=value;
			d[q][p]=value; //Add this sentence to an undirected graph
		}
	}
    
    int N=max_point;
	int a[N],b[N];
	for(int i=0;i<S;i++)
	{
		cin>>a[i];
	}
	for(int i=0;i<D;i++)
	{
		cin>>b[i];
	}
	
	int min_dist = INT_MAX;
	for(int i=0;i<S;i++)
	{
		int  dist[N+1],pre[N+1];
		int  result=Dijkstra(a[i],N,dist,pre,d,D,b);
		if(result <min_dist) min_dist=result;
// for(int j=0;j<D;j++) // double loop TLE
//		{
//			Dijkstra(a[i],N,dist,pre,d);
//			if(dist[b[j] ]< min_dist)  min_dist=dist[b[j]] ;
//		}
	
	}
	
	cout<<min_dist<<endl;
	}
	return 0;
}
 
 
  


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325818041&siteId=291194637