日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

pat 甲级 1072. Gas Station (30)

發布時間:2023/12/1 编程问答 56 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pat 甲级 1072. Gas Station (30) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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 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

題意:最短路,找出一個建立加油站的合適地方。現在有幾個備選的地方。按照如下規則篩選:
1:加油站與所有住宅區的的最小距離越大越好。
2:加油站與所有住宅區的平均距離越小越好。
3:挑選編號數值最小的加油站。
AC 代碼: #define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<algorithm> #include<cmath> #include<cstring> #include<string> #include<set> #include<queue> #include<map> using namespace std; #define INF 0x3f3f3f #define N_MAX 1000+20 typedef long long ll; int n,m,k,d_max; struct edge {int to, cost;edge() {}edge(int to ,int cost):to(to),cost(cost) {} }; vector<edge>G[N_MAX]; struct P {int first, second;P() {}P(int first,int second):first(first),second(second) {}bool operator < (const P&b)const {return first > b.first;} }; int d[N_MAX]; int V; void dijkstra(int s) {priority_queue<P>que;fill(d, d + V, INF);d[s] = 0;que.push(P(0,s));while (!que.empty()) {P p = que.top(); que.pop();int v = p.second;if (d[v] < p.first)continue;for (int i = 0; i < G[v].size();i++) {edge e = G[v][i];if (d[e.to]>d[v]+e.cost) {d[e.to] = d[v] + e.cost;que.push(P(d[e.to], e.to));}}} }int translation(string s) {if (s[0] == 'G') {if (s.size() == 3)return m+n;//m最大為10,唯一的三位數else return s[1] - '0'+n;}else {return atoi(s.c_str());} } string recover(int id) {string s="G";s += '0' + id-n;return s; }int main() {while (cin>>n>>m>>k>>d_max) {V = n + m+1;for (int i = 0; i < k;i++) {string from, to; int cost;cin >> from >> to >> cost;G[translation(from)].push_back(edge(translation(to), cost));G[translation(to)].push_back(edge(translation(from), cost));}double max_mindist = -1, max_avedist = -1; int id;for (int i = 1; i <= m;i++) {//對于每一個stationbool flag = 1;//判斷當前情況是否可以int pos = n + i;dijkstra(pos);double tmp_ave = 0;int tmp_min = INF;for (int j = 1; j <= n; j++) {if (d[j] > d_max) {flag = 0;break;}tmp_min = min(d[j], tmp_min);tmp_ave += d[j];}if (!flag)continue;tmp_ave /= (double)n;if (tmp_min > max_mindist) {max_mindist=tmp_min;max_avedist = tmp_ave;id = pos;}else if (tmp_min == max_mindist&&tmp_ave < max_avedist) {max_avedist=tmp_ave;id = pos;}else if (tmp_min == max_mindist&&tmp_ave == max_avedist&& id>pos) {id = pos;}}if (max_mindist == -1)puts("No Solution");else {cout << recover(id) << endl;printf("%.1f %.1f\n",max_mindist,max_avedist);}}return 0; }

?

轉載于:https://www.cnblogs.com/ZefengYao/p/8556088.html

總結

以上是生活随笔為你收集整理的pat 甲级 1072. Gas Station (30)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。