当前位置:网站首页>L3-005 Litter box distribution
L3-005 Litter box distribution
2022-08-08 04:08:00 【Cod_ing】
Summarize the requirements for the final output:
①Dumpsters are far from settlements“最短距离”最大的优先.
②在①相同的情况下,The one with the smallest average distance takes precedence.
③在②相同的情况下,编号小的优先.
If neither match, output a string.
The point of this question isDijkstra算法.
1、Therefore, the input points need to be preprocessed,Known settlements areN个,Then the bins are sorted in order(N+1…N+2…N+M)即可;
2、Ask for every piece of junk单源最短路径;
3、根据Ds筛选符合条件的
4、Output after custom sorting
大家倒垃圾的时候,都希望垃圾箱距离自己比较近,但是谁都不愿意守着垃圾箱住.所以垃圾箱的位置必须选在到所有居民点的最短距离最长的地方,同时还要保证每个居民点都在距离它一个不太远的范围内.
现给定一个居民区的地图,以及若干垃圾箱的候选地点,请你推荐最合适的地点.如果解不唯一,则输出到所有居民点的平均距离最短的那个解.如果这样的解还是不唯一,则输出编号最小的地点.
输入样例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<string>
#include<string.h>
#include<vector>
#include<algorithm>
#include<stdio.h>
using namespace std;
int N, M, K, D;
int map[1015][1015];
int dist[1015];
bool vis[1015];
struct node {
int num;
int minn, sum;
};
vector<node> ans;
void dijkstra(int n) {
memset(vis, false, sizeof(vis));
memset (dist, 0x3f, sizeof(dist));
int minn,t;
vis[n] = true;
for (int i = 1; i <= N + M; i++) {
if (map[n][i] < 0x3f3f3f3f) dist[i] = map[n][i];
}
for (int i = 1; i <= N +M; i++) {
minn = 0x3f3f3f3f;
for (int j = 1; j <= N +M; j++) {
if (!vis[j] && minn > dist[j]) {
minn = dist[j];
t = j;
}
}
vis[t] = true;
for (int j = 1; j <= N +M; j++) {
if (!vis[j] && dist[j] > dist[t] + map[t][j])
dist[j] = dist[t] + map[t][j];
}
}
}
void check(int n) {
int sum = 0;
int minn = dist[1];
node res;
for (int i = 1; i <= N; i++) {
if (dist[i] > D) return;
if (dist[i] < minn) minn = dist[i];
sum += dist[i];
}
res.num = n;
res.sum = sum;
res.minn = minn;
ans.push_back(res);
}
bool cmp(node a, node b) {
if (a.minn != b.minn) return a.minn > b.minn;
if (a.sum != b.sum) return a.sum < b.sum;
return a.num < b.num;
}
#define trans(s)(s[0]=='G'?stoi(s.substr(1))+N:stoi(s))
int main() {
cin >> N >> M >> K >> D;
memset(map, 0x3f, sizeof(map));
string s1, s2;
int x,y,z;
for (int i = 0; i < K; i++) {
cin >> s1 >> s2 >> z;
x = trans(s1);
y = trans(s2);
map[x][y] = map[y][x] = z;
}
for (int i =1; i <=M; i++) {
dijkstra(i+N);
check(i);
}
if (ans.empty()) cout << "No Solution";
else {
sort(ans.begin(), ans.end(), cmp);
cout <<"G" << ans.front().num << endl;
printf("%.1f %.1f", 1.0*ans.front().minn,1.0*ans.front().sum/N);
}
return 0;
}
边栏推荐
- Nanny level tutorial!Golang microservices simple architecture in practice
- Inside outside l think MindSpore AI framework, heavy industry gathering, huawei big extraordinary path of the model
- 32. Do you know how Redis strings are implemented?
- Simulate login - add cookies, use postmanget to request web page data
- The use of mmedicting get_flops. Py
- Voice identification software
- 以0为底或以1为底对图片迭代次数的影响
- 一文带你彻底了解synchronized 和 Lock
- The effect of base 0 or base 1 on the number of image iterations
- NorFlash的存储原理
猜你喜欢

32. Do you know how Redis strings are implemented?

强网杯 2019-随便注 (堆叠注入)

Amazon Cloud Technology Build On Learning Experience

leetcode: 874. 模拟行走机器人

Week 4 Step by step building multi-layer neural network and application (1 & 2)

Inside outside l think MindSpore AI framework, heavy industry gathering, huawei big extraordinary path of the model

leetcode 112. Path sum recursion

高效记忆法

The ORACLE database connection exception after restart

VSCode opens some records of C (embedded) projects
随机推荐
Knowledge of DisplayPort-DP interface
实现Express中间件原理
模拟登录——添加cookies,使用postmanget请求网页数据
The JVM tuning strategy
[Graph Basics] How to Define Few-Shot Learning on Heterogeneous Graphs: Heterogeneous Graph Few-Shot Learning
32. Do you know how Redis strings are implemented?
Simulate login - add cookies, use postmanget to request web page data
流程控制语句顺序分支循环结构
The live broadcast of agricultural products continues to heat up, Economic Daily: Don’t forget quality when rushing
MySQL——索引与事务
风控策略必学|这种用决策树来挖掘规则的方法
【opencv】opencv开发包简介
New retail project and offline warehouse core interview,, 220807,,
第4周 一步步搭建多层神经网络以及应用(1 & 2)
使用 Presto 和 Alluxio 在 AWS 上搭建高性能平台来支持实时游戏服务
ICML 2022 | LeNSE: Subgraph-Based Large-Scale Combinatorial Optimization: Achieving Over 140x Speedup
egg-session 将数据存储到redis
文本生成介绍
torch.view() function usage
Voice identification software