Use a constant for infinite values
This commit is contained in:
parent
77488f2f3e
commit
baee83266a
|
@ -142,9 +142,9 @@ we can directly implement a solution in C++:
|
||||||
|
|
||||||
\begin{lstlisting}
|
\begin{lstlisting}
|
||||||
int f(int x) {
|
int f(int x) {
|
||||||
if (x < 0) return 1e9;
|
if (x < 0) return INF;
|
||||||
if (x == 0) return 0;
|
if (x == 0) return 0;
|
||||||
int u = 1e9;
|
int u = INF;
|
||||||
for (int i = 1; i <= k; i++) {
|
for (int i = 1; i <= k; i++) {
|
||||||
u = min(u, f(x-c[i])+1);
|
u = min(u, f(x-c[i])+1);
|
||||||
}
|
}
|
||||||
|
@ -154,7 +154,7 @@ int f(int x) {
|
||||||
|
|
||||||
The code assumes that the available coins are
|
The code assumes that the available coins are
|
||||||
$\texttt{c}[1], \texttt{c}[2], \ldots, \texttt{c}[k]$,
|
$\texttt{c}[1], \texttt{c}[2], \ldots, \texttt{c}[k]$,
|
||||||
and $10^9$ denotes infinity.
|
and the constant \texttt{INF} denotes infinity.
|
||||||
This function works but it is not efficient yet,
|
This function works but it is not efficient yet,
|
||||||
because it goes through a large number
|
because it goes through a large number
|
||||||
of ways to construct the sum.
|
of ways to construct the sum.
|
||||||
|
@ -191,10 +191,10 @@ implemented as follows:
|
||||||
|
|
||||||
\begin{lstlisting}
|
\begin{lstlisting}
|
||||||
int f(int x) {
|
int f(int x) {
|
||||||
if (x < 0) return 1e9;
|
if (x < 0) return INF;
|
||||||
if (x == 0) return 0;
|
if (x == 0) return 0;
|
||||||
if (d[x]) return d[x];
|
if (d[x]) return d[x];
|
||||||
int u = 1e9;
|
int u = INF;
|
||||||
for (int i = 1; i <= k; i++) {
|
for (int i = 1; i <= k; i++) {
|
||||||
u = min(u, f(x-c[i])+1);
|
u = min(u, f(x-c[i])+1);
|
||||||
}
|
}
|
||||||
|
@ -233,7 +233,7 @@ instead of a recursive function:
|
||||||
\begin{lstlisting}
|
\begin{lstlisting}
|
||||||
d[0] = 0;
|
d[0] = 0;
|
||||||
for (int i = 1; i <= x; i++) {
|
for (int i = 1; i <= x; i++) {
|
||||||
int u = 1e9;
|
int u = INF;
|
||||||
for (int j = 1; j <= k; j++) {
|
for (int j = 1; j <= k; j++) {
|
||||||
if (i-c[j] < 0) continue;
|
if (i-c[j] < 0) continue;
|
||||||
u = min(u, d[i-c[j]]+1);
|
u = min(u, d[i-c[j]]+1);
|
||||||
|
@ -269,7 +269,7 @@ is used for this:
|
||||||
\begin{lstlisting}
|
\begin{lstlisting}
|
||||||
d[0] = 0;
|
d[0] = 0;
|
||||||
for (int i = 1; i <= x; i++) {
|
for (int i = 1; i <= x; i++) {
|
||||||
d[i] = 1e9;
|
d[i] = INF;
|
||||||
for (int j = 1; j <= k; j++) {
|
for (int j = 1; j <= k; j++) {
|
||||||
if (i-c[j] < 0) continue;
|
if (i-c[j] < 0) continue;
|
||||||
int u = d[i-c[j]]+1;
|
int u = d[i-c[j]]+1;
|
||||||
|
|
|
@ -207,10 +207,10 @@ reduce the distances.
|
||||||
The algorithm constructs an array \texttt{distance}
|
The algorithm constructs an array \texttt{distance}
|
||||||
that will contain the distance from $x$
|
that will contain the distance from $x$
|
||||||
to all nodes in the graph.
|
to all nodes in the graph.
|
||||||
The initial value $10^9$ means infinity.
|
The constant \texttt{INF} denotes an infinite distance.
|
||||||
|
|
||||||
\begin{lstlisting}
|
\begin{lstlisting}
|
||||||
for (int i = 1; i <= n; i++) distance[i] = 1e9;
|
for (int i = 1; i <= n; i++) distance[i] = INF;
|
||||||
distance[x] = 0;
|
distance[x] = 0;
|
||||||
for (int i = 1; i <= n-1; i++) {
|
for (int i = 1; i <= n-1; i++) {
|
||||||
for (int a = 1; a <= n; a++) {
|
for (int a = 1; a <= n; a++) {
|
||||||
|
@ -307,7 +307,7 @@ in which case the algorithm does not add
|
||||||
the node to the queue again.
|
the node to the queue again.
|
||||||
|
|
||||||
\begin{lstlisting}
|
\begin{lstlisting}
|
||||||
for (int i = 1; i <= n; i++) distance[i] = 1e9;
|
for (int i = 1; i <= n; i++) distance[i] = INF;
|
||||||
distance[x] = 0;
|
distance[x] = 0;
|
||||||
q.push(x);
|
q.push(x);
|
||||||
while (!q.empty()) {
|
while (!q.empty()) {
|
||||||
|
@ -571,10 +571,10 @@ The code keeps track of processed nodes
|
||||||
in an array \texttt{ready},
|
in an array \texttt{ready},
|
||||||
and maintains the distances in an array \texttt{distance}.
|
and maintains the distances in an array \texttt{distance}.
|
||||||
Initially, the distance to the starting node is 0,
|
Initially, the distance to the starting node is 0,
|
||||||
and the distance to all other nodes is $10^9$ (infinite).
|
and the distance to all other nodes is infinite.
|
||||||
|
|
||||||
\begin{lstlisting}
|
\begin{lstlisting}
|
||||||
for (int i = 1; i <= n; i++) distance[i] = 1e9;
|
for (int i = 1; i <= n; i++) distance[i] = INF;
|
||||||
distance[x] = 0;
|
distance[x] = 0;
|
||||||
q.push({0,x});
|
q.push({0,x});
|
||||||
while (!q.empty()) {
|
while (!q.empty()) {
|
||||||
|
@ -764,15 +764,14 @@ The following code constructs a
|
||||||
distance matrix \texttt{d} where $\texttt{d}[a][b]$
|
distance matrix \texttt{d} where $\texttt{d}[a][b]$
|
||||||
is the shortest distance between nodes $a$ and $b$.
|
is the shortest distance between nodes $a$ and $b$.
|
||||||
First, the algorithm initializes \texttt{d}
|
First, the algorithm initializes \texttt{d}
|
||||||
using the adjacency matrix \texttt{v} of the graph
|
using the adjacency matrix \texttt{v} of the graph:
|
||||||
($10^9$ means infinity):
|
|
||||||
|
|
||||||
\begin{lstlisting}
|
\begin{lstlisting}
|
||||||
for (int i = 1; i <= n; i++) {
|
for (int i = 1; i <= n; i++) {
|
||||||
for (int j = 1; j <= n; j++) {
|
for (int j = 1; j <= n; j++) {
|
||||||
if (i == j) d[i][j] = 0;
|
if (i == j) d[i][j] = 0;
|
||||||
else if (v[i][j]) d[i][j] = v[i][j];
|
else if (v[i][j]) d[i][j] = v[i][j];
|
||||||
else d[i][j] = 1e9;
|
else d[i][j] = INF;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
\end{lstlisting}
|
\end{lstlisting}
|
||||||
|
|
Loading…
Reference in New Issue