1072 Gas Station

题目大意

在居民户间建加油站,从m个备选加油站点里面选取1个站点,这个站点到其中一户的最短距离是其他站点最短距离中最长的,(再直白一点的说,这几个站大家都拿出到所有居民户的最短距离,最短距离里最长的就是所要的)但是这个距离不能超出服务范围ds。如果有多个符合要求的站,输出距离所有居民区距离平均值最小的那个,如果平均值还是一样,就输出按照顺序排列加油站编号最小的那个。

思路解析

还是用最短路径算法,稍微变通一点的是,把G1 G2...这些编号转换成数字,这样才好遍历图,转换算法很简单,就是加在1...n后面,当做普通节点就好,然后对n后面的加油站节点进行dijkstra,按题目要求选就可以。

示例代码

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#define INF (~(0x1<<31))
using namespace std;
int gra[1100][1100];
int n, m, k, ds, mind = 0,minnode = -1;
double average = INF;
vector<int> distd;
int toint(string str) {
	return str[0] == 'G'? n + stoi(str.substr(1, str.length() - 1)): stoi(str);
}
void dijsktra(int v) {
	vector<bool> visit(1100);
	vector<int> dist(1100, INF);
	dist[v] = 0;
	for (int i = 0; i < n + m; i++) {//执行n+m次
		int min = INF, u = -1;
		for (int j = 1; j <= n + m; j++) {
			if (!visit[j] && dist[j] < min) {
				u = j;
				min = dist[j];
			}
		}
		if (u == -1) break;
		visit[u] = true;
		for (int j = 1; j <= n + m; j++) {//更新
			if (!visit[j] && gra[u][j] != INF) {
				if (dist[u] + gra[u][j] < dist[j]) 
				   dist[j] = dist[u] + gra[u][j];
			}
		}
	}
	int min = INF;
	double sum = 0;
	for (int i = 1; i <= n; i++) {//找最小节点
		if (dist[i] < min) {
			min = dist[i];
			sum += 1.0*dist[i];
		}
		if (dist[i] > ds) return;//说明该加油站不能覆盖所有住户
	}
	if ((min > mind) || (min == mind && sum/n < average)) {
		mind = min;
		minnode = v;
		average = sum / n;
		distd = dist;
	}
	return;
}
int main() {
	scanf("%d %d %d %d", &n, &m, &k, &ds);
	fill(gra[0] , gra[0] + 1100 * 1100, INF);
	for (int i = 0; i < k; i++) {
		string s1,s2;
		int d;
		cin >> s1 >> s2 >> d;
		int a = toint(s1);
		int b = toint(s2);
		gra[a][b] = gra[b][a] = d;
	}
	for (int i = n + 1; i <= n + m; i++) //从G1出发找最短路径
		dijsktra(i);
	if (minnode == -1) {
		cout << "No Solution";
		return 0;
	}
	cout << "G" << minnode - n << endl;
	int sum = 0;
	for (int i = 1; i <= n; i++)
		sum += distd[i];
	printf("%.1f %.1f", (double)mind,(double)sum/n);
	return 0;
}
  • 1
    点赞
  • 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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值