1072 Gas Station [dj]
程序员文章站
2022-06-07 11:38:33
...
题目大意:找出加油站要尽可能远离居民区 输出这个加油站到居民区最短距离和平均距离
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<math.h>
#include<algorithm>
#include<map>
#include<cstring>
#include<set>
using namespace std;
const int maxn = 1011;
const int inf = 1000000001;
int n, m, k, ds;
int d[maxn];
int G[maxn][maxn];
bool vis[maxn];
void dj(int s)
{
memset(vis, false, sizeof(vis));
fill(d, d + maxn, inf);
d[s] = 0;
for (int i = 1; i <= n+m; i++)
{
int u = -1, min = inf;
for (int j = 1; j <= n + m; j++)
{
if (vis[j] == false && d[j] < min)
{
u = j;
min = d[j];
}
}
if (u == -1) return;
vis[u] = true;
for (int v = 1; v <= n + m; v++)
{
if (vis[v] == false)
{
if (d[u] + G[u][v] < d[v])
{
d[v] = d[u] + G[u][v];
}
}
}
}
}
int strtoInt(string s)
{
int i = 0, len = s.size(), id = 0;
while (i < len)
{
if (s[i] != 'G')
{
id = id * 10 + (s[i] - '0');
}
i++;
}
if (s[0] == 'G') return n + id;
else return id;
}
int main()
{
cin >> n >> m >> k >> ds;
string s1, s2;
int u, v, q;
fill(G[0], G[0] + maxn * maxn, inf);
for (int i = 1; i <= k; i++)
{
cin >> s1 >> s2 >> q;
u = strtoInt(s1);
v = strtoInt(s2);
G[u][v] = G[v][u] = q;
}
double Mindis = -1, id = -1, Avq = inf;
for (int i = n + 1; i <= n + m; i++)
{
double mindis = inf, avq = 0;
dj(i);
for (int j = 1; j <= n; j++)
{
if (d[j] > ds)
{
mindis = -1;
break;
}
if (d[j] < mindis)
{
mindis = d[j];
}
avq += 1.0 * d[j] / n;
}
if (mindis == -1) continue;
if (mindis > Mindis)
{
id = i;
Mindis = mindis;
Avq = avq;
}
else if (mindis == Mindis && avq < Avq)
{
id = i;
Avq = avq;
}
}
if (id == -1) cout << "No Solution" << endl;
else
{
cout << "G"<< id - n << endl;
printf("%.1f %.1f\n", Mindis, Avq);
}
return 0;
}