hdu 1596 乘积的最大值

一般题是 和的最小值 这个是乘积的最大值

Sample Input
3
1 0.5 0.5
0.5 1 0.4
0.5 0.4 1
3
1 2 //起点 终点
2 3
1 3

Sample Output
0.500
0.400
0.500

Dijkstra:

 1 # include <iostream>
 2 # include <cstdio>
 3 # include <cstring>
 4 # include <algorithm>
 5 # include <cmath>
 6 # define LL long long
 7 using namespace std ;
 8 
 9 const int MAXN=1010;
10 const int INF=0x3f3f3f3f;
11 int n ;
12 bool vis[MAXN];
13 double cost[MAXN][MAXN] ;
14 double lowcost[MAXN] ;
15 void Dijkstra(int beg)
16 {
17     for(int i=0;i<n;i++)
18     {
19         lowcost[i]=cost[beg][i];vis[i]=false;;
20     }
21     for(int j=0;j<n;j++)
22     {
23         int k=-1;
24         double Max= -INF;
25         for(int i=0;i<n;i++)
26             if(!vis[i]&&lowcost[i] > Max)
27             {
28                 Max=lowcost[i];
29                 k=i;
30             }
31             if(k==-1)
32                 break ;
33             vis[k]=true;
34             for(int i=0;i<n;i++)
35                 lowcost[i]=max(lowcost[i],lowcost[k]*cost[k][i]) ;
36 
37     }
38 
39 }
40 
41 
42 
43 int main ()
44 {
45   //  freopen("in.txt","r",stdin) ;
46 
47     while (scanf("%d" , &n ) !=EOF)
48     {
49         int i , j ;
50         for (i = 0 ; i < n ; i++)
51             for (j = 0 ; j < n ; j++)
52                scanf("%lf" , &cost[i][j]) ;
53         int m ;
54         scanf("%d" , &m) ;
55         while(m--)
56         {
57             int s , e;
58             scanf("%d %d" , &s , &e) ;
59             Dijkstra(s-1) ;
60             if (lowcost[e-1] > 1 || lowcost[e-1] <= 0)
61                 printf("What a pity!
") ;
62             else
63                 printf("%.3lf
" , lowcost[e-1]) ;
64         }
65     }
66 
67     return 0 ;
68 }
View Code
原文地址:https://www.cnblogs.com/mengchunchen/p/4587263.html