1072 gas station (30)

Source: Internet
Author: User

The topics are as follows:

Gas station have to being built at such a location that the minimum distance between the station and any of the residential Ho Using is as far away as possible. However it must guarantee that all the houses is in its service range.

Now given the map of the city and several candidate locations for the gas station, you is supposed to give the best recom Mendation. If there is more than one solution, the output of the one with the smallest average distance to all the houses. If Such a solution is still not unique, the output of 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 (<=), 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 be numbered from 1 to N, and all the candidate locations be 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 both ends of a road which can be either house numbers or gas station numbers, and Dist is the Integ Er 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 is separated by a space and is accurate up to 1 decimal place. If The solution does not exist, simply output "No solution".

Sample Input 1:
4 3 2 4 G1 G2 3 G2 4 G3 G1 3g2 G1 1g3 G2 2
Sample Output 1:
G12.0 3.3
Sample Input 2:
2 1 2 101 G1 G1 20
Sample Output 2:
No Solution


The requirements of the topic is more cumbersome, we must carefully read the question, the following test instructions to summarize.

There are n residential areas, m gas stations, now require the identification of a gas station, to meet the following requirements:

The distance from ① station to each residential area is ≤d

The shortest route to each residential area of the ② station is the smallest in all stations that meet the ①.

③ if more than one solution is present in ②, find the minimum distance from the gas station to all residential areas.

To sum up, the key to this problem is to find out each gas station to all residential areas of the shortest distance, that is, single-source shortest path problem, using the Dijkstra algorithm for each gas station are calculated, and then stored separately, finally in accordance with the above requirements screening can be.

The code is as follows:

#include <iostream> #include <vector> #include <stdio.h> #include <string> #include <sstream    > #include <stdlib.h> #include <memory.h>using namespace std; #define INF 99999999struct arc{Double dist;    int num;        ARC (int n, double d) {num = n;    dist = D; }};int n,m,k,d;bool* visited;vector<vector<double> > Mindists;vector<vector<arc> > Graph;int    Parsevertex (String ver) {StringStream ss;    SS << ver;    int num;    SS >> Num;    if (num! = 0) return num;    Ss.clear ();    char c;    SS >> C;    SS >> Num; return num + N;}    void Dijkstra (int source) {memset (visited,false,sizeof (BOOL) * (1 + N + M));    Mindists[source].resize (1 + N + M);    for (int i = 1; i < mindists[source].size (); i++) {mindists[source][i] = INF;    } Mindists[source][source] = 0;    Visited[source] = true;      for (int i = 0; i < graph[source].size (); i++) {Arc arc = graph[source][i];  int num = Arc.num;        int dist = arc.dist;    Mindists[source][num] = dist;    } double mindist = INF;    int V;        for (int k = 0; k < N + M; k++) {mindist = INF; for (int i = 1; i < mindists[source].size (); i++) {if (!visited[i] && Mindists[source][i] < mindist                ) {mindist = Mindists[source][i];            v = i;        }} if (mindist = = INF) break;        VISITED[V] = true;            for (int i = 0; i < graph[v].size (); i++) {Arc arc = graph[v][i];            int w = arc.num;            Double dist = arc.dist;            if (visited[w]) continue;            if (Mindists[source][w] > Mindists[source][v] + dist) {mindists[source][w] = Mindists[source][v] + dist;    }}}}int Main () {cin >> N >> M >> K >> D;    Graph.resize (1 + N + M);    visited = (bool*) malloc (1 + N + M);    Mindists.resize (1 + N + M);    String source,dest;Double Dist;        for (int i = 0; i < K; i++) {cin >> source >> dest >> Dist;        int sourceint = Parsevertex (source);        int destint = Parsevertex (dest);        printf ("<%d%d%d>\n", sourceint,destint,dist);        Graph[sourceint].push_back (ARC (destint,dist));    Graph[destint].push_back (ARC (sourceint,dist));        } for (int i = 1; I <= M; i++) {int gasnum = N + i;    Dijkstra (Gasnum);    } int gasnum =-1;    Double gassum;    Double maxmindist =-1;    Double mindist = INF;    Double average;        for (int i = N + 1; i < mindists.size (); i++) {double sum = 0;        Mindist = INF;            for (int j = 1; J <= N; j + +) {Double dist = mindists[i][j];                if (Dist > D) {sum =-1;            Break            } sum + = dist;        if (Mindist > Dist) mindist = dist;        } if (sum = =-1) continue; if (Maxmindist < mindist) {gasnum = i- N            Maxmindist = mindist;            Gassum = sum;        Average = sum/n;                }else if (maxmindist = = mindist) {if (sum < gassum) {gasnum = I-n;                Gassum = sum;            Average = sum/n;    }}} if (Gasnum! =-1) {printf ("g%d\n%0.1f%0.1f\n", gasnum,maxmindist,average);    }else{printf ("No solution\n"); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

1072 gas station (30)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.