Test instructions Give you a map, and then ask from the beginning to the end of the shortest, but there is a limit, is the total cost can not exceed k, that is, each edge has two weights, one is the length, one is spent, to meet the shortest length of expenditure.
Ideas: First wrote a mark[i][j] The first point to spend the J-state of the spfa,tle, and then optimize the next, is to reverse the search through the simple shortest way (with the cost of the weight) and then use this result in the mark[][] two-dimensional short-cut inside the pruning, the result is still timed out, Then try the next priority queue, the result 900+ac, I put my first optimization removed, the result ran 800+, hey. For the use of priority queue on the SPFA, this I feel is not very reliable Ah, if not consider the priority queue time complexity of running SPFA is really an optimization, because after all a bit greedy meaning (how much to optimize, to see the data, in short, not like the memory of the level of optimization of the search), But the priority queue operation time is the log level, between the two of them to measure, or to see the specific data ah.
This is a reverse-lookup preprocessing optimization of the AC code, the reverse preprocessing is removed after a bit faster (eh. )
int List[n_node], tot; int List2[n_node], tot2; int mark[n_node][10000+5]; int s_x[n_node][10000+5]; int S_x2[n_node]; STAR E[n_edge]; STAR2 E2[n_edge]; NODE Xin, tou;
void Add (int a, int b, int c, int d) { E[++tot].to = b; E[tot].cost = C; E[tot].time = D; E[tot].next = List[a]; List[a] = tot; }
void Add2 (int a, int b, int c) { E2[++tot2].to = b; E2[tot2].cost = C; E2[tot2].next = List2[a]; List2[a] = Tot2; }
void SPFA (int s, int n, int maxtime) { for (int i = 0; I <= n; i + +) for (int j = 0; J <= MaxTime; j + +) S_X[I][J] = INF, mark[i][j] = 0; priority_queue<node>q; Xin.id = 1, xin.cost = Xin.time = 0; Q.push (Xin); S_x[xin.id][xin.time] = 0; Mark[xin.id][xin.time] = 1; while (!q.empty ()) { tou = Q.top (); Q.pop (); Mark[tou.id][tou.time] = 0; for (int k = list[tou.id]; k; k = e[k].next) { Xin.id = e[k].to; Xin.cost = Tou.cost + e[k].cost; Xin.time = Tou.time + e[k].time; if (Xin.time + s_x2[xin.id]> maxtime) continue; if (S_x[xin.id][xin.time] > S_x[tou.id][tou.time] + e[k].cost) { S_x[xin.id][xin.time] = S_x[tou.id][tou.time] + e[k].cost; if (!mark[xin.id][xin.time]) { Mark[xin.id][xin.time] = 1; Q.push (Xin); } }
} } }
void Spfa2 (int s, int n) { int Mk[n_node] = {0}; for (int i = 0; I <= n; i + +) S_x2[i] = INF; queue<int>q; Q.push (s); Mk[s] = 1; S_x2[s] = 0; while (!q.empty ()) { int Xin, tou; tou = Q.front (); Q.pop (); Mk[tou] = 0; for (int k = List2[tou]; k; k = e2[k].next) { Xin = e2[k].to; if (S_x2[xin] > S_x2[tou] + e2[k].cost) { S_x2[xin] = S_x2[tou] + e2[k].cost; if (!mk[xin]) { Mk[xin] = 1; Q.push (Xin); } } } } }
int main () { int n, m, MaxTime, I; int A, B, C, D; while (~SCANF ("%d", &maxtime)) { scanf ("%d%d", &n, &m); memset (list, 0, sizeof (list)); memset (list2, 0, sizeof (LIST2)); tot = 1, tot2 = 1; for (i = 1; I <= m; i + +) { scanf ("%d%d%d", &a, &b, &c, &d); Add (A, B, C, D); ADD2 (b, A, d); } SPFA2 (n, N); SPFA (1, N, maxtime); int ans = INF; for (i = 1; I <= maxtime; i + +) if (ans > s_x[n][i]) ans = s_x[n][i]; if (ans = = INF) ans =-1; printf ("%d\n", ans); } return 0; }
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.