国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

PAT甲級1072

2019-11-11 04:14:06
字體:
來源:轉載
供稿:網友

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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 进贤县| 内丘县| 遂昌县| 陆河县| 洛南县| 林芝县| 华蓥市| 湖州市| 五常市| 曲靖市| 石家庄市| 玉山县| 榕江县| 图们市| 师宗县| 永新县| 福安市| 海门市| 万全县| 娱乐| 当雄县| 廉江市| 西平县| 南澳县| 民和| 汉寿县| 任丘市| 法库县| 宜州市| 长武县| 调兵山市| 寿宁县| 泊头市| 巴中市| 泌阳县| 乳源| 西峡县| 永清县| 赣榆县| 松溪县| 饶河县|