Use a constant for infinite values

This commit is contained in:
Antti H S Laaksonen 2017-04-17 14:38:33 +03:00
parent 77488f2f3e
commit baee83266a
2 changed files with 14 additions and 15 deletions

View File

@ -142,9 +142,9 @@ we can directly implement a solution in C++:
\begin{lstlisting}
int f(int x) {
if (x < 0) return 1e9;
if (x < 0) return INF;
if (x == 0) return 0;
int u = 1e9;
int u = INF;
for (int i = 1; i <= k; i++) {
u = min(u, f(x-c[i])+1);
}
@ -154,7 +154,7 @@ int f(int x) {
The code assumes that the available coins are
$\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,
because it goes through a large number
of ways to construct the sum.
@ -191,10 +191,10 @@ implemented as follows:
\begin{lstlisting}
int f(int x) {
if (x < 0) return 1e9;
if (x < 0) return INF;
if (x == 0) return 0;
if (d[x]) return d[x];
int u = 1e9;
int u = INF;
for (int i = 1; i <= k; i++) {
u = min(u, f(x-c[i])+1);
}
@ -233,7 +233,7 @@ instead of a recursive function:
\begin{lstlisting}
d[0] = 0;
for (int i = 1; i <= x; i++) {
int u = 1e9;
int u = INF;
for (int j = 1; j <= k; j++) {
if (i-c[j] < 0) continue;
u = min(u, d[i-c[j]]+1);
@ -269,7 +269,7 @@ is used for this:
\begin{lstlisting}
d[0] = 0;
for (int i = 1; i <= x; i++) {
d[i] = 1e9;
d[i] = INF;
for (int j = 1; j <= k; j++) {
if (i-c[j] < 0) continue;
int u = d[i-c[j]]+1;

View File

@ -207,10 +207,10 @@ reduce the distances.
The algorithm constructs an array \texttt{distance}
that will contain the distance from $x$
to all nodes in the graph.
The initial value $10^9$ means infinity.
The constant \texttt{INF} denotes an infinite distance.
\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;
for (int i = 1; i <= n-1; i++) {
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.
\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;
q.push(x);
while (!q.empty()) {
@ -571,10 +571,10 @@ The code keeps track of processed nodes
in an array \texttt{ready},
and maintains the distances in an array \texttt{distance}.
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}
for (int i = 1; i <= n; i++) distance[i] = 1e9;
for (int i = 1; i <= n; i++) distance[i] = INF;
distance[x] = 0;
q.push({0,x});
while (!q.empty()) {
@ -764,15 +764,14 @@ The following code constructs a
distance matrix \texttt{d} where $\texttt{d}[a][b]$
is the shortest distance between nodes $a$ and $b$.
First, the algorithm initializes \texttt{d}
using the adjacency matrix \texttt{v} of the graph
($10^9$ means infinity):
using the adjacency matrix \texttt{v} of the graph:
\begin{lstlisting}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == j) d[i][j] = 0;
else if (v[i][j]) d[i][j] = v[i][j];
else d[i][j] = 1e9;
else d[i][j] = INF;
}
}
\end{lstlisting}