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 <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <iomanip>
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<vecnum; ++v)
	{
		final[v] = false; 
		ShortPathTable[v] = map[v0][v];
		for (int w=0; w<vecnum; ++w)
		{
			PathMatrix[v][w] = false;
		}
		if (ShortPathTable[v] < INF)
		{
			PathMatrix[v][v0] = true;
			PathMatrix[v][v] = true;
		}
	}

	ShortPathTable[v0] = 0;
	final[v0] = true;

	for (int i=1; i<vecnum; ++i)
	{
		int min = INF;
		int v;
		for (int w=0; w<vecnum; ++w)
		{
			if (!final[w])
			{
				if (ShortPathTable[w] < min)
				{
					v = w;
					min = ShortPathTable[w];
				}
			}
		}
		final[v] = true;
		for (int w=0; w<vecnum; ++w)
		{
			if (!final[w] && (min+map[v][w]<ShortPathTable[w]))
			{
				ShortPathTable[w] = min + map[v][w];
				for (int j=0; j<vecnum; ++j)
				{
					PathMatrix[w][j] = PathMatrix[v][j];
					PathMatrix[w][w] = true;
				}
			}
		}
	}
}

bool cmp(const vector<int> &a, const vector<int> &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<K; ++i)
	{
		int num[3];
		int cnt = 0;
		string line, word;
		getline(cin, line);
		istringstream stream(line);
		while (stream>>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<vector<int>> vec;
	for (int i=0; i<M; i++)
	{
		ShortestPath_DIJ(N+i);
		int flag = true;
		for (int j=0; j<N; j++)
		{
			if (ShortPathTable[j] > D)
			{
				flag = false;
				break;
			}
		}
		if (flag)
		{
			vector<int> temp;
			int sum = 0;
			int min = INF;
			temp.push_back(i);
			for (int k=0; k<N; k++)
			{
				sum += ShortPathTable[k];
				if (min > 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"<<vec[0][0]+1<<endl<<setiosflags(ios::fixed)<<setprecision(1)<<vec[0][1]*1.0<<" "<<setiosflags(ios::fixed)<<setprecision(1)<<vec[0][2]*1.0/N<<endl;
	}
	else
	{
		cout<<"No Solution"<<endl;
	}
	
	system("pause");
	return 0;
}



算法改进:

#include<iostream>
#include<vector>
#include<sstream>
#include<iomanip>
#include<algorithm>
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<n; i++)
	{
		dist[i] = map[v][i];
		s[i] = false;
		if(dist[i] == INF)
			before[i] = -1;
		else
			before[i] = v;
	}

	dist[v] = 0;
	s[v] = true;

	for(int i=1; i<n; i++)
	{
		int temp = INF;
		int u = v;
		for(int j=0; j<n; j++)
		{
			if((!s[j]) && (dist[j] < temp))
			{
				u = j; 
				temp = dist[j];
			}
		}
		s[u] = true;
		for(int j=0; j<n; j++)
		{
			if((!s[j]) && (map[u][j] < INF))
			{
				int newdist = dist[u] + map[u][j];
				if(newdist<dist[j])
				{
					dist[j] = newdist;
					before[j] = u;
				}
			}
		}
	}
}

bool cmp(const vector<int> &a, const vector<int> &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<K; i++)
	{
		getline(cin, line);
		cout<<line<<endl;
		istringstream stream(line);
		int num[3];
		int cnt = 0;
		while(stream>>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<vector<int>> vec;
	for(int i=0; i<M; i++)
	{
		Dij(N+M, N+i);
		bool flag = true;
		for(int j=0; j<N; j++)
		{
			if(dist[j] > D)
			{
				flag = false;
				break;
			}
		}
		if(flag)
		{
			int min = INF;
			int sum = 0;
			vector<int> temp;
			temp.push_back(i);
			for (int j=0; j<N; j++)
			{
				if (dist[j] < min)
				{
					min = dist[j];
				}
				sum += dist[j];
			}
			temp.push_back(min);
			temp.push_back(sum);

			vec.push_back(temp);
		}
	}

	if(vec.size() > 0)
	{
		sort(vec.begin(), vec.end(), cmp);
		cout<<"G"<<vec[0][0]+1<<endl<<fixed<<setprecision(1)<<vec[0][1]*1.0<<" "<<fixed<<setprecision(1)<<vec[0][2]*1.0/N<<endl;
	}
	else
	{
		cout<<"No Solution"<<endl;
	}

	system("pause");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值