L3-005. 垃圾箱分布(dijkstra模版题)

L3-005. 垃圾箱分布

时间限制
200 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越

大家倒垃圾的时候,都希望垃圾箱距离自己比较近,但是谁都不愿意守着垃圾箱住。所以垃圾箱的位置必须选在到所有居民点的最短距离最长的地方,同时还要保证每个居民点都在距离它一个不太远的范围内。

现给定一个居民区的地图,以及若干垃圾箱的候选地点,请你推荐最合适的地点。如果解不唯一,则输出到所有居民点的平均距离最短的那个解。如果这样的解还是不唯一,则输出编号最小的地点。

输入格式:

输入第一行给出4个正整数:N(<= 103)是居民点的个数;M(<= 10)是垃圾箱候选地点的个数;K(<= 104)是居民点和垃圾箱候选地点之间的道路的条数;DS是居民点与垃圾箱之间不能超过的最大距离。所有的居民点从1到N编号,所有的垃圾箱候选地点从G1到GM编号。

随后K行,每行按下列格式描述一条道路:
P1 P2 Dist
其中P1和P2是道路两端点的编号,端点可以是居民点,也可以是垃圾箱候选点。Dist是道路的长度,是一个正整数。

输出格式:

首先在第一行输出最佳候选地点的编号。然后在第二行输出该地点到所有居民点的最小距离和平均距离。数字间以空格分隔,保留小数点后1位。如果解不存在,则输出“No Solution”。

输入样例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
输出样例1:
G1
2.0 3.3
输入样例2:
2 1 2 10
1 G1 9
2 G1 20
输出样例2:
No Solution

// 将垃圾桶也当作地图上的一个点,然后对每个垃圾桶求它到其他点的最短距离,每次求完后需要保存它距离其他居民点的最短距离和总距离
// 我们是从第一个垃圾桶开始计算的,所以只需要考虑到“最短距离最长的地方” 和 “到所有居民点的平均距离最短”这两个条件。
#include <iostream>
#include <vector>
#include <string>
#define INF 1 << 30
using namespace std;
int N, M, K, D;
struct point {
	int w;
	int no;
	point(int w, int no) {
		this->w = w;
		this->no = no;
	}
};
void dijkstra(int start, vector< vector<point> >& map, int cnt[2]) {
	vector<int> dis(N + M + 1, INF);
	vector<int> visit(N + M + 1, 0);
	dis[start] = 0;
	for (int i = 0; i < map[start].size(); i++) {
		dis[map[start][i].no] = map[start][i].w;
	}

	while (true) {
		int v;
		int min = INF;
		for (int j = 1; j <= N + M; j++) {
			if (visit[j] == 0 && dis[j] < min) {
				min = dis[j];
				v = j;
			}
		}
		if (min == INF)
			break;
		visit[v] = 1;
		for (int i = 0; i < map[v].size(); i++) {
			if (dis[map[v][i].no] > dis[v] + map[v][i].w)
				dis[map[v][i].no] = dis[v] + map[v][i].w;
		}
	}
	// 统计
	for (int i = 1; i <= N; i++) {
		if (dis[i] > D) {
			cnt[0] = 0;
			cnt[1] = 0;
		}
		if (dis[i] < cnt[1])
			cnt[1] = dis[i];
		cnt[0] += dis[i];
	}
}

int main() {
	//freopen("1.txt", "r", stdin);
	cin >> N >> M >> K >> D;
	vector< vector<point> > map(N + M + 1, vector<point>());
	for (int i = 0; i < K; i++) {
		string start_string, end_string;
		int dist, start, end;
		cin >> start_string >> end_string >> dist;
		if (start_string[0] == 'G')
			start = atoi(start_string.substr(1).c_str()) + N;
		else
			start = atoi(start_string.c_str());
		if (end_string[0] == 'G')
			end = atoi(end_string.substr(1).c_str()) + N;
		else
			end = atoi(end_string.c_str());
		map[start].push_back(point(dist, end));
		map[end].push_back(point(dist, start));
	}

	int min_dis = 0;
	double avg_dis = INF;
	int min_no;

	for (int i = N + 1; i <= N + M; i++) {
		int cnt[2] = { 0,INF };
		// cnt[0] == cnt_dis; cnt[1] == min_dis;
		// 正常的dijk,传一个数组进去,用来返回值。
		dijkstra(i, map, cnt);
		// 判断,如果cnt[0]为零,表示有大于Ds的点,这个垃圾桶抛弃;如果cnt[1] > mid_dis,表示满足最短距离最大这个条件,更新值;
		// 如果mid_dis == cnt[1],表示出现了相同的最大距离,则需要用第三个条件,也就是“平均距离最小”这个条件,如果满足,则更新值
		// 我们这里不判断平均值相同的情况,因为我们需要保证平均值相同时,编号最小。这样我们就省了一个排序的时间
		if (cnt[0] != 0 && (min_dis < cnt[1] || (min_dis == cnt[1] && ((double)cnt[0] / N) < avg_dis))) {
			min_dis = cnt[1];
			avg_dis = (double)cnt[0] / N;
			min_no = i;
		}
	}

	if (avg_dis == INF)
		cout << "No Solution";
	else {
		cout << "G" << min_no - N << endl;
		printf("%d.0 %.01lf", min_dis, avg_dis);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/wbb1997/article/details/80063153
今日推荐