1072 Gas Station (30分)

在这里插入图片描述
在这里插入图片描述
对每个候选的加油站点都用一次Dijkstra,根据题意找出最符合要求的站点。

  • 注意一些初始值的设置,比如bestLocation初始值-1,表示No Solution;
  • shortest记录加油站到住宅的最小距离,初始值为0,保证了当Dijkstra找到了第一个可行的站点时能够更新;
  • 因为要求当最小距离shortest相等时,取平均距离最小的,亦即总距离totalDis最小,所以totalDis的初始值为inf。

总而言之,题目要求什么,各变量初始值就反其道而行之,以确保结果能够得到正确更新。

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
const int inf = 0x7fffffff;
int numHouse, numCandidate, numRoad, range;
int map[1024][1024];
int minDistance[1024];	//Dijkstra中用来保存当前从起点到各个地点的最短距离,各地点起始距离为int最大值,仅起点起始距离为零
bool visited[1024];
//获取某个地点的编号,将带G前缀的地点放到数组末尾
int getIndex(string pos) {
	if (pos[0] == 'G') {
		string temp = pos.substr(1, pos.size() - 1);
		int offset = stoi(temp);
		return offset + numHouse;
	}
	else return stoi(pos);
}
int main() {
	cin >> numHouse >> numCandidate >> numRoad >> range;
	fill(map[0], map[0] + 1024 * 1024, inf);
	for (int i = 0; i < numRoad; i++) {
		string s1, s2;
		int dis, p1, p2;
		cin >> s1 >> s2 >> dis;
		p1 = getIndex(s1);
		p2 = getIndex(s2);
		map[p1][p2] = dis;
		map[p2][p1] = dis;
	}
	int bestLocation = -1, shortest = 0, totalDis = inf;
	for (int k = 1; k <= numCandidate; k++) {
		fill(minDistance, minDistance + 1024, inf);
		fill(visited, visited + 1024, false);
		minDistance[k + numHouse] = 0;
		for (int i = 1; i <= numHouse + numCandidate; i++) {
			int u = -1, minDis = inf;
			for (int j = 1; j <= numHouse + numCandidate; j++) {
				if (!visited[j] && minDis > minDistance[j]) {
					minDis = minDistance[j];
					u = j;
				}
			}
			if (u == -1) {
				break;
			}
			visited[u] = true;
			for (int v = 1; v <= numHouse + numCandidate; v++) {
				if (!visited[v] && map[u][v] != inf) {
					if (map[u][v] + minDis < minDistance[v]) {
						minDistance[v] = map[u][v] + minDis;
					}
				}
			}
		}
		int total = 0, min = inf, farthest = 0;
		//找出最远的和最近的距离,只有最远的距离在服务范围内并且最近的距离小于当前最小的距离时更新bestLocation
		for (int i = 1; i <= numHouse; i++) {
			if (minDistance[i] < min) {
				min = minDistance[i];
			}
			if (farthest < minDistance[i]) {
				farthest = minDistance[i];
			}
			total += minDistance[i];
		}
		if (farthest <= range && (min > shortest || 
			min == shortest && total < totalDis || 
			min == shortest && total == totalDis && bestLocation > k)) {
			shortest = min;
			totalDis = total;
			bestLocation = k;
		}
	}
	if (shortest == 0) {
		printf("No Solution");
	} else printf("%s\n%.1f %.1f\n", ("G" + to_string(bestLocation)).c_str(), (double)shortest, (double)totalDis / numHouse);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用c++解决pipeline system consists of N transfer station, some of which are connected by pipelines. For each of M pipelines the numbers of stations A[i] and B[i], which are connected by this pipeline, and its profitability C[i] are known. A profitability of a pipeline is an amount of dollars, which will be daily yielded in taxes by transferring the gas through this pipeline. Each two stations are connected by not more than one pipeline. The system was built by Soviet engineers, who knew exactly, that the gas was transferred from Ukrainian gas fields to Siberia and not the reverse. That is why the pipelines are unidirectional, i.e. each pipeline allows gas transfer from the station number A[i] to the station number B[i] only. More over, if it is possible to transfer the gas from the station X to the station Y (perhaps, through some intermediate stations), then the reverse transfer from Y to X is impossible. It is known that the gas arrives to the starting station number S and should be dispatched to the buyers on the final station number F. The President ordered the Government to find a route (i.e. a linear sequence of stations which are connected by pipelines) to transfer the gas from the starting to the final station. A profitability of this route should be maximal. A profitability of a route is a total profitability of its pipelines. Unfortunately, the President did not consider that some pipelines ceased to exist long ago, and, as a result, the gas transfer between the starting and the final stations may appear to be impossible... Input The first line contains the integer numbers N (2 ≤ N ≤ 500) and M (0 ≤ M ≤ 124750). Each of the next M lines contains the integer numbers A[i], B[i] (1 ≤ A[i], B[i] ≤ N) and C[i] (1 ≤ C[i] ≤ 10000) for the corresponding pipeline. The last line contains the integer numbers S and F (1 ≤ S, F ≤ N; S ≠ F). Output If the desired route exists, you should output its profitability. Otherwise you should output "No solution".
最新发布
05-28
这是一个经典的图论问题,可以使用Dijkstra算法或Bellman-Ford算法解决。以下是使用Dijkstra算法的步骤: 1. 定义一个数组dist,其中dist[i]表示从起点S到第i个站点的最大收益。 2. 初始化dist数组,将起点S的dist[S]赋值为0,其他点的dist[i]赋值为负无穷。 3. 定义一个优先队列pq,将起点S加入队列中。 4. 当pq非空时,取出队列中dist最小的站点u。 5. 对于每个与站点u相邻的站点v,如果通过从u到v的边可以获得更高的收益,则更新dist[v]为dist[u]+边(u,v)的收益,并将v加入队列pq中。 6. 重复步骤4和步骤5,直到队列pq为空。 7. 最终dist[F]即为最大收益。如果dist[F]为负无穷,则表示从起点S无法到达终点F,输出"No solution"。 以下是使用Dijkstra算法解决此问题的C++代码实现: ```c++ #include <iostream> #include <vector> #include <queue> using namespace std; const int INF = 0x3f3f3f3f; int main() { int n, m, s, f; cin >> n >> m; vector<vector<pair<int, int>>> g(n + 1); for (int i = 0; i < m; i++) { int a, b, c; cin >> a >> b >> c; g[a].push_back({b, c}); } cin >> s >> f; vector<int> dist(n + 1, -INF); dist[s] = 0; priority_queue<pair<int, int>> pq; pq.push({0, s}); while (!pq.empty()) { int u = pq.top().second; pq.pop(); for (auto p : g[u]) { int v = p.first; int w = p.second; if (dist[v] < dist[u] + w) { dist[v] = dist[u] + w; pq.push({dist[v], v}); } } } if (dist[f] == -INF) cout << "No solution" << endl; else cout << dist[f] << endl; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值