Today, the first time the system learned the shortest path algorithm, began to brush the 11th chapter, the first write Dijkstra algorithm, there have been many loved errors. and UVA on the sample is very water, ladybug is also very water, pit me for a long time.
First of all, for the processing of nodes, we must maintain a two-tuple, a node that represents the shortest path to the current node. Because the Dijkstra algorithm uses the priority queue to speed up the algorithm, it is necessary to define a less-than operator, and it is obviously not right to initially load the state into the priority queue, because the priority queue is to take out the node with the shortest current distance.
Second, talk about the ingenious idea of the shortest-path algorithm: Each time you select a point with the smallest distance from all the currently unmarked nodes, update all the nodes connected to it from this point. Repeat this process.
Why is this the right thing to do? Baidu Encyclopedia on another dynamic map can help us to understand the process well. Portal: Click to open link
The problem is a very clever short-circuit question, we need to abstract the model, to see clearly what the essence of the problem to solve.
The state set is treated as a node, the time spent as the weight of the edge, and whether two "nodes" are connected depends on the relationship of the first string to the node! That's a good question ~
Another point, it is important to use dynamic planning for state transfer is not to transfer to the previous state, that is, the state diagram is not a DAG, so you can not use the memory of the search.
See the code for details:
#include <bits/stdc++.h>using namespace Std;const int INF = 0x3f3f3f3f;const int MAXN = 22;const int MAXM = 105;int n , m,kase = 0,ok = 0, d[1<<maxn],done[1<<maxn];struct node{int t; Char A[MAXN],B[MAXN];} pat[maxm];struct node{int bugs,dist; BOOL operator < (const node& v) const {return dist > v.dist; }};int Dijkstra () {priority_queue<node> q; for (int i=0;i< (1<<n); i++) {done[i] = 0; D[i] = INF;} Node u; u.dist = 0; U.bugs = (1<<n)-1; D[u.bugs] = 0; Q.push (U); while (!q.empty ()) {Node u = q.top (); Q.pop (); if (u.bugs = = 0) return u.dist; if (done[u.bugs]) continue; Done[u.bugs] = true; for (int i=1;i<=m;i++) {bool OK = true; for (int j=0;j<n;j++) {//Check if the node can have one edge if (pat[i].a[j] = = '-' && u.bugs & (1<<j)) {OK = false; Break } if (pat[i].a[j] = = ' + ' &&! ( U.bugs & (1<<J)){ok = false; break;} } if (ok) {Node v = u; for (int j=0;j<n;j++) {//update found next node if (pat[i].b[j] = = '-') {V.bugs &= ~ ( 1<<J); } else if (pat[i].b[j] = = ' + ') {v.bugs |= (1<<j); }} if (D[v.bugs] > u.dist + pat[i].t) {D[v.bugs] = u.dist + pat[i].t; V.dist = D[v.bugs]; Q.push (v); }}}} return-1;} int main () {while (~scanf ("%d%d", &n,&m)) {if (!n &&!m) return 0; for (int i=1;i<=m;i++) {scanf ("%d%s%s", &pat[i].t,pat[i].a,pat[i].b); } int ans = Dijkstra (); printf ("Product%d\n", ++kase); if (ans < 0) printf ("Bugs cannot be fixed.\n\n"); else printf ("Fastest sequence takes%d seconds.\N\n ", ans); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
658-it ' s not a Bug, It's a feature! (Dijkstra algorithm)