[TOJ 3692] Emergency Rescue, toj1_2 rescue
# Include <iostream> # include <algorithm> # include <queue> using namespace std; # define INF 1e8int n, m, x = 0; double g [1005] [1005]; // Save the required time void f () {int I, a, vis [1005], num, s, minn = INF; double d [1005]; // storage time priority_queue <int, vector <int>, greater <int> v; // use the priority queue, from small to large x ++; for (I = 1; I <= n; I ++) // set the time from the initialization Start point 0 to the remaining intersections to the maximum value g [0] [I] = INF; scanf ("% d", & num, & s); // num intersection No. s number of police cars for (I = 0; I <= n; I ++) // traverse each intersection {vis [I] = 0; // each intersection has Status marked as inaccessible d [I] = INF; // the start time is the maximum value} // initialize for (I = 1; I <= s; I ++) // enter the location of each police car {scanf ("% d", & a); g [0] [a] = 0;} d [0] = 0; v. push (0); while (! V. empty () {a = v. top (); v. pop (); if (vis [a] = 1) continue; vis [a] = 0; for (I = 0; I <= n; I ++) {if (d [I]> d [a] + g [a] [I]) {d [I] = d [a] + g [a] [I]; v. push (I) ;}}cout <"Scenario" <x <":" <endl; if (minn = d [num]) cout <"Impossible. "<endl; else printf (" %. 2lf \ n ", d [num]); cout <" \ n ";}int main () {int I, j, k, s, e; double t; scanf ("% d", & n, & m, & k); // Number of n intersections m number of roads k number of incidents for (I = 0; I <= n; I ++) {for (j = 0; j <= n; j ++) g [I] [j] = INF; // save the time from intersection I to intersection j. g [I] [I] = 0; // set the time to 0} for (I = 0; I <m; I ++) {scanf ("% d % lf", & s, & e, & t ); // s start point e end point t time if (t <g [s] [e]) g [s] [e] = t;} for (I = 1; I <= k; I ++) f ();}