求助,为啥TLE了
查看原帖
求助,为啥TLE了
124740
ethan_zhou楼主2022/4/8 12:02

rt,这代码里循环都写死的,n也才1000,为啥会tle?

#include <bits/stdc++.h>
using namespace std;
const char nl = '\n';
const double INF = 1e18, EPS = 1e-7;
const int MXN = 1005;
int n;
bool vis[MXN];
double p[MXN][MXN], prod[MXN], f[MXN];
int main(int argc, char *argv[]) {
    /* freopen("test.in", "r", stdin); */
    /* freopen("test.out","w",stdout); */
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> p[i][j];
            p[i][j] *= 0.01;
        }
        prod[i] = f[i] = 1;
    }
    f[n] = 0;
    auto eq = [](double x, double y) { return fabs(x - y) <= 1e-7; };
    for (int i = 1; i <= n; i++) {
        int u = 0;
        double v = INF;
        if (i == 1)
            u = n;
        else
            for (int j = 1; j <= n; j++)
                if (!vis[j] && !eq(prod[j], 1)) {
                    double _v = f[j] / (1. - prod[j]);
                    if (_v < v) u = j, v = _v;
                }
        vis[u] = 1;
        if (!eq(prod[u], 1)) f[u] /= 1. - prod[u];
        if (u == 1) {
            cout.flags(ios::fixed);
            cout.precision(10);
            cout << f[1] << nl;
            return 0;
        }
        for (int j = 1; j <= n; j++)
            if (!vis[j]) {
                f[j] += p[j][u] * prod[j] * f[u];
                prod[j] *= (1. - p[j][u]);
            }
    }

    return 0;
}
2022/4/8 12:02
加载中...