PAT 1072. Gas Station (30)

10 篇文章 0 订阅

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
//典型的图论问题
#include <iostream>
#include <vector>
#include <cstdio>
#include <limits.h>
using namespace std;
struct Edge
{
	int next;
	int dist;
};
vector<vector<Edge> > vec;
vector<bool> visit;
vector<int> Dist;
struct Solu
{
	bool flag;
	int minDist;
	int aveDist;
};
vector<Solu> solu;
int getIndex(string str, int m)
{
	int sum = 0;
	int length = str.size();
	int i = 0;
	if (str[0] == 'G')
		i = 1;
	while (i < length)
	{
		sum = sum * 10 + str[i] - '0';
		++i;
	}
	if (str[0] == 'G')
		return sum - 1;
	else
		return sum + m - 1;

}
int main(void)
{
	//包含四个正整数,n为房点,m为站点,k为边
	int n, m, k, d;
	scanf("%d%d%d%d", &n, &m, &k, &d);
	vec.resize(n + m);
	int v1, v2;
	Edge edge;
	char s1[3], s2[3];
	for (int i = 0; i < k; ++i)
	{
		scanf("%s%s%d", s1, s2, &edge.dist);
		v1 = getIndex(s1, m);
		v2 = getIndex(s2, m);
		edge.next = v1;
		vec[v2].push_back(edge);
		edge.next = v2;
		vec[v1].push_back(edge);	
	}
	//Dijkstra 算法,计算最短路径
	solu.resize(m);
	for (int i = 0; i < m; ++i)//计算从候选站点开始到每个房站点的距离
	{
		visit.clear();
		visit.resize(n + m, false);
		Dist.clear();
		Dist.resize(n + m, INT_MAX);
		Dist[i] = 0;
		for (int j = 0; j < m + n; ++j)
		{
			//寻找最小长度
			int minIndex = -1, minDist = INT_MAX;

			for (int t = 0; t < m + n; ++t)
			{
				if (visit[t] == false && Dist[t] < minDist)
				{
					minDist = Dist[t];
					minIndex = t;
				}
			}
			if (minIndex == -1 || minDist > d)
				break;
			visit[minIndex] = true;
			for (vector<Edge>::iterator iter = vec[minIndex].begin(); iter != vec[minIndex].end(); ++iter)
			{
				if (visit[iter -> next] == false && Dist[minIndex] + iter -> dist < Dist[iter -> next])
					Dist[iter -> next] = Dist[minIndex] + iter -> dist;
			}
		}
		solu[i].flag = true;
		for (int j = m; j < m + n; ++j)
		{
			if (Dist[j] > d)
			{
				solu[i].flag = false;
				break;
			}
		}
		if (solu[i].flag)
		{
			int sumDist = 0, minDist = INT_MAX;
			for (int j = m; j < m + n; ++j)
			{
				sumDist += Dist[j];
				minDist = min(minDist, Dist[j]);
			}

				solu[i].minDist = minDist;
				solu[i].aveDist = sumDist;
		}
	}
	int index = 0;
	while (index < m && solu[index].flag == false)
		++index;
	for (int i = index; i < m; ++i)
	{
		if (solu[i].flag == true && (solu[i].minDist > solu[index].minDist || (solu[i].minDist == solu[index].minDist && solu[i].aveDist < solu[index].aveDist)))
			index = i;
	}
	if (index < m)
		printf("G%d\n%.1f %.1f\n", index + 1, solu[index].minDist * 1.0, solu[index].aveDist * 1.0 / n);
	else
		printf("No Solution\n");

	return 0;
}
//典型的图论问题
//下面的方法有一个案例通不过,代码中会给出具体原因
#include <iostream>
#include <vector>
#include <cstdio>
#include <limits.h>
using namespace std;
struct Edge
{
	int next;
	int dist;
};
vector<vector<Edge> > vec;
vector<bool> visit;
vector<int> Dist;
struct Solu
{
	bool flag;
	int minDist;
	int aveDist;
};
vector<Solu> solu;
int getIndex(string str, int m)
{
	int sum = 0;
	int length = str.size();
	int i = 0;
	if (str[0] == 'G')
		i = 1;
	while (i < length)
	{
		sum = sum * 10 + str[i] - '0';
		++i;
	}
	if (str[0] == 'G')
		return sum - 1;
	else
		return sum + m - 1;

}
int main(void)
{
	//包含四个正整数,n为房点,m为站点,k为边
	int n, m, k, d;
	scanf("%d%d%d%d", &n, &m, &k, &d);
	vec.resize(n + m);
	int v1, v2;
	Edge edge;
	char s1[3], s2[3];
	for (int i = 0; i < k; ++i)
	{
		scanf("%s%s%d", s1, s2, &edge.dist);
		v1 = getIndex(s1, m);
		v2 = getIndex(s2, m);
		edge.next = v1;
		vec[v2].push_back(edge);
		edge.next = v2;
		vec[v1].push_back(edge);	
	}
	//Dijkstra 算法,计算最短路径
	solu.resize(m);
	for (int i = 0; i < m; ++i)//计算从候选站点开始到每个房站点的距离
	{
		visit.clear();
		visit.resize(n + m, false);
		Dist.clear();
		Dist.resize(n + m, INT_MAX);
		Dist[i] = 0;
		int j = 0;
		while(j < m + n)
		{
			//寻找最小长度
			int minIndex = -1, minDist = INT_MAX;

			for (int t = 0; t < m + n; ++t)
			{
				if (visit[t] == false && Dist[t] < minDist)
				{
					minDist = Dist[t];
					minIndex = t;
				}
			}
			if (minIndex == -1 || minDist > d)
				break;
			visit[minIndex] = true;
			for (vector<Edge>::iterator iter = vec[minIndex].begin(); iter != vec[minIndex].end(); ++iter)
			{
				if (visit[iter -> next] == false && Dist[minIndex] + iter -> dist < Dist[iter -> next])
					Dist[iter -> next] = Dist[minIndex] + iter -> dist;
			}
			++j;
		}
		/*通不过的原因出现在这里,if (j < m + n)判断的是从候选点到其他点(候选点和房点)的具体不全部满足距离<d时,
		实质上,题目只要求候选点到放点的距离满足要求就可以了
		举个实际的例子
		1 2 1 2 
		1 G1 1
		上面的数据输入测试,结果为“No Solution", 分析过程很显然候选点有两个:G1和G2,G2肯定不满足,
		G1满足,但是在代码中由于还有判断G1到G2的距离,所以把G1误判为不可选,所以进行的改进是遍历Dist[m, m+n]来判断是否满足距离要求
		*/
		if (j < m + n)
			solu[i].flag = false;
		else
		{
			solu[i].flag = true;
			int sumDist = 0, minDist = INT_MAX;
			for (int j = m; j < m + n; ++j)
			{
				sumDist += Dist[j];
				minDist = min(minDist, Dist[j]);
			}

				solu[i].minDist = minDist;
				solu[i].aveDist = sumDist;
		}
	}
	int index = 0;
	while (index < m && solu[index].flag == false)
		++index;
	for (int i = index; i < m; ++i)
	{
		if (solu[i].flag == true && (solu[i].minDist > solu[index].minDist || (solu[i].minDist == solu[index].minDist && solu[i].aveDist < solu[index].aveDist)))
			index = i;
	}
	if (index < m)
		printf("G%d\n%.1f %.1f\n", index + 1, solu[index].minDist * 1.0, solu[index].aveDist * 1.0 / n);
	else
		printf("No Solution\n");

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值