답안 #239688

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
239688 2020-06-17T07:43:08 Z kartel Ceste (COCI17_ceste) C++14
160 / 160
459 ms 3064 KB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define in(x) freopen(x, "r", stdin)
#define out(x) freopen(x, "w", stdout)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#define F first
#define S second
#define pb push_back
#define N +100500
#define M ll(1e9 + 7)
#define sz(x) (int)x.size()
#define re return
#define oo ll(1e9)
#define el '\n'
#define pii pair <int, int>
#define err ld(1e-9)
using namespace std;
//using namespace __gnu_pbds;
//typedef tree <int, null_type, less_equal <int> , rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef long long ll;
typedef long double ld;

ll ans[N], A[N], B[N], x, y, u, v, i, j, n, m;
vector <pair <ll, ll> > g[N];
ld cs, sn;
pair <ld, pair <ll, ll> > dist[N];

set <pair <ld, ll> > q;

int main()
{
    srand(time(0));
    ios_base::sync_with_stdio(0);
    iostream::sync_with_stdio(0);
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);

//    in("input.txt");
//    out("output.txt");

    cin >> n >> m;
    for (i = 1; i <= m; i++)
    {
        cin >> v >> u >> A[i] >> B[i];

        g[u].pb({v, i});
        g[v].pb({u, i});
    }

    for (i = 1; i <= n; i++) ans[i] = 1e18;

    sn = 1;
    cs = 0;

    while (1)
    {
        for (i = 1; i <= n; i++) dist[i] = {1e18, {1e9, 1e9}};

        q.insert({0.0, 1});

        dist[1] = {0.0, {0, 0}};

        while (sz(q))
        {
            ll v = (*q.begin()).S;

            q.erase(q.begin());

            for (auto to : g[v])
            {
                ll u = to.F;
                ll i = to.S;

                ld new_dist = dist[v].F + sn * ld(A[i]) + cs * ld(B[i]);

                if (new_dist + err < dist[u].F)
                {
                    q.erase({dist[u].F, u});

                    dist[u] = {new_dist, {dist[v].S.F + A[i], dist[v].S.S + B[i]}};

                    q.insert({dist[u].F, u});
                }
            }
        }

        ld new_sin = 0;

        for (v = 1; v <= n; v++)
        {
            if (dist[v].F == 1e18) continue;

            if (v > 1)
                ans[v] = min(ans[v], ll(dist[v].S.F * dist[v].S.S));

            for (auto to : g[v])
            {
                ll u = to.F;
                ll i = to.S;

                ll s = (dist[v].S.F + A[i]) - dist[u].S.F;
                ll c = (dist[v].S.S + B[i]) - dist[u].S.S;

                if (s == c) continue;

                ld maybe = ld(c) / (ld(c) - ld(s));

                if (maybe + err < sn && maybe > new_sin)
                    new_sin = maybe;
            }
        }

        if (new_sin >= sn) break;

        sn = new_sin;
        cs = 1 - sn;
    }

    for (i = 2; i <= n; i++)
       if (ans[i] == 1e18)
         cout << "-1" << el;
    else cout << ans[i] << el;
}

/*
8 8
...).).*
*....)..
.)*(..).
(*)((...
.)).)(..
.)(.)..(
...).(.*
M.......
*/
//
//110000
//1100
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 2688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 2688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 2688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 2688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 92 ms 2936 KB Output is correct
2 Correct 176 ms 2900 KB Output is correct
3 Correct 44 ms 2816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 2816 KB Output is correct
2 Correct 25 ms 2816 KB Output is correct
3 Correct 40 ms 2816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 204 ms 3064 KB Output is correct
2 Correct 208 ms 2944 KB Output is correct
3 Correct 315 ms 3064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 276 ms 2944 KB Output is correct
2 Correct 459 ms 3064 KB Output is correct
3 Correct 22 ms 2944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 166 ms 2944 KB Output is correct
2 Correct 228 ms 2944 KB Output is correct
3 Correct 271 ms 2944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 221 ms 2944 KB Output is correct
2 Correct 263 ms 3064 KB Output is correct
3 Correct 280 ms 2944 KB Output is correct