Description
DescriptionChen and Y recently become fascinated by a game. it is an outstanding simulated business game released by Electronic Arts this year, Spore. in Spore, players will go through the evolution from single-cell creature to the ruler of the galaxy, creating and guarding your own civilization. after several days of game * dozens of minutes/day, Chen finally completed almost clearance. at present, a Chen rule the galaxy numbered 1 to N, and his empire is centered on a beautiful planet of Galaxy 1. trade, Development, alliances, expansion, and resistance to the aggression of Grox [a huge, powerful, and terrible evil empire in the center of the Galaxy], just like any civilization in the Milky Way. A Chen has enough power to spread his territory several hundred light years ago. however, Grox is exceptionally powerful. Their ships appear anywhere Chen knows and often attack any location of Chen and his ally's units [spacecraft, buildings, planet, even Galaxy]. war is inevitable. A Chen will mark N as a galaxy from the center of the Empire, the edge of his territory, to find a possible black hole leading to the center of the Galaxy. he wants to plan a proper route. from Galaxy g1 When arriving at g2, Chen needs to spend c1 [mainly fuel, and also the cost of fighting Grox forces along the way]. c1 is less than 0 because of Galaxy travel, it will bring benefits to Chen [from the logistics price difference and the taxation of some colonies...]. correspondingly, c2 represents the cost of reaching g1 from Galaxy g2. according to Chen, there are M such inter-Galaxy paths. to prepare for war, he needs to select a route with the lowest total cost. idea: The spfa is relatively simple, but there is a negative ring, and the output has no path. Therefore, we need to judge whether to use an array to store the number of entries to join the queue. If the number of entries to join is greater than n, for break, only a loop is exited at the beginning, and the result is tle... (When the array is opened up, add a morning in one night, which consumes no space... Noip bless me)
# Include <iostream> # include <cstdio> # include <cstring> using namespace std; int next [20001] = {0}, point [10001] = {0 }, en [20001] = {0}, va [20001] = {0}, que [2010] = {0}, num [10001] = {0 }; long dis [1001] = {0}; bool visit [10001] = {false}; int main () {int n, m, I, j, tot, head, tail, g1, g2, c1, c2, x, y; long maxn; bool ff; maxn = 2100000000; while (true) {tot = 0; memset (que, 0, sizeof (que); memset (next, 0, sizeof (next); memset (point , 0, sizeof (point); memset (en, 0, sizeof (en); memset (va, 0, sizeof (va); memset (num, 0, sizeof (num); memset (visit, false, sizeof (visit); ff = false; scanf ("% d", & n, & m ); if (n = 0 & m = 0) break; if (m = 0 & n = 1) {printf ("0 \ n "); continue;} if (m = 0) {printf ("No such path \ n"); continue;} for (I = 1; I <= m; ++ I) {scanf ("% d", & g1, & g2, & c1, & c2); ++ tot; next [tot] = point [g1]; point [g1] = tot; en [tot] = g2; va [tot] = c 1; ++ tot; next [tot] = point [g2]; point [g2] = tot; en [tot] = g1; va [tot] = c2 ;} head = 0; tail = 1; que [tail] = 1; for (I = 0; I <= n; ++ I) dis [I] = maxn; dis [1] = 0; visit [1] = true; num [1] ++; do {++ head; head = (head-1) % 2000 + 1; x = que [head]; visit [x] = false; y = point [x]; while (y! = 0) {if (dis [en [y]> dis [x] + va [y]) {dis [en [y] = dis [x] + va [y]; if (! Visit [en [y]) {++ tail; tail = (tail-1) % 2000 + 1; que [tail] = en [y]; + num [en [y]; visit [en [y] = true; if (num [en [y]> n) {ff = true; break ;}} y = next [y];} if (ff) break;} while (head! = Tail); if (ff) printf ("No such path \ n"); else {if (dis [n]! = Maxn) printf ("% lld \ n", dis [n]); else printf ("No such path \ n ");}}}RZUC Code
CODEVS2645 Spore