QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#129826#4370. Road TimesSwarthmore#TL 134ms4076kbC++176.5kb2023-07-23 01:29:302023-07-23 01:29:33

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-23 01:29:33]
  • 评测
  • 测评结果:TL
  • 用时:134ms
  • 内存:4076kb
  • [2023-07-23 01:29:30]
  • 提交

answer

#include "bits/stdc++.h"
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
 
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
 
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template<class T> using pq = priority_queue<T>;
template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
 
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a,x) for (auto& a : x)
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
 
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert

template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef DEBUG
#define dbg(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << endl;
#else
#define dbg(x...)
#endif


const int MOD = 1000000007;
const char nl = '\n';
const int MX = 100001; 

typedef long double T;
typedef vector<vd> vvd;
const T eps = 1e-9, inf = 1/.0;
#define ltj(X) if (s == -1 || mp(X[j], N[j]) < mp(X[s], N[s])) s = j

struct LPSolver {
    int m, n;
    vi N, B;
    vvd D;
    LPSolver(const vvd &A, const vd& b, const vd& c) :
        m(sz(b)), n(sz(c)), N(n+1), B(m), D(m+2, vd(n+2)) {
            F0R(i, m) F0R(j, n) D[i][j] = A[i][j];
            F0R(i, m) {
                B[i] = n+i, D[i][n] = -1, D[i][n+1] = b[i];
            }
            F0R(j, n) {
                N[j] = j;
                D[m][j] = -c[j];
            }
            N[n] = -1; D[m+1][n] = 1;
    }

    void pivot(int r, int s) {
        T *a = D[r].data(), inv = 1/a[s];
        F0R(i, m+2) if (i != r && abs(D[i][s]) > eps) {
            T *b = D[i].data(), binv = b[s]*inv;
            F0R(j, n+2) b[j] -= a[j]*binv;
            b[s] = a[s]*binv;
        }
        F0R(j, n+2) if (j != s) D[r][j] *= inv;
        F0R(i, m+2) if (i != r) D[i][s] *= -inv;
        D[r][s] = inv; swap(B[r], N[s]);
    }

    bool simplex(int phase) {
        int x = m+phase-1;
        while(true) {
            int s = -1; F0R(j, n+1) if (N[j] != -phase) ltj(D[x]);
            if (D[x][s] >= -eps) return true;
            int r = -1;
            F0R(i, m) {
                if (D[i][s] <= eps) continue;
                if (r == -1 || mp(D[i][n+1] / D[i][s], B[i]) < mp(D[r][n+1] / D[r][s], B[r])) r = i;
            }
            if (r == -1) return false;
            pivot(r, s);
        }
    }

    T solve() {
        int r = 0; FOR(i, 1, m) if (D[i][n+1] < D[r][n+1]) r = i;
        if (D[r][n+1] < -eps) {
            pivot(r, n);
            if (!simplex(2) || D[m+1][n+1] < -eps) return -inf;
            F0R(i, m) if (B[i] == -1) {
                int s = 0; FOR(j, 1, n+1) ltj(D[i]);
                pivot(i, s);
            }
        }
        bool ok = simplex(1);
        return ok ? D[m][n+1] : inf;
    }
};

vector<vector<pair<int, pi>>> graph(31);
int N;

vi getPath(int r, int s) {
    int dist[N];
    F0R(i, N) dist[i] = 1e9;
    dist[r] = 0;
    pqg<pi> q; q.push({0, r});
    pi lst[N];
    while (!q.empty()) {
        auto [d, v] = q.top(); q.pop();
        if (d != dist[v]) continue;
        trav(a, graph[v]) {
            if (ckmin(dist[a.f], d + a.s.f)) {
                q.push({dist[a.f], a.f});
                lst[a.f] = {v, a.s.s};
            }
        }
    }
    int cur = s;
    vi res;
    while (cur != r) {
        res.pb(lst[cur].s); cur = lst[cur].f;
    }
    return res;
}

