A1072 Gas Station 🔥🔥🔥
💙content
https://pintia.cn/problem-sets/A1072
condition 1 : the minimum distance between the station and any of the residential housing is as far away as possible
condition 2 : output the one with the smallest average distance to all the houses
condition 3 : output the one with the smallest index number
🐶【Dijkstra】
🛠 skill
using vector + struct + sort ---->function bool cmp() ---->to realize three-level sort
easy but complex
bool cmp(gas g1 , gas g2){
if(g1.mind > g2.mind){
return true;
}
else{
if(g1.mind == g2.mind){
if(g1.averd < g2.averd) {
return true;
}
else{
if(g1.averd == g2.averd){
if(g1.id < g2.id){
return true;
}
else{
return false;
}
}
else{
return false;
}
}
}
else{
return false;
}
}
}
❗️issue
how to convert “G1” to int , N is the number of housing
so that [0 , N-1] is residential housing , [N , N + M -1] is station;
int reformat(string a){
if(a[0] == 'G'){
a[0] = ' ';
return stoi(a) + N - 1;
}
else{
return stoi(a) - 1;
}
}
💥 error
1.wrong code for Dijkstra when finding the u and MIN
👇 is not u , it is v
int u = -1 , MIN = INF;
for(int j = 0; j != V; j++){
if(vis[u] == false && d[u] < MIN ){
u = j;
MIN = d[u];
}
}
correct
int u = -1 , MIN = INF;
for(int j = 0; j != V; j++){
if(vis[j] == false && d[j] < MIN ){
u = j;
MIN = d[j];
}
}
2.using printf("%.1f") to print 3.25 ,fucking result is 3.2
solution ----> + 0.005
printf("%.1f %.1f" ,g.begin()->mind + 0.005, g.begin()->averd + 0.005);
⭐️solution
!!!key!!!—>
💻 code
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1020;
const int INF = 10000000;
int G[maxn][maxn] , d[maxn];
vector<int> pre[maxn];
bool vis[maxn] = {false};
int N , M , K ,ds;
int V;
struct gas{
int id ;
double mind , averd;
gas(int a , double b , double c){
id = a ;
mind = b ;
averd = c;
}
};
vector<gas> g;
bool cmp(gas g1 , gas g2){
if(g1.mind > g2.mind){
return true;
}
else{
if(g1.mind == g2.mind){
if(g1.averd < g2.averd) {
return true;
}
else{
if(g1.averd == g2.averd){
if(g1.id < g2.id){
return true;
}
else{
return false;
}
}
else{
return false;
}
}
}
else{
return false;
}
}
}
void dij(int s){
fill(d , d + maxn , INF);
memset(vis , false , sizeof(vis));
d[s] = 0;
for(int i = 0; i != V; i++){
int u = -1 , MIN = INF;
for(int j = 0; j != V; j++){
if(vis[j] == false && d[j] < MIN ){
u = j;
MIN = d[j];
}
}
if(u == -1) return;
vis[u] = true;
for(int v = 0; v != V; v++){
if(vis[v] == false && G[u][v] != INF){
if(G[u][v] + d[u] < d[v]){
d[v] = G[u][v] + d[u];
}
}
}
}
}
int reformat(string a){
if(a[0] == 'G'){
a[0] = ' ';
return stoi(a) + N - 1;
}
else{
return stoi(a) - 1;
}
}
void calDis(){
for(int i = N; i != V; i++){
dij(i);
double mind = INF, averd = 0;
int flag = 0;
for(int j = 0; j != N; j++){
if(d[j] > ds) {
flag = 1;
break;
}
if(d[j] < mind) {
mind = d[j];
}
averd += d[j];
}
if(flag != 1){
averd = 1.0 * averd / N;
// cout<< i <<" "<< mind <<" "<< averd<<endl;
gas temp(i , mind , averd);
g.push_back(temp);
}
}
}
int main()
{
scanf("%d%d%d%d" , &N , &M , &K ,&ds);
V = N + M;
fill(G[0] , G[0] + maxn * maxn , INF);
for(int i = 0; i != K; i++){
string s1, s2 ;
int a , b, dd;
cin>>s1>>s2>>dd;
a = reformat(s1);
b = reformat(s2);
//cout<< a <<" "<< b<<endl;
G[a][b] = G[b][a] = dd;
}
calDis();
if(g.size() == 0){
cout << "No Solution";
}
else{
sort(g.begin() , g.end() , cmp);
printf("G%d\n" , g.begin()->id - N + 1);
printf("%.1f %.1f" ,g.begin()->mind + 0.005, g.begin()->averd + 0.005);
}
return 0;
}