Acm hdu 3665 seaside

Source: Internet
Author: User
Seaside

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 396 accepted submission (s): 272

Problem descriptionxiaoy is living in a big city, there are n towns in it and some towns near the sea. all these towns are numbered from 0 to N-1 and xiaoy lives in the town numbered '0 '. there are some directed roads connecting them. it is guaranteed that you can reach any town from the town numbered '0', but not all towns connect to each other by roads directly, and there is no ring in this city. one day, xiaoy want to go to the seaside, he asks you to help him find out the shortest way.

Inputthere are several test cases. in each cases the first line contains an integer N (0 <= n <= 10), indicating the number of the towns. then followed n blocks of data, in block-I there are two integers, mi (0 <= mi <= N-1) and PI, then MI lines followed. mi means there are Mi roads beginning with the I-th Town. pi indicates whether the I-th Town is near to the sea, Pi = 0 means no, Pi = 1 means yes. in next mi lines, each line contains two integers SMI and LMIs, which means that the distance between the I-th Town and the SMI town is LMIS.

Outputeach case takes one line, print the shortest length that xiaoy reach seaside.

Sample input5
1 0
1 1
2 0
2 3
3 1
1 1
4 100
0 1
0 1

Sample output2

Source2010 Asia Regional Harbin

Recommendlcy
 # Include  <  Stdio. h  >  
# Include
< Iostream >
Using Namespace STD;
Const Int INF = 65533 ;
Const Int Maxn = 15 ;
Int Distances [maxn] [maxn];
Int A [maxn]; // Whether the record is near the beach
Int N;
Void Floyed ()
{
Int I, J, K;
For (K = 0 ; K < N; k ++ )
For (I = 0 ; I < N; I ++ )
For (J = 0 ; J < N; j ++ )
{
If (Distances [I] [J] > Distances [I] [k] + Distances [k] [J])
Distances [I] [J]
= Distances [I] [k] + Distances [k] [J];
}
}
Int Main ()
{
// Freopen ("test. In", "r", stdin );
// Freopen ("test. Out", "W", stdout );
Int M;
Int CNT;
Int I, J;
Int B;
Int DIS;
While (Scanf ( " % D " , & N) ! = EOF)
{
For (I = 0 ; I < N; I ++ )
For (J = 0 ; J < N; j ++ )
{
If (I = J) distances [I] [J] = 0 ;
Else
Distances [I] [J]
= INF;
}
For (I = 0 ; I < N; I ++ )
{
Scanf (
" % D " , & M, & A [I]);
For (J = 0 ; J < M; j ++ )
{
Scanf (
" % D " , & B, & Dis );
Distances [I] [B]
= DIS;
}
}
Floyed ();
CNT
= INF;
For (I = 0 ; I < N; I ++ )
If (A [I])
If (Distances [ 0 ] [I] < CNT) CNT = Distances [ 0 ] [I];
Printf (
" % D \ n " , CNT );

}
Return 0 ;
}

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.