void solve() {
    cin >> N;
    int nr = 0;
    vd dist;
    F0R(i, N) {
        F0R(j, N) {
            int X; cin >> X;
            if (i != j && X != -1) {
                graph[i].pb({j, {X, nr}});
                dist.pb(X);
                nr++;
            }
        }
    }

    vvd A;
    vd B;
    F0R(i, nr) {
        vd cur(nr);
        cur[i] = 1;
        A.pb(cur);
        B.pb(dist[i]*2);
        cur[i] = -1;
        A.pb(cur);
        B.pb(-dist[i]);
    }
    int R; cin >> R;
    F0R(i, R) {
        int X, Y; ld T; cin >> X >> Y >> T;
        vi pth = getPath(X, Y);
        vd cur(nr);
        trav(a, pth) cur[a] = 1;
        A.pb(cur);
        B.pb(T+eps);
        trav(a, pth) cur[a] = -1;
        A.pb(cur);
        B.pb(-T+eps);
    }

    /*dbg(A);
    dbg(B);*/

    cout << setprecision(20);
    int Q; cin >> Q;
    while(Q--) {
        int X, Y; cin >> X >> Y;
        cout << X << " " << Y << " ";
        vi pth = getPath(X, Y);
        vd cur(nr);
        trav(a, pth) cur[a] = -1;
        LPSolver lp1(A, B, cur);
        cout << -lp1.solve() << " ";
        trav(a, pth) cur[a] = 1;
        LPSolver lp2(A, B, cur);
        cout << lp2.solve() << nl;
    }

}
 
int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);

    int T = 1;
//    cin >> T;
    while(T--) {
        solve();
    }

	return 0;
}



详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3880kb

input:

3
0 50 -1
55 0 40
-1 40 0
1
0 2 120
3
0 1
1 2
1 0

output:

0 1 50 80.000000000999999999
1 2 40 70.000000000999999999
1 0 55 110

result:

ok 12 numbers

Test #2:

score: 0
Accepted
time: 0ms
memory: 3880kb

input:

3
0 50 -1
55 0 40
-1 40 0
1
0 2 90
3
0 1
1 2
1 0

output:

0 1 50 50.000000000999999999
1 2 40 40.000000000999999999
1 0 55 110

result:

ok 12 numbers

Test #3:

score: 0
Accepted
time: 1ms
memory: 3816kb

input:

3
0 50 -1
55 0 40
-1 40 0
1
0 2 180
3
0 1
1 2
1 0

output:

0 1 99.999999999000000001 100
1 2 79.999999999000000001 80
1 0 55 110

result:

ok 12 numbers

Test #4:

score: 0
Accepted
time: 1ms
memory: 3820kb

input:

6
0 960 -1 -1 -1 -1
-1 0 -1 -1 1000 -1
-1 1000 0 -1 -1 -1
-1 -1 -1 0 -1 -1
-1 -1 -1 1000 0 970
-1 -1 -1 -1 -1 0
3
0 3 5900
2 3 5800
2 5 5700
6
0 1
2 1
1 4
4 3
4 5
0 5

output:

0 1 1899.9999999989999999 1920
2 1 1799.9999999989999999 1820.0000000020000002
1 4 1979.9999999989999999 2000
4 3 1979.9999999989999999 2000
4 5 1879.9999999969999998 1900.0000000020000002
0 5 5799.9999999969999998 5800.0000000030000002

result:

ok 24 numbers

Test #5:

score: 0
Accepted
time: 0ms
memory: 3820kb

input:

6
0 960 -1 -1 -1 -1
-1 0 -1 -1 1000 -1
-1 1000 0 -1 -1 -1
-1 -1 -1 0 -1 -1
-1 -1 -1 1000 0 940
-1 -1 -1 -1 -1 0
3
0 3 5900
2 3 5800
2 5 5700
6
0 1
2 1
1 4
4 3
4 5
0 5

output:

0 1 1919.9999999969999998 1920
2 1 1819.9999999989999999 1820.0000000020000002
1 4 1999.9999999969999998 2000
4 3 1979.9999999989999999 1980.0000000020000002
4 5 1879.9999999969999998 1880
0 5 5799.9999999969999998 5800

result:

ok 24 numbers

Test #6:

score: 0
Accepted
time: 0ms
memory: 3888kb

input:

