Ultraviolet A Problem 10278 Fire Station)

Source: Internet
Author: User
Tags dot net
// Fire Station // PC/ultraviolet A IDs: 111003/10278, popularity: B, success rate: Low Level: 2 // verdict: accepted // submission date: 2011-10-05 // UV Run Time: 0.888 S // copyright (c) 2011, Qiu. Metaphysis # Yeah dot net // [Problem description] // some fire brigades exist in a city. Residents complain that some houses are far away from the nearest fire station, therefore, a new fire station must be created. You need to select the location of the new fire station to bring it closer to the residential area that lacks services. //// The city can have a maximum of 500 intersections, which are connected by different road lengths. The number of roads at the same intersection cannot exceed 20. Both the House and the fire station are located at the intersection. Suppose there is at least one house at each intersection, and there can be multiple fire stations at the same intersection. //// [Input] // the first line of the input has only one integer, representing the number of test data, followed by a blank line. There is also an empty row between two adjacent groups of data. The first line of each group of // data has two integers: the number of existing fire stations F (F <= 100) and the number of intersections I (I <= 500 ). Intersection use // 1 ~ The continuous integer of I. The number of each line in Row F indicates the number of the intersection where the fire station is located. There are three positive integers in each line: the number of an intersection, the number of another intersection, and the length of the road connecting the two intersections. All roads // are bidirectional (at least the fire truck can pass through both sides), and any intersection is reachable. //// [Output] // output the intersection number of the new fire station for each set of data (minimizing the maximum distance from any intersection to the nearest fire station ). If multiple intersections meet the conditions, the output number is the smallest. The output of two adjacent groups of data is separated by an empty row. /// [Sample input] // 1 // 1 6 // 2 // 1 2 10 // 2 3 10 // 3 4 10 // 4 5 10 // 5 6 10 // 6 1 10 /// [sample output] // 5 // [solution] // how to find the intersection to the nearest fire station distance? Recall the learned graph algorithm, which can be obtained by finding the shortest length between each pair of nodes, namely, the // Floyd algorithm. Using the Floyd algorithm, you can easily calculate the distance matrix from the edge weight matrix of the graph, and then compare the distance from the intersection to the various // fire station to get the distance from the nearest fire station, then compare the distance between each intersection and the nearest fire station to get the maximum distance between the intersection and the fire station. Because the new fire station can only be located at one of all intersections, the intersection where the new fire station is located can be enumerated from the intersection with a large number, and the maximum distance between the intersection and the fire station can be calculated. In this process, the intersection number with the minimum and maximum distance is the location of the new fire station. # Include <iostream> # include <sstream> using namespace STD; # define Maxi 500 # define maxf 100 # define maxint (1 <20) int main (int ac, char * AV []) {string line; int output = 0, cases; int nstations, nintersections; int stations [maxf + 1]; int weight [Maxi + 1] [Maxi + 1]; int shortest [Maxi + 1]; int Fi, Si, wi; istringstream ISS; CIN> cases; while (cases --) {// read the total number of existing fire stations and the total number of intersections. Cin> nstations> nintersections; // read the location of the fire station. For (INT c = 1; C <= nstations; C ++) CIN> stations [c]; // initialize the edge weight matrix. For (INT I = 1; I <= nintersections; I ++) for (Int J = 1; j <= nintersections; j ++) weight [I] [J] = maxint; // read edge weight data. Cin. ignore (); While (Getline (CIN, line), line. length () {ISS. clear (); ISS. STR (line); ISS> fi> SI> wi; weight [fi] [Si] = weight [Si] [fi] = wi ;} // use the Floyd algorithm to calculate the shortest distance between points. For (int K = 1; k <= nintersections; k ++) for (INT I = 1; I <= nintersections; I ++) for (Int J = 1; j <= nintersections; j ++) {int through = weight [I] [k] + weight [k] [J]; if (through <weight [I] [J]) weight [I] [J] = through;} // The distance between an intersection and a fire station is 0. For (INT I = 1; I <= nintersections; I ++) weight [I] [I] = 0; // obtain the distance from each intersection to the nearest fire station and the maximum distance. Int maxshortest = 0; For (INT I = 1; I <= nintersections; I ++) {int distance = maxint; For (Int J = 1; j <= nstations; j ++) if (weight [I] [stations [J] <distance) distance = weight [I] [stations [J]; shortest [I] = distance; if (distance> maxshortest) maxshortest = distance;} // enumerate the location of the new fire station from the intersection with the largest number. Int intersection = 1; for (INT I = nintersections; I> = 1; I --) {// compare the distance from the intersection to the nearest fire station with the distance from the intersection to the new fire station, if the distance between the nearest // and the new fire station is less than the distance between the intersection and the new fire station, the distance between the intersection and the fire station is the distance to the new fire station //; otherwise, the recent distance remains unchanged, at the same time, find the maximum recent distance. Int tmaxshortest = 0; For (Int J = 1; j <= nintersections; j ++) if (weight [J] [I] <shortest [J]) {If (tmaxshortest <weight [J] [I]) tmaxshortest = weight [J] [I];} else {If (tmaxshortest <shortest [J]) tmaxshortest = shortest [J];} // update the maximum recent distance. If (tmaxshortest <= maxshortest) {intersection = I; maxshortest = tmaxshortest ;}// empty rows are output between two adjacent groups of data. If (output ++) cout <Endl; // outputs the location of the new fire station. Cout <intersection <Endl;} return 0 ;}

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.