1072.Gas Station

【题意】
        找一个到所有房屋最短距离最远,平均距离最近,且覆盖所有房屋的加油站

【思路】
        把住房和加油站都视作普通节点,对每个加油站用一次Dijkstra算法即可

【注意点】

        此题Dijkstra算法中可以不用遍历到每个点,只要把所有房屋都访问到了就可以了,即使有些加油站访问不到或者超出了服务范围也不要紧。


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
using namespace std;

#define MAXIMUM 0x0fffffff

int n,m,k,ds;
vector<bool> visited;
vector<int> dist;
vector<vector<int>> weight;

typedef struct{
	int index;
	int minDist;
	float avgDist;
}gasStation;

bool cmp(gasStation g1, gasStation g2){
	if(g1.minDist>g2.minDist){
		return 1;
	}
	else if(g1.minDist==g2.minDist && g1.avgDist<g2.avgDist){
		return 1;
	}
	else if(g1.minDist==g2.minDist && g1.avgDist==g2.avgDist && g1.index<g2.index){
		return 1;
	}
	else{
		return 0;
	}
}

int lable2index(string lable){
	int index = 0;

	if(lable[0]=='G'){
		for(int i=1; i<lable.length(); i++){
			index = index*10+lable[i]-'0';
		}
		index += n-1;
	}
	else{
		for(int i=0; i<lable.length(); i++){
			index = index*10+lable[i]-'0';
		}
		index--;
	}

	return index;
}

int main(int argc, char const *argv[])
{
	cin >> n >> m >> k >> ds;
	visited.resize(n+m);
	dist.resize(n+m);
	weight.resize(n+m,vector<int>(n+m));

	for(int i=0; i<n+m; i++){
		for(int j=0; j<=i; j++){
			if(i==j){
				weight[i][j] = 0;
			}
			else{
				weight[i][j] = weight[j][i] = MAXIMUM;
			}
		}
	}
	//0~n-1		:residential housing
	//n~n+m-1	:gas station
	for(int i=0; i<k; i++){
		string ends[2];
		int d,index[2];

		cin >> ends[0] >> ends[1] >> d;
		index[0] = lable2index(ends[0]);
		index[1] = lable2index(ends[1]);
		weight[index[0]][index[1]] = weight[index[1]][index[0]] = d;
	}
	
	vector<gasStation> stations;
	//Dijkstra
	for(int gasIndex=n; gasIndex<n+m; gasIndex++){
		visited.assign(n+m,0);
		dist.assign(n+m,MAXIMUM);
		visited[gasIndex] = 1;
		dist[gasIndex] = 0;
		bool valid = 1;
		int minDist = MAXIMUM;
		float avgDist = 0.0;
		
		int cnt = n;
		int index = gasIndex;
		//并不需要把每个节点都遍历到,只需要把n个城市遍历到即可
		while(cnt){
			int min = MAXIMUM;
			int nextIndex;
			for(int j=0; j<n+m; j++){
				if(!visited[j]){
					if(dist[index]+weight[index][j]<dist[j]){
						dist[j] = dist[index]+weight[index][j];
					}
					if(dist[j]<min){
						min = dist[j];
						nextIndex = j;
					}
				}
			}
			if(min>ds){
				valid = 0;
				break;
			}
			index = nextIndex;
			visited[index] = 1;
			//编号小于n的点代表住房
			if(index<n){
				cnt--;
				if(dist[index]<minDist){
					minDist = dist[index];
				}
				avgDist += dist[index];
			}
		}
		avgDist /= n;
		if(valid){
			gasStation tmp;
			tmp.index = gasIndex;
			tmp.minDist = minDist;
			tmp.avgDist = avgDist;
			stations.push_back(tmp);
		}
	}

	if(stations.empty()){
		cout << "No Solution";
	}
	else{
		sort(stations.begin(),stations.end(),cmp);
		cout << 'G' << stations[0].index-n+1 << endl;
		printf("%d.0 %.1f", stations[0].minDist, stations[0].avgDist);
	}

	system("pause");
	return 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、付费专栏及课程。

余额充值