1072. Gas Station (30) PAT+Dijkstra单源最短路径

注意:关键在于理解题意,之后就是基本的最短路径问题。

题目:1072. Gas Station (30) 

A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible. However it must guarantee that all the houses are in its service range.

Now given the map of the city and several candidate locations for the gas station, you are supposed to give the best recommendation. If there are more than one solution, output the one with the smallest average distance to all the houses. If such a solution is still not unique, output the one with the smallest index number.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 positive integers: N (<= 103), the total number of houses; M (<= 10), the total number of the candidate locations for the gas stations; K (<= 104), the number of roads connecting the houses and the gas stations; and DS, the maximum service range of the gas station. It is hence assumed that all the houses are numbered from 1 to N, and all the candidate locations are numbered from G1 to GM.

Then K lines follow, each describes a road in the format
P1 P2 Dist
where P1 and P2 are the two ends of a road which can be either house numbers or gas station numbers, and Dist is the integer length of the road.

Output Specification:

For each test case, print in the first line the index number of the best location. In the next line, print the minimum and the average distances between the solution and all the houses. The numbers in a line must be separated by a space and be accurate up to 1 decimal place. If the solution does not exist, simply output “No Solution”.

Sample Input 1:
4 3 11 5
1 2 2
1 4 2
1 G1 4
1 G2 3
2 3 2
2 G2 1
3 4 2
3 G3 2
4 G1 3
G2 G1 1
G3 G2 2
Sample Output 1:
G1
2.0 3.3
Sample Input 2:
2 1 2 10
1 G1 9
2 G1 20
Sample Output 2:
No Solution
C++代码
 
   
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

#define INF 65535

int N,M,K,D;

int map[1100][1100];
bool PathMatrix[1100][1100];
int ShortPathTable[1100];
bool final[1100];

void ShortestPath_DIJ(int v0)
{
	int vecnum = N+M;
	for (int v=0; v &a, const vector &b)
{
	if(a[1] > b[1])
		return true;
	else if(a[1] < b[1])
		return false;
	else
	{
		if (a[2] < b[2])
			return true;
		else if(a[2] > b[2])
			return false;
		else
		{
			return a[0] < b[0];
		}
	}
}

int main()
{
	cin>>N>>M>>K>>D;
	getchar();
	for (int i=0; i<(N+M); ++i)
	{
		for (int j=0; j<(N+M); ++j)
		{
			map[i][j] = INF;
		}
	}
	for (int i=0; i>word)
		{
			if (word[0] != 'G')
			{
				if (cnt != 2)
				{
					num[cnt] = atoi(word.c_str()) -1;
				}
				else
				{
					num[cnt] = atoi(word.c_str());
				}
				
			}
			else
			{
				string s(word.begin()+1, word.end());
				num[cnt] = atoi(s.c_str()) +N -1;
			}
			cnt++;
		}
		map[num[0]][num[1]] = map[num[1]][num[0]] = num[2];
	}

	vector> vec;
	for (int i=0; i D)
			{
				flag = false;
				break;
			}
		}
		if (flag)
		{
			vector temp;
			int sum = 0;
			int min = INF;
			temp.push_back(i);
			for (int k=0; k ShortPathTable[k])
				{
					min = ShortPathTable[k];
				}
			}
			temp.push_back(min);
			temp.push_back(sum);

			vec.push_back(temp);
		}
	}

	if (vec.size() > 0)
	{
		sort(vec.begin(), vec.end(), cmp);
		cout<<"G"<

1072. Gas Station (30) PAT+Dijkstra单源最短路径_第1张图片


算法改进:

#include
#include
#include
#include
#include
using namespace std;

#define MAX 1100
#define INF 1000000

int map[MAX][MAX];
int dist[MAX]; 
int before[MAX];
bool s[MAX];

int N, M, K, D;

void Dij(int n, int v)
{
	for(int i=0; i &a, const vector &b)
{
	if(a[1] > b[1])
		return true;
	else if(a[1] < b[1])
		return false;
	else
	{
		if(a[2] < b[2])
			return true;
		else if(a[2] > b[2])
			return false;
		else
			return a[0] < b[0];
	}
}

int main()
{
	cin>>N>>M>>K>>D;

	for(int i=0; i<(N+M); i++)
	{
		for(int j=0; j<(N+M); j++)
		{
			map[i][j] = map[j][i] = INF;
		}
	}

	getchar();
	string line, word;
	for(int i=0; i>word)
		{
			if(word[0] != 'G')
			{
				if(cnt == 2)
				{
					num[cnt] = atoi(word.c_str());
				}
				else
				{
					num[cnt] = atoi(word.c_str()) - 1;
				}
			}
			else
			{
				string s(word.begin()+1, word.end());
				num[cnt] = atoi(s.c_str()) + N -1;
			}
			cnt++;
		}
		map[num[0]][num[1]] = map[num[1]][num[0]] = num[2];
	}

	vector> vec;
	for(int i=0; i D)
			{
				flag = false;
				break;
			}
		}
		if(flag)
		{
			int min = INF;
			int sum = 0;
			vector temp;
			temp.push_back(i);
			for (int j=0; j 0)
	{
		sort(vec.begin(), vec.end(), cmp);
		cout<<"G"<


你可能感兴趣的:(PAT)