The main topic: give n points, M line, Q Inquiry, ask from the beginning to the end point, up to k points of the shortest line
Problem-Solving ideas: SPFA run directly, and then record the shortest array plus a dimension, the record after a few points on it, or a very water
#include <cstdio>#include <cstring>#include <map>#include <iostream>#include <queue>using namespace STD;#define N#define M 1010#define INF 0x3f3f3f3fstructedge{intU, V, next, C;} E[M];structNode {intu, time; Node () {} node (intUintTime): U (U), Time (time) {}}n1, n2; Map<string, int>CityintN, m, tot, q;intHead[n], d[n][n];BOOLVis[n][n];voidAddedge (intUintVintc) {e[tot].v = v; E[TOT].C = C; E[tot].next = Head[u]; Head[u] = tot++;}voidInit () {city.clear ();scanf("%d", &n);stringA, B; for(inti =1; I <= N; i++) {Cin>> A; City[a] = i; }memset(Head,-1,sizeof(head)); tot =0;scanf("%d", &m);intC for(inti =0; I < m; i++) {Cin>> a >> b >> c; Addedge (City[a], city[b], c); }}intS, t;voidSPFA () {memset(d,0x3f,sizeof(d));memset(Vis,0,sizeof(VIS)); queue<Node>Q; Q.push (Node (s),0)); d[s][0] =0; while(! Q.empty ()) {n1 = Q.front (); Q.pop ();intU = n1.u, time = N1.time; for(inti = head[n1.u]; ~i; i = e[i].next) {intv = e[i].v;if(D[u][time] + e[i].c < D[v][time +1]) {d[v][time +1] = D[u][time] + e[i].c;if(!vis[v][time +1]) {vis[v][time +1] =true; Q.push (Node (V, Time +1)); } } } }}voidSolve () {s = city["Calgary"]; t = city["Fredericton"]; SPFA ();scanf("%d", &q);intMax; while(q--) {scanf("%d", &max); max = min (max, n);intans = INF; for(inti =0; I <= Max +1; i++) ans = min (ans, d[t][i]);if(ans = = INF)printf("No satisfactory flights\n");Else printf("Total cost of flight (s) is $%d\n", ans); }}intMain () {intTest, CAS =1;BOOLFlag =false;scanf("%d", &test); while(test--) {if(flag)printf("\ n"); Flag =true;printf("Scenario #%d\n", cas++); Init (); Solve (); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVA-11280 Flying to Fredericton (SPFA)