Hdu1546-Idiomatic Phrases (Gamehttp)

Source: Internet
Author: User

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]);
}
}

 

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.