幸福的通道-洛谷4308

传送门

 

真的不好想到啊

刚看完题目的我

一脸懵

 

做法:

  倍增+Floyd

 

 

#include<cstdio>
#include<algorithm>
#include<cstring> 
#include<iostream>
using namespace std;

inline int read()//快读 
{
    int sum = 0,p = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-')
            p = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
        (sum *= 10)+= ch - '0';
        ch = getchar();
    }
    return sum * p;
}

#define lb long double
const int maxn = 150;
const lb inf = 1e30;
int n,m,s;
lb f[maxn][maxn],g[maxn][maxn],p,a[maxn],ans;

int main()
{
    n = read(),m = read();
    for(int i = 1;i <= n;i++)
        cin>>a[i];
    s = read();
    cin>>p;
    int x,y;
    for(int i = 0;i <= n;i++)
        for(int j = 0;j <= n;j++)
            f[i][j] = -inf;
    for(int i = 1;i <= n;i++)
        f[i][i] = 0;
    for(int i = 1;i <= m;i++)
    {
        x = read(),y = read();
        f[x][y] = a[y] * p; 
    }
    for(lb tmp = p;tmp > (lb)(1e-8);tmp *= tmp)
    {
        for(int i = 1;i <= n;i++)
            for(int j = 1;j <= n;j++)
                g[i][j] = -inf;
        for(int k = 1;k <= n;k++)
            for(int i = 1;i <= n;i++)
                for(int j = 1;j <= n;j++)
                    g[i][j] = max(g[i][j],f[i][k] + f[k][j] * tmp);
        memcpy(f,g,sizeof(g));
    }
    for(int i = 1;i <= n;i++)
        ans = max(ans,f[s][i]);
    printf("%.1Lf",ans + a[s]);
    return 0;
}

 

posted @ 2019-04-07 21:56  darrrr  阅读(132)  评论(0编辑  收藏  举报