Hdu1596find the safest road (Floyd)

Source: Internet
Author: User
Find the safest road

Time Limit: 10000/5000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 16965 accepted submission (s): 5876


Problem descriptionxx has many cities with one or more flight routes between each city, but not all roads are safe. Each road has a safety factor S, S is a real number between 0 and 1 (including 0, 1). The security of channel P from u to V is safe (p) = S (E1) * s (E2 )... * S (EK) E1, E2, and EK are on the P side. Now 8600 wants to go out for a tour. In the face of so many roads, he wants to find the safest path. But 8600 is not good at mathematics. Could you please help? ^_^

 

Input includes multiple test instances, each of which includes:
The first line is N. N indicates the number of cities. n <= 1000;
Next, an N * n matrix represents the security coefficient between two cities. (0 can be understood as there is no direct channel between the two cities)
Next is the Q 8600 route to travel. Each line has two numbers, indicating the city where 8600 is located and the city to be visited.

 

Output if 86 cannot reach his destination, output "What a pity! ",
Others output the safety factor of the safest road between the two cities, with three decimal places retained.

 

Sample input31 0.5 0.50.5 1 0.40.5 0.4 131 22 31 3

 

Sample output0.5000.4000.500

Question: Give a matrix of the security coefficients between the two cities, and then give the number-to-city relationship. The maximum security coefficient must be output.

Question: getting started with Floyd.

 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int INF=0x3f3f3f3f; 4 double a[1005][1005]; 5 int n; 6 void floyd() { 7     for(int k=1; k<=n; k++) { 8         for(int i=1; i<=n; i++) { 9             for(int j=1; j<=n; j++) {10                 a[i][j]=max(a[i][j],a[i][k]*a[k][j]);11             }12         }13     }14 }15 int main() {16     17     while(~scanf("%d",&n))18     {19         memset(a,0,sizeof(a));20         for(int i=1;i<=n;i++)21         {22             for(int j=1;j<=n;j++)23             {24                 scanf("%lf",&a[i][j]);25             }26         }27         floyd();28         int q;29         scanf("%d",&q);30         while(q--)31         {32             int x,y;33             scanf("%d %d",&x,&y);34             if(a[x][y]==0)printf("What a pity!\n");35             else 36             printf("%.3lf\n",a[x][y]);37         }38     }39     return 0;40 }

 

Hdu1596find the safest road (Floyd)

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.