Dijkstra solves the shortest path, but the question stipulates that the first and last characters of a string can be connected at the same time, but the following method is used, and this condition is ignored (which has been implicitly determined );
The meaning of the question is roughly given an integer and a string. When the four characters at the end of the string are the same as the four characters at the beginning of the other string, add the preceding string, and then find the minimum value of the connection status, in addition, each character string that meets the conditions must have the same first and last characters before they can be added;
Convert the question to the shortest circuit, and then process the relationship between the string and the corresponding integer.
[Html]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <algorithm>
Using namespace std;
Constint INF = 1000000000;
Const int maxn = 1005;
Int edge [maxn] [maxn];
Int path [maxn];
Int dist [maxn];
Int used [maxn];
Int n;
Int temp [maxn];
Char str [maxn] [maxn];
Void Dijkstra (int v0)
{
Int I, j, k;
Memset (used, 0, sizeof (used ));
For (I = 0; I <n; I ++)
{
Dist [I] = INF;
}
Dist [0] = 0;
For (I = 0; I <n; I ++)
{
Int MIN = INF, u = v0;
For (j = 0; j <n; j ++)
{
If (! Used [j] & dist [j] <MIN)
{
MIN = dist [j];
U = j;
}
}
Used [u] = 1;
For (k = 0; k <n; k ++)
{
If (edge [u] [k]! =-1 & (edge [u] [k] + dist [u] <dist [k])
{
Dist [k] = dist [u] + edge [u] [k];
Path [k] = u;
}
}
}
}
Int main ()
{
Int I, j, k;
While (scanf ("% d", & n )! = EOF & n)
{
Memset (temp, 0, sizeof (temp ));
Memset (str, 0, sizeof (str ));
For (I = 0; I <n; I ++)
Scanf ("% d % s", & temp [I], str [I]);
Memset (edge,-1, sizeof (edge ));
For (I = 0; I <n; I ++)
{
For (j = 0; j <n; j ++)
{
If (I = j)
Continue;
Int len = strlen (str [I]);
For (k = 0; k <4; k ++)
{
If (str [I] [len-4 + k]! = Str [j] [k])
Break;
}
If (k = 4)
Edge [I] [j] = temp [I];
}
}
Dijkstra (0 );
If (dist [n-1] = INF)
Printf ("-1 \ n ");
Else
Printf ("% d \ n", dist [n-1]);
}
}
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <algorithm>
Using namespace std;
Constint INF = 1000000000;
Const int maxn = 1005;
Int edge [maxn] [maxn];
Int path [maxn];
Int dist [maxn];
Int used [maxn];
Int n;
Int temp [maxn];
Char str [maxn] [maxn];
Void Dijkstra (int v0)
{
Int I, j, k;
Memset (used, 0, sizeof (used ));
For (I = 0; I <n; I ++)
{
Dist [I] = INF;
}
Dist [0] = 0;
For (I = 0; I <n; I ++)
{
Int MIN = INF, u = v0;
For (j = 0; j <n; j ++)
{
If (! Used [j] & dist [j] <MIN)
{
MIN = dist [j];
U = j;
}
}
Used [u] = 1;
For (k = 0; k <n; k ++)
{
If (edge [u] [k]! =-1 & (edge [u] [k] + dist [u] <dist [k])
{
Dist [k] = dist [u] + edge [u] [k];
Path [k] = u;
}
}
}
}
Int main ()
{
Int I, j, k;
While (scanf ("% d", & n )! = EOF & n)
{
Memset (temp, 0, sizeof (temp ));
Memset (str, 0, sizeof (str ));
For (I = 0; I <n; I ++)
Scanf ("% d % s", & temp [I], str [I]);
Memset (edge,-1, sizeof (edge ));
For (I = 0; I <n; I ++)
{
For (j = 0; j <n; j ++)
{
If (I = j)
Continue;
Int len = strlen (str [I]);
For (k = 0; k <4; k ++)
{
If (str [I] [len-4 + k]! = Str [j] [k])
Break;
}
If (k = 4)
Edge [I] [j] = temp [I];
}
}
Dijkstra (0 );
If (dist [n-1] = INF)
Printf ("-1 \ n ");
Else
Printf ("% d \ n", dist [n-1]);
}
}