[CTSC2011] Path of Happiness

Topic description

A directed graph G has n vertices 1, 2, …, n, and the weight of point i is w(i). Now there is an ant, starting from a given starting point v0, and crawling along the edge of the graph G. At the beginning, it has a stamina of 1. Each time it climbs over an edge, its physical strength decreases by a factor of ρ, where ρ is a given constant less than 1. The happiness of an ant when it climbs to a certain vertex is the product of its physical strength at that time and the weight of that point.

We denote the sum of the happiness of the ants on the crawling path as H. Obviously, for different crawl paths, the value of H may also be different. Little Z is interested in the maximum possible value of H, can you help him calculate it? Note that the length of the path ants crawl may be infinite.

Input and output format

Input format:

 

Separate the two numbers on each line with a space.

The first line of the input file contains two positive integers n, m, which represent the number of vertices and the number of edges in G, respectively.

The second line contains n non-negative real numbers, which in turn represent the n vertex weights w(1), w(2), …, w(n).

The third line contains a positive integer v0 representing the given starting point.

The fourth line contains a real number ρ representing the given positive constant less than 1.

Next m lines, each line contains two positive integers x, y, indicating that <x, y> is a directed edge of G. There may be self-loops, but no heavy edges.

 

Output format:

 

Contains only one real number, the largest possible value of the H value, rounded to one decimal place.

 

Input and output example

Input Example #1:  Copy
5 5
10.0 8.0 8.0 8.0 15.0
1
0.5
1 2
2 3
3 4
4 2
4 5
Output Sample #1:  Copy
18.0

illustrate

For 100% of the data, n ≤ 100, m ≤ 1000, ρ ≤ 1 – 10^-6, w(i) ≤ 100 (i = 1, 2, …, n).

Because one decimal place is reserved, and w(i)<=100

So when $p^k$<1e-4 can be ignored

That is, when the number of edges is greater than k, it can be ignored

The maximum k is:

$log_{0.999999}1e-4$ is approximately equal to $10^7$

So use multiplication floyd to find the maximum value

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 using namespace std;
 7 double f[26][101][101],w[101],inf=2e18,p,ans;
 8 int n,m,S;
 9 int main()
10 {int i,j,k,t,u,v;
11   cin>>n>>m;
12   for (i=1;i<=n;i++)
13     {
14       scanf("%lf",&w[i]);
15     }
16   cin>>S;
17   for (i=0;i<=25;i++)
18     for (j=1;j<=n;j++)
19       for (k=1;k<=n;k++)
20     f[i][j][k]=-inf;
21   for (i=1;i<=n;i++)
22     f[0][i][i]=0;
23   scanf("%lf",&p);
24   for (i=1;i<=m;i++)
25     {
26       scanf("%d%d",&u,&v);
27       f[0][u][v]=p*w[v];
28     }
29   for (t=1;t<=25;t++)
30     {
31       for (i=1;i<=n;i++)
32     {
33       for (j=1;j<=n;j++)
34         {
35           for (k=1;k<=n;k++)
36         if (f[t-1][i][k]!=-inf&&f[t-1][k][j]!=-inf)
37           f[t][i][j]=max(f[t][i][j],f[t-1][i][k]+f[t-1][k][j]*p);
38         }
39     }
40       p=p*p;
41     }
42   ans=-inf;
43   for (i=1;i<=n;i++)
44     ans=max(ans,f[25][S][i]+w[S]);
45   printf("%.1lf\n",ans);
46 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324940919&siteId=291194637