6
0 950 -1 -1 -1 -1
-1 0 -1 -1 1000 -1
-1 1000 0 -1 -1 -1
-1 -1 -1 0 -1 -1
-1 -1 -1 1000 0 970
-1 -1 -1 -1 -1 0
3
0 3 5900
2 3 5800
2 5 5700
6
0 1
2 1
1 4
4 3
4 5
0 5

output:

0 1 1899.9999999989999999 1900
2 1 1799.9999999989999999 1800.0000000020000002
1 4 1999.9999999989999999 2000
4 3 1999.9999999989999999 2000
4 5 1899.9999999969999998 1900.0000000020000002
0 5 5799.9999999969999998 5800.0000000020000002

result:

ok 24 numbers

Test #7:

score: 0
Accepted
time: 7ms
memory: 3776kb

input:

10
0 123 -1 -1 -1 -1 -1 -1 -1 -1
-1 0 234 -1 -1 -1 -1 -1 -1 -1
-1 -1 0 345 -1 -1 -1 -1 -1 -1
-1 -1 -1 0 456 -1 -1 -1 -1 -1
-1 -1 -1 -1 0 567 -1 -1 -1 -1
-1 -1 -1 -1 -1 0 678 -1 -1 -1
-1 -1 -1 -1 -1 -1 0 890 -1 -1
-1 -1 -1 -1 -1 -1 -1 0 901 -1
-1 -1 -1 -1 -1 -1 -1 -1 0 555
666 -1 -1 -1 -1 -1 -1 -1 -1...

output:

0 0 -0 0
0 1 215.99999999499999953 246
0 2 449.99999999499999953 714
0 3 1083.9999999969999996 1114.0000000020000002
0 4 1539.9999999969999996 1570.0000000020000002
0 5 2673.9999999969999998 2704.0000000020000002
0 6 3407.9999999959999997 3438.0000000010000001
0 7 4297.9999999959999997 4358.00000000...

result:

ok 400 numbers

Test #8:

score: 0
Accepted
time: 3ms
memory: 3848kb

input:

10
0 123 -1 -1 -1 -1 -1 -1 -1 -1
-1 0 234 -1 -1 -1 -1 -1 -1 -1
-1 -1 0 345 -1 -1 -1 -1 -1 -1
-1 -1 -1 0 456 -1 -1 -1 -1 -1
-1 -1 -1 -1 0 567 -1 -1 -1 -1
-1 -1 -1 -1 -1 0 678 -1 -1 -1
-1 -1 -1 -1 -1 -1 0 890 -1 -1
-1 -1 -1 -1 -1 -1 -1 0 901 -1
-1 -1 -1 -1 -1 -1 -1 -1 0 555
666 -1 -1 -1 -1 -1 -1 -1 -1...

output:

0 0 -0 0
0 1 215.9999999949999995 246.00000000000000003
0 2 579.99999999499999975 640.00000000700000075
0 3 1083.9999999969999998 1114.0000000020000003
0 4 1539.9999999969999996 1570.0000000020000002
0 5 2673.9999999969999995 2704.0000000020000002
0 6 3407.9999999959999992 3438.0000000009999999
0 7 ...

result:

ok 400 numbers

Test #9:

score: 0
Accepted
time: 8ms
memory: 3796kb

input:

10
0 123 -1 -1 -1 -1 -1 -1 -1 -1
-1 0 234 -1 -1 -1 -1 -1 -1 -1
-1 -1 0 345 -1 -1 -1 -1 -1 -1
-1 -1 -1 0 456 -1 -1 -1 -1 -1
-1 -1 -1 -1 0 567 -1 -1 -1 -1
-1 -1 -1 -1 -1 0 678 -1 -1 -1
-1 -1 -1 -1 -1 -1 0 890 -1 -1
-1 -1 -1 -1 -1 -1 -1 0 901 -1
-1 -1 -1 -1 -1 -1 -1 -1 0 555
666 -1 -1 -1 -1 -1 -1 -1 -1...

output:

0 0 -0 0
0 1 244.99999999599999932 246
0 2 608.99999999599999917 640.00000000700000119
0 3 1083.9999999969999994 1114.0000000020000004
0 4 1568.9999999979999998 1570.0000000020000005
0 5 2702.9999999979999998 2704.0000000020000006
0 6 3436.9999999969999995 3438.0000000010000003
0 7 4326.999999996999...

