QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#46150#2161. The Cost of Speed Limitsdmga44ML 3ms6196kbC++173.4kb2022-08-26 10:13:432022-08-26 10:13:43

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-08-26 10:13:43]
  • 评测
  • 测评结果:ML
  • 用时:3ms
  • 内存:6196kb
  • [2022-08-26 10:13:43]
  • 提交

answer

// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops", "omit-frame-pointer", "inline")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,fma,tune=native")
// #pragma GCC option("arch=native", "no-zero-upper") // Enable AVX

/// UH Top
#include <bits/stdc++.h>
#define db(x) cerr << #x << ':' << (x) << '\n';
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
// #define int ll
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
// typedef __int128_t int128;
typedef pair<ll, ll> pii;
typedef pair<ld, ll> pdi;
typedef pair<ld, ld> pdd;
typedef pair<ld, pdd> pdp;
typedef pair<string, ll> psi;
typedef pair<ll, string> pls;
typedef pair<string, string> pss;
typedef pair<ll, pii> pip;
typedef pair<pii, pii> ppp;
typedef complex<ld> point;
typedef vector<point> polygon;
typedef vector<ll> vi;
typedef pair<point, int> ppi;
#define prec(n)        \
    cout.precision(n); \
    cout << fixed
const ll mod = (1e9 + 7);
const ld eps = (1e-9);
const ll oo = (ll)(1e18 + 5);
#define pi acos(-1)
#define MAXN (ll)(2e4 + 5)

int cont, c;
vector<int> vs;
int trans[MAXN * 5];
int dp[MAXN][MAXN];
vector<pii> g[MAXN];
ll ans;

void dfs(int u, int p, int w)
{
    vector<int> sons;
    for (auto e : g[u])
        if (e.first != p)
        {
            dfs(e.first, u, e.second);
            sons.push_back(e.first);
        }

    if (sons.empty() && u)
    {
        int ini = trans[w];
        for (int i = 0; i < cont; i++)
        {
            if (i < ini)
                dp[u][i] = 2e9;
            else
                dp[u][i] = vs[i] - vs[ini];
        }
    }
    else if(u)
    {
        int ini = trans[w];
        ll mi = 0;
        for (auto v : sons)
        {
            int add = 2e9;
            for (int i = 0; i < cont; i++)
                add = min(add, dp[v][i]);
            mi += add;
        }
        for (int i = 0; i < cont; i++)
        {
            if (i < ini)
                dp[u][i] = 2e9;
            else
            {
                ll x1 = 0;
                for (auto v : sons)
                    x1 += dp[v][i];

                dp[u][i] = min(((ll)2e9), x1 + vs[i] - vs[ini]);
                if(dp[u][i]>mi + vs[i] - vs[ini] + 1ll * (sons.size() + 1) * c)
                    dp[u][i]=mi + vs[i] - vs[ini] + 1ll * (sons.size() + 1) * c;
            }
        }
    }

    if(!u)
    {
        ll mi = 0;
        for (auto v : sons)
        {
            int add = 2e9;
            for (int i = 0; i < cont; i++)
                add = min(add, dp[v][i]);
            mi += add;
        }
        ans=mi+1ll*sons.size()*c;
        for (int i = 0; i < cont; i++)
        {
            ll x1 = 0;
            for (auto v : sons)
                x1 += dp[v][i];

            ans=min(ans,x1);
        }
    }
}

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

    int n;
    cin >> n >> c;
    set<int> ww;
    for (int i = 0; i < n - 1; i++)
    {
        int u, v, w;
        cin >> u >> v >> w;
        u--, v--;
        g[u].push_back(pii(v, w));
        g[v].push_back(pii(u, w));
        ww.insert(w);
    }

    for (auto x : ww)
    {
        trans[x] = cont;
        vs.push_back(x);
        cont++;
    }

    dfs(0, -1, -1);

    cout << ans << '\n';

    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 3ms
memory: 4112kb

input:

13 20
1 8 101
1 9 30
1 2 100
1 3 100
2 4 75
2 5 70
2 6 82
2 7 77
3 10 73
3 11 69
3 12 83
3 13 79

output:

272

result:

ok single line: '272'

Test #2:

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

input:

9 10
1 6 26
2 6 27
3 6 28
4 6 29
5 6 30
7 9 14
8 9 1
9 6 10

output:

60

result:

ok single line: '60'

Test #3:

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

input:

7 64
2 1 194
3 1 187
4 2 158
5 1 42
6 5 101
7 5 80

output:

308

result:

ok single line: '308'

Test #4:

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

input:

6 32
2 1 110
3 2 36
4 1 54
5 3 101
6 5 71

output:

178

result:

ok single line: '178'

Test #5:

score: -100
Memory Limit Exceeded

input:

20000 100
2273 4097 98627
14155 14055 33506
16060 6363 28081
14840 12695 23379
11520 7892 5831
6633 13834 73944
19218 19341 62064
14392 160 58289
18147 209 46443
16941 5453 55103
11895 12849 31201
10275 1622 71781
19595 6349 14232
19485 10800 9778
10745 13541 44786
18727 15264 25726
5847 12815 43894...

output:


result: