首页 > 学院 > 开发设计 > 正文

PAT甲级1072

2019-11-11 05:42:22
字体:
来源:转载
供稿:网友

1072. Gas Station (30)

时间限制200 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, Yue

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 formatP1 P2 Distwhere 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 51 2 21 4 21 G1 41 G2 32 3 22 G2 13 4 23 G3 24 G1 3G2 G1 1G3 G2 2Sample Output 1:
G12.0 3.3Sample Input 2:
2 1 2 101 G1 92 G1 20Sample Output 2:
No Solution
#include<cstdio>#include<iostream>#include<vector>#include<map>#include<string>#include<algorithm>#include<queue>using namespace std;struct Node{	int v, dis;}node;struct compare{	bool Operator()(Node n1, Node n2)	{		return n1.dis > n2.dis;	}};struct Result{	string solution;	double mindistance, averagedistance;	bool operator<(Result r)	{		if (mindistance != r.mindistance)			return mindistance > r.mindistance;//最大的最近距离,注意理解题意		else if (averagedistance != r.averagedistance)			return averagedistance < r.averagedistance;		else			return solution < r.solution;	}}result;int N, M, K, Ds;const int maxn = 1020;//这里注意maxn必须是1011以上const int INF = 1000000000;map<string, int> gas2num;map<int,string> num2gas;vector<Node> Adj[maxn];bool vis[maxn];int d[maxn];vector<Result> solutions;void Dijkstra(int s){	fill(vis, vis + maxn, false);	fill(d, d + maxn, INF);	d[s] = 0;	priority_queue<Node, vector<Node>, compare>Q;	node.v = s; node.dis = 0;	Q.push(node);	int u;	for (int i = 1; i <= N + M; i++)	{		if (!Q.empty())		{			u = Q.top().v;			vis[u] = true;			Q.pop();		}		for (int j = 0; j < Adj[u].size(); j++)		{			int v = Adj[u][j].v;			int dis = Adj[u][j].dis;			if (!vis[v]&&d[u]+dis<d[v])			{				d[v] = d[u] + dis;				node.v = v; node.dis = d[v];				Q.push(node);			}		}	}	double sum = 0.0;	result.mindistance = INF;	for (int i = 1; i <= N; i++)	{		if (d[i] > Ds)return;		else sum += d[i];		if (result.mindistance > d[i])		{			result.mindistance = d[i];		}	}	result.solution = num2gas[s];	result.averagedistance = sum / N;	solutions.push_back(result);}int main(){	scanf("%d%d%d%d", &N, &M, &K, &Ds);	string s1, s2; int distance;	int n = N;	for (int i = 0; i < K; i++)	{		cin >> s1 >> s2>>distance;		int v1, v2;		if (s1[0] != 'G')		{			//v1 = s1[0] - '0';这个地方注意若输入的是10以上的数就不行			v1 = stoi(s1);		}		else		{			if (gas2num.find(s1) == gas2num.end())			{				v1 = gas2num[s1] = ++n;				num2gas[n] = s1;			}			else				v1 = gas2num[s1];		}		if (s2[0] != 'G')		{			//v2 = s2[0] - '0';			v2 = stoi(s2);		}		else		{			if (gas2num.find(s2) == gas2num.end())			{				v2 = gas2num[s2] = ++n;				num2gas[n] = s2;			}			else				v2 = gas2num[s2];		}		node.v = v2; node.dis = distance;		Adj[v1].push_back(node); node.v = v1;		Adj[v2].push_back(node);	}	for (int i = N + 1; i <= N + M; i++)	{		Dijkstra(i);	}	if (solutions.size())	{		sort(solutions.begin(), solutions.end());		cout << solutions[0].solution << endl;		printf("%.1f %.1f/n", solutions[0].mindistance, solutions[0].averagedistance);	}	else		printf("No Solution/n");	return 0;}
上一篇:网络基础

下一篇:C#之MySql更新

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表