result:

ok 400 numbers

Test #10:

score: 0
Accepted
time: 1ms
memory: 3776kb

input:

3
0 10 -1
-1 0 10
10 -1 0
3
0 2 21
1 0 21
2 1 21
3
0 1
1 2
2 0

output:

0 1 10.499999998500000001 10.500000001499999999
1 2 10.499999998500000001 10.500000001499999999
2 0 10.499999998500000001 10.500000001499999999

result:

ok 12 numbers

Test #11:

score: 0
Accepted
time: 2ms
memory: 3772kb

input:

8
0 10 -1 -1 -1 -1 -1 -1
-1 0 10 -1 -1 -1 -1 -1
-1 -1 0 10 -1 -1 -1 -1
-1 -1 -1 0 10 -1 -1 -1
-1 -1 -1 -1 0 10 -1 -1
-1 -1 -1 -1 -1 0 10 -1
-1 -1 -1 -1 -1 -1 0 10
10 -1 -1 -1 -1 -1 -1 0
8
0 7 71
1 0 71
2 1 71
3 2 71
4 3 71
5 4 71
6 5 71
7 6 71
8
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 0

output:

0 1 10.142857141 10.142857144714285712
1 2 10.142857141 10.142857144714285712
2 3 10.142857141000000001 10.142857144714285711
3 4 10.142857141000000001 10.142857144714285711
4 5 10.142857141000000001 10.142857144714285711
5 6 10.142857141000000001 10.142857144714285711
6 7 10.142857141000000001 10.1...

result:

ok 32 numbers

Test #12:

score: 0
Accepted
time: 1ms
memory: 3764kb

input:

7
0 10 -1 -1 -1 -1 -1
-1 0 10 -1 -1 -1 -1
-1 -1 0 10 -1 -1 -1
-1 -1 -1 0 10 -1 -1
-1 -1 -1 -1 0 10 -1
-1 -1 -1 -1 -1 0 10
10 -1 -1 -1 -1 -1 0
6
0 4 41
1 5 41
2 6 41
3 0 41
5 2 41
6 3 41
7
0 1
1 2
2 3
3 4
4 5
5 6
6 0

output:

0 1 10 11.000000000999999996
1 2 10.000000000000000002 10.333333335666666666
2 3 10.000000000000000002 10.333333335666666666
3 4 10.000000000000000002 10.333333335666666666
4 5 10 11.000000000999999996
5 6 10.000000000000000002 10.333333335666666666
6 0 10.000000000000000002 10.333333335666666666

result:

ok 28 numbers

Test #13:

score: 0
Accepted
time: 133ms
memory: 4076kb

input:

30
0 392 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 0 -1 -1 -1 -1 -1 -1 -1 -1 793 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 0 -1 -1 -1 -1 750 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
703 -1 -1 0 -1 -1 -1 -1 -1 ...

output:

1 10 793.17241379113793054 793.17241379506896609
10 16 726.17241379113793015 726.17241379506896604
16 29 367.17241379113793048 367.1724137950689662
29 15 812.17241379113793015 812.17241379506896581
15 24 959.17241379113793015 959.17241379506896604
24 2 826.17241379113793015 826.17241379506896604
2 7...

result:

ok 400 numbers

Test #14:

score: 0
Accepted
time: 134ms
memory: 3956kb

input:

30
0 392 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 0 -1 -1 -1 -1 -1 -1 -1 -1 793 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 0 -1 -1 -1 -1 750 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
703 -1 -1 0 -1 -1 -1 -1 -1 ...

output:

1 10 792.99999999999999994 793.17857143053571495
10 16 725.99999999999999994 726.17857143053571534
16 29 366.99999999999999981 367.17857143053571525
29 15 811.99999999999999978 812.17857143053571534
15 24 958.99999999999999978 959.17857143053571511
24 2 825.99999999999999978 826.17857143053571511
2 ...

result:

ok 400 numbers

Test #15:

score: -100
Time Limit Exceeded

input:

1
0
100
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 ...

output:


result: