Bellman_ford ultraviolet 11280-flying to Fredericton

Source: Internet
Author: User

// Take A Look At bellman_ford: in fact, he is constructing a shortest path tree with the source point as the root through constant relaxation, in the original algorithm, after the end of the I-th relaxation, the shortest distance from the root node to the point with the node length not greater than I will be generated)

// Note that the unoptimized bellman does not guarantee that every distance after I is relaxed is the shortest path length of I, because it is out of order when the side is relaxed, the result may be greater than I.

// Use spfa to mark the distance between points. Note that we need to mark two-dimensional arrays ,, obviously, we cannot determine who will make the final result better when a shorter weight and a shorter short circuit are updated to the adjacent contacts.

# Include <iostream> <br/> # include <string> <br/> # include <map> <br/> # include <vector> <br/> # include <memory. h> <br/> # include <cstdio> <br/> # include <cmath> <br/> # include <algorithm> <br/> using namespace STD; <br/> const int max = 1005, INF = 1 <30; <br/> struct state {<br/> int DIS, num; <br/> }; </P> <p> int n, m, U [Max * 10], V [Max * 10], W [Max * 10], <br/> d [Max] [Max], first [Max], next [Max * 10]; </P> <p> Map <string, int> my Map; <br/> bool INQ [Max] [Max]; <br/> state Q [Max * max], temp, out; </P> <p> void bellman_ford (const Int & Len) {<br/> memset (INQ, 0, sizeof (INQ )); <br/> int front = 1, rear = 1; <br/> int U, V, W, minval = inf, <br/> sta = 0, end = n-1, DIS; </P> <p> for (INT I = 0; I <n; I ++) <br/> for (Int J = 0; j <= Len; j ++) {<br/> If (! I) <br/> d [I] [J] = 0; <br/> else <br/> d [I] [J] = inf; <br/>}</P> <p> temp. num = sta; <br/> temp. dis = 0; </P> <p> INQ [temp. num] [temp. dis] = 1; </P> <p> q [rear ++] = temp; </P> <p> while (front <rear) {</P> <p> out = Q [Front ++]; <br/> U = out. num; <br/> Dis = out. DIS; </P> <p> for (int e = first [u]; e! =-1; E = next [e]) {</P> <p> V = V [E]; W = W [E]; </P> <p> If (d [v] [DIS + 1]> d [u] [dis] + W <br/> & DIS + 1 <= Len) {</P> <p> d [v] [DIS + 1] = d [u] [dis] + W; </P> <p> If (! INQ [v] [DIS + 1]) {<br/> INQ [v] [DIS + 1] = 1; <br/> temp. num = V; temp. dis = DIS + 1; <br/> q [rear ++] = temp; <br/>}</P> <p> for (INT I = 0; I <= Len; I ++) {<br/> If (d [end] [I] <minval) <br/> minval = d [end] [I]; <br/>}</P> <p> If (minval! = Inf) <br/> cout <"Total Cost of flight (s) is $" <minval <Endl; <br/> else <br/> cout <"no satisfactory flights" <Endl; <br/>}</P> <p> int main () <br/>{< br/> freopen ("I .txt", "r", stdin); </P> <p> int ncase, T, num = 0, Q; <br/> string S, Su, SV; <br/> CIN> ncase; <br/> while (ncase --) {</P> <p> memset (first,-1, sizeof (first); <br/> If (Num ++) <br/> cout <Endl; <br/> cout <"Scenario #" <num <Endl; <br/> mymap. clear (); </P> <p> CIN> N; </P> <p> for (INT I = 0; I <n; I ++) {<br/> CIN> S; <br/> mymap [s] = I; <br/>}</P> <p> CIN> m; </P> <p> for (INT I = 0; I <m; I ++) {<br/> CIN> su> Sv; <br/> U [I] = mymap [Su]; V [I] = mymap [SV]; <br/> CIN> W [I]; </P> <p> next [I] = first [U [I]; <br/> first [U [I] = I; </P> <p >}</P> <p> CIN> q; <br/> while (Q --) {<br/> CIN> T; <br/> If (T <0) t = 0; <br/> If (T> N) t = n; <br/> bellman_ford (t + 1 ); <br/>}</P> <p >}< br/> return 0; <br/>}</P> <p>

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.