Zoj 2797 best way to escape Chicago

Source: Internet
Author: User

//////////////////////////////////////// ////////////////////
// The safest way to escape from Chicago
// Use the single-Source Shortest Path Method
// The Prev [] in this question is not used. This is used to construct the shortest path.
# Include <iostream>
# Include <iomanip>
Using namespace STD;
# Define min 0.0
Double C [105] [105];
Int Prev [105];
Double Dist [105];

 

 

Void build (INT m)
{
Int I, A, B, cost;
For (I = 1; I <= m; I ++)
{
Cin> A> B> cost;
C [a] [B] = C [B] [a] = double (cost)/100;
}
}

 

Void Dijk (int n, int V)
{
Int I, J, K;
Double tempmax;
For (I = 1; I <= N; I ++) // Initialization
{
Dist [I] = C [v] [I];
If (Dist [I] = min) Prev [I] = 0;
Else Prev [I] = V;
}
C [v] [v] = 1.0;
Dist [vs] = 1.0;
For (I = 1; I <n; I ++)
{
Tempmax = 0.0;
For (k = 1; k <= N; k ++)
If (C [k] [k]! = 1.0 & tempmax <Dist [k]) // find the maximum probability
{
Tempmax = DIST [k];
J = K;
}
C [J] [J] = 1.0;
For (k = 1; k <= N; k ++)
{
If (C [k] [k]! = 1.0 & C [J] [k]! = Min) // modify the DIST Value
If (Dist [k] <Dist [J] * C [J] [k])
{
Dist [k] = DIST [J] * C [J] [k];
Prev [k] = J;
}
}
}
}

Int main ()
{
Int I, j, n, m;
While (CIN> N & n! = 0)
{
Cin> m;
For (I = 0; I <= N; I ++)
For (j = 0; j <= N; j ++)
C [I] [J] = min;
For (I = 0; I <= N; I ++)
C [I] [I] = 0;
Build (m );
Dijk (n, 1 );

Cout <setiosflags (IOs: fixed) <setprecision (6) <Dist [N] * 100 <"percent" <Endl;
}
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.