PAT 1072. Gas Station (dijkstra)

问题描述:

n m k d

n个房子,m个加油站候选地址,k条无向路,每个加油站的最大加油范围为d

选择一个候选地址,最大化“所有房子离加油站距离的最小值”。

当候选地址不唯一时,选择使得所有房子平均距离最小的那个地址;

候选地址仍不唯一时,选择index最小的那个。


n <= 1000, m <= 10, 对每个候选点用dijkstra算一遍就可以了

开始想错了,以为要开map[10000][10000]的数组,以为复杂度是10000*10000*10, 所以用了 list<int> edge_house[1010], edge_station[11]这样的邻接表

实际上是没必要的,直接map[1100][1100]就可以了。


代码:

#include <list>
#include <iostream>
#include <sstream>
#include <cstring>
#include <string>

using namespace std;

const int INF = 2100000000;
int dis_house[1005], dis_station[11];
list<int> edge_house[1005], edge_station[11];
bool vi[1011];
int n, m, k, d;
int map[1100][1100], mini_dis[11];
double avg_dis[11];

inline int string_to_int(const string& s)
{
	int ret;
	stringstream ss;
	if (s[0] != 'G')
	{
		ss << s;
		ss >> ret;
	} else
	{
		ss << s.substr(1);
		ss >> ret;
		ret += 1000;
	}
	return ret;
}

void input()
{	
	for (int i = 1; i <= 1010; ++ i)
	{
		for (int j = 1; j <= 1010; ++ j)
		{
			map[i][j] = INF;
		}
	}
	cin >> n >> m >> k >> d;
	for (int i = 0; i < k; ++ i)
	{
		string s1, s2; 
		int dis, x, y;
		cin >> s1 >> s2 >> dis;
		x = string_to_int(s1);
		y = string_to_int(s2);

		if (x > 1000)
		{
			edge_station[x-1000].push_back(y);
		} else
		{
			edge_house[x].push_back(y);
		}
		if (y > 1000)
		{
			edge_station[y-1000].push_back(x);
		} else
		{
			edge_house[y].push_back(x);
		}
		map[x][y] = map[y][x] = dis;
	}
}

void init()
{
	for (int i = 1; i <= 1000; ++ i)
	{
		dis_house[i] = INF;
		vi[i] = false;
	}
	for (int i = 1; i <= 10; ++ i)
	{
		dis_station[i] = INF;
		vi[i+1000] = false;
	}
}

void dijkstra(int locate)
{
	init();
	dis_station[locate] = 0;
	
	for (int times = 0; times < n+m; ++ times)
	{
		int mini = INF, v = -1;
		for (int i = 1; i <= n; ++ i)
		{
			if (dis_house[i] < mini && vi[i] == false)
			{
				mini = dis_house[i];
				v = i;
			}
		}
		for (int i = 1; i <= m; ++ i)
		{
			if (dis_station[i] < mini && vi[i+1000] == false)
			{
				mini = dis_station[i];
				v = i + 1000;
			}
		}
		
		if (v == -1)
		{
			break;
		}
		vi[v] = true;

		if (v > 1000)
		{
			for (auto it = edge_station[v-1000].begin(); it != edge_station[v-1000].end(); ++ it)
			{
				if (*it > 1000)
				{
					if (dis_station[*it-1000] > dis_station[v-1000] + map[v][*it])
					{
						dis_station[*it-1000] = dis_station[v-1000] + map[v][*it];
					}
				} else
				{
					if (dis_house[*it] > dis_station[v-1000] + map[v][*it])
					{
						dis_house[*it] = dis_station[v-1000] + map[v][*it];
					}
				}
			}
		} else // < 1000 
		{
			for (auto it = edge_house[v].begin(); it != edge_house[v].end(); ++ it)
			{
				if (*it > 1000)
				{
					if (dis_station[*it-1000] > dis_house[v] + map[v][*it])
					{
						dis_station[*it-1000] = dis_house[v] + map[v][*it];
					}
				} else
				{
					if (dis_house[*it] > dis_house[v] + map[v][*it])
					{
						dis_house[*it] = dis_house[v] + map[v][*it];
					}
				}				
			}
		}
	}

	mini_dis[locate] = INF;
	avg_dis[locate] = 0;
	for (int i = 1; i <= n; ++ i)
	{
		avg_dis[locate] += dis_house[i];
		if (dis_house[i] > d)
		{
			mini_dis[locate] = -1;
			break;
		} else if (dis_house[i] < mini_dis[locate])
		{
			mini_dis[locate] = dis_house[i];
		}
	}
	avg_dis[locate] /= n;
}

void output()
{
	double minimum_dis = 0;
	int index = -1;
	for (int i = 1; i <= m; ++ i)
	{
		if (mini_dis[i] != -1 && mini_dis[i] > minimum_dis)
		{
			minimum_dis = mini_dis[i];
			index = i;
		} else if (mini_dis[i] == minimum_dis 
			&& (avg_dis[i] < avg_dis[index]) || (avg_dis[i]==avg_dis[index] && i < index))
		{
			index= i;
		}
	}
	if (index == -1)
	{
		printf("No Solution\n");
	} else
	{
		printf("G%d\n", index);
		printf("%.1lf %.1lf\n", mini_dis[index]*1., avg_dis[index]);
	}
}

int main()
{
	input();

	for (int i = 1; i <= m; ++ i)
	{
		dijkstra(i);
	}

	output();

	return 0;
}


你可能感兴趣的:(C++,dijkstra,pat)