【C/C++】1072. Gas Station (30)

1072. Gas Station (30)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible. However it must guarantee that all the houses are in its service range.

Now given the map of the city and several candidate locations for the gas station, you are supposed to give the best recommendation. If there are more than one solution, output the one with the smallest average distance to all the houses. If such a solution is still not unique, output the one with the smallest index number.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 positive integers: N (<= 103), the total number of houses; M (<= 10), the total number of the candidate locations for the gas stations; K (<= 104), the number of roads connecting the houses and the gas stations; and DS, the maximum service range of the gas station. It is hence assumed that all the houses are numbered from 1 to N, and all the candidate locations are numbered from G1 to GM.

Then K lines follow, each describes a road in the format
P1 P2 Dist
where P1 and P2 are the two ends of a road which can be either house numbers or gas station numbers, and Dist is the integer length of the road.

Output Specification:

For each test case, print in the first line the index number of the best location. In the next line, print the minimum and the average distances between the solution and all the houses. The numbers in a line must be separated by a space and be accurate up to 1 decimal place. If the solution does not exist, simply output “No Solution”.




题意分析:
1.N户住宅 ,M个加油站待选地址,K条路(N+M个点),D加油站最远服务范围。
2.要求选一个加油站地址可以服务覆盖所有住宅。
3.从满足条件2的所有地址中选一个离住宅尽可能远的地址(即“the minimum distances between the solution and all the houses”要求见题any of the residential housing is as far away as possible)
4.从满足条件3的地址中选择一个平均距离最小的地址。
5.从满足条件4的地址选择一个索引号(下标)小的地址。

思路分析:通过Dijkstra算法遍历M个加油站,找出满足条件的一个。


#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#define MaxData 2002

typedef struct 
{
  int Ne, Nv;
  int length[MaxData][MaxData];
}GNode;
typedef GNode* graph;

typedef struct {
  int v1, v2;
  int len;
}ENode;
typedef ENode * edge;

graph createGraph(int size);
void insertEdge(graph map, edge e);
graph buildGraph(int N, int M, int routes);
int Toint(string v, int n);
void dijkstra(int v,int N,graph map, int ** dist, bool * visited);
int findMin(int v,int N,int ** dist, bool * visited);


int main()
{
  int N, M, K, D;
  cin >> N >> M >> K >> D;
  graph map = new GNode;
  int ** dist = new int*[N+M];
  bool * visited = new bool[N+M];
  for (int i = 0; i < N+M; i++)dist[i] = new int[N + M];
  for (int i = 0; i < N+M; i++)visited[i] = false;
  map = buildGraph(N, M, K);
  int size = N + M;
  float * res = new float[N];
  float * avg = new float[N];
  for (int i = 0; i < M; i++) {
    int minimum = 66535;
    float sum = 0; bool jud = true;
    dijkstra(N + i, size, map, dist, visited);
    for (int j = 0; j < N; j++) {
      if (dist[N + i][j] > D) {
        jud = false;
        break;
      }
      else {
        sum += dist[N + i][j];
        if (dist[N + i][j] < minimum) minimum = dist[N + i][j];
      }    
    }
    if (jud) {
      avg[i] = sum / N;
      res[i] = minimum; 
    }
    else { res[i] = -2; avg[i] = 65535; }
    for (int i = 0; i < N + M; i++)visited[i] = false;
  }
  int max = -1; int temp=-1;
  for (int i = 0; i < M; i++) {
    if(res[i]==-2)continue;
    if (res[i] >= max) {
      if (res[i] == max) {
        if (avg[i] < avg[temp])
          temp = i;
      }
      else {
        max = res[i];
        temp = i;
      }
    }
  }
  if (max == -1) cout << "No Solution";
  else {
    cout << 'G' << temp+1 << endl << fixed << setprecision(1) << res[temp] << ' ' << fixed << setprecision(1) << avg[temp];
  }
    return 0;
}

graph createGraph(int size) {
  graph map = new GNode;
  map->Nv = size;
  map->Ne = 0;
  for (int i = 0; i < size; i++)
    for (int j = 0; j < size; j++) {
      if (i == j) map->length[i][j] = 0;
      else map->length[i][j] = 65535;
    }
  return map;
}

void insertEdge(graph map, edge e) {
  map->length[e->v1][e->v2] = e->len;
  map->length[e->v2][e->v1] = e->len;
}

graph buildGraph(int N,int M,int routes) {
  graph map = new GNode;
  int size = N + M;
  map = createGraph(size);
  for(int i = 0; i < routes; i++) {
    string v1, v2;
    int n1, n2,l;
    cin >> v1 >> v2>>l;
    n1 = Toint(v1, N) - 1;
    n2 = Toint(v2, N) - 1;
    edge e = new ENode;
    e->v1 = n1; e->v2 = n2; e->len = l;
    insertEdge(map, e);
  }
  return map;
}

int Toint(string v,int n) {
  int len = v.length();
  int num = 0;
  if (v[0] == 'G') {
    for (int i = 1; i < len; i++) {
      num = num * 10 + v[i] - '0';
    }
    num += n ;
  }
  else {
    for (int i = 0; i < len; i++) {
      num = num * 10 + v[i] - '0';
    }
  }
  return num;
}

void dijkstra(int v, int N, graph map, int ** dist, bool * visited) {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      dist[i][j] = map->length[i][j];
    }
  }
  visited[v] = true;
  int min = findMin(v,N, dist,visited);
  while (min != -1) {
    for (int i = 0; i < N; i++) {
      if (!visited[i]&&dist[v][min] + dist[min][i] < dist[v][i]) {
        dist[v][i] = dist[v][min] + dist[min][i];
      }
    }
    visited[min] = true;
    min = findMin(v,N, dist, visited);
    
  }
}

int findMin(int v,int N, int ** dist,bool * visited) {
  int minimum = 65535; int tmp = -1;
  for (int i = 0; i < N; i++) {
    if (!visited[i]&&dist[v][i] < minimum) {
      minimum = dist[v][i];
      tmp = i;
    }
  }
  return tmp;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值