Poj1122-FDNY to the rescue!

Source: Internet
Author: User


Want to see more problem-solving reports: http://blog.csdn.net/wangjian8006/article/details/7870410
Reprinted please indicate the source: http://blog.csdn.net/wangjian8006

On a map, you can find the points that are taken in the shortest time and shortest time from the fire station to the fire point.
Only one group of data
The first line represents a map with N points
Next is a matrix of n × N, which represents the time from column J of row I to J.-1 indicates that it cannot be reached.
In the last line, the first number indicates the fire point, and the other number indicates the fire station.
The output is the time path of the fire station.

Solution: A deformation of the shortest path, all sides in turn from the fire point to find the shortest path from all points
The shortest path of the fire point. When calculating the shortest path, record the path point so that the output can be from the time.

/* Memory 176 ktime 0 Ms */# include <iostream> using namespace STD; # define maxv 20 # define INF int_maxint map [maxv] [maxv], d [maxv], parent [maxv]; int N, fire, stasum, station [maxv]; void Dijkstra () {int I, j, V, TMP; bool vis [maxv]; memset (parent,-1, sizeof (parent); memset (VIS, false, sizeof (VIS); for (I = 1; I <= N; I ++) d [I] = inf; d [Fire] = 0; for (I = 1; I <= N; I ++) {TMP = inf; For (j = 1; j <= N; j ++) if (d [J] <TMP &&! Vis [J]) {TMP = d [J]; V = J;} vis [v] = true; For (j = 1; j <= N; j ++) {If (! Vis [J] & map [v] [J]! =-1 & D [J]> d [v] + map [v] [J]) {//-1 cannot go through D [J] = d [v] + map [v] [J]; parent [J] = V; // record path }}} void output () {Int J, I, TMP, V; printf ("orgdesttimepath \ n"); j = 0; while (J! = Stasum) {TMP = inf; for (I = 1; I <= N; I ++) {// output if (station [I] & D [I] <TMP) {TMP = d [I]; V = I ;}} station [v] = 0; // set this site to a non-fire station printf ("% d", V, fire, d [v]); While (V! =-1) {printf ("% d", V); V = parent [v];} printf ("\ n"); j ++ ;}} int main () {int I, j, X; scanf ("% d", & N); for (I = 1; I <= N; I ++) {for (j = 1; j <= N; j ++) {scanf ("% d", & map [J] [I]); // edge reverse storage} scanf ("% d", & fire); stasum = 0; memset (station, 0, sizeof (Station); While (~ Scanf ("% d", & X) {station [x] = 1; // mark the stasum ++ of the fire station; // calculate the total number of fire stations} Dijkstra (); output (); 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.