Submission #962206

# Submission time Handle Problem Language Result Execution time Memory
962206 2024-04-13T08:56:12 Z stev2005 Dreaming (IOI13_dreaming) C++17
47 / 100
1000 ms 11604 KB
#include "dreaming.h"
#include<bits/stdc++.h>
using namespace std;
const int maxn = (1<<17), inf = (1<<30);
int n, m, lprice;

struct edge{
    int ver, cost;
    edge(){
        ver = -1;
        cost = 0;
    }
    edge(int _ver, int _cost){
        ver = _ver;
        cost = _cost;
    }
    bool operator<(const edge &other) const{
        return cost > other.cost;
    }
};

struct diamgr{
    int s, f;
    int cost, sum1, sum2;
    diamgr(){
        /*mid =*/ s = f = -1;
        cost = sum1 = sum2 = -1;
    }
    diamgr(int _s, int _f, int _cost, int _sum1, int _sum2){
        s = _s;
        f = _f;
        //mid = _mid;
        cost = _cost;
        sum1 = _sum1;
        sum2 = _sum2;
    }
};

vector <edge> v[maxn];
bool used[maxn];
bool checkver[maxn];
int parsum[maxn], parsum1[maxn], parsum2[maxn];

int anscost;
int d[maxn];
int par[maxn], costpar[maxn];


inline void init_vectors(int *a, int *b, int *t){
    for (int i = 0; i < m; ++i){
        v[a[i]].push_back(edge(b[i], t[i]));
        v[b[i]].push_back(edge(a[i], t[i]));
        //cerr << a[i] << " " << b[i] << " " << t[i] << endl;
    }
}

void check_component(int ver){
    checkver[ver] = 1;
    for (auto nb: v[ver]){
        //cerr << "ver: " << ver << " nb: " << nb.ver << "\n";
        if (!checkver[nb.ver])
            check_component(nb.ver);
    }
}

int Dijkstra(int beg){
    memset(used, 0, sizeof(used));
    memset(par, -1, sizeof(par));
    memset(costpar, 0, sizeof(costpar));
    used[beg] = 1;
    checkver[beg] = 1;
    for (int i = 0; i < n; ++i)
        d[i] = inf;
    d[beg] = 0;
    costpar[beg] = 0;
    par[beg] = -1;
    priority_queue<edge> q;
    q.push(edge(beg, 0));
    int lastpopped = -1;
    while (!q.empty()){
        edge cur = q.top();
        q.pop();
        lastpopped = cur.ver;
        if (cur.cost <= d[cur.ver]){
            used[cur.ver] = 1;
            checkver[cur.ver] = 1;
            for (int i = 0; i < (int)v[cur.ver].size(); ++i){
                edge nb = v[cur.ver][i];
                if (cur.cost + nb.cost < d[nb.ver]){
                    par[nb.ver] = cur.ver;
                    costpar[nb.ver] = nb.cost;
                    d[nb.ver] = cur.cost + nb.cost;
                    nb.cost = d[nb.ver];
                    q.push(nb);
                }
            }
        }
    }
    return lastpopped;
}

int init_parsums(int s, int f, int *parsum){
    int cur = f, prev = -1;
    int cnt = 1;
    while (cur != -1){
        parsum[cnt] = parsum[cnt - 1] + costpar[cur];
        cur = par[cur];
        cnt++;
    }
    return cnt;
}

pair<int, int> find_mindif(int n, int *parsum){
    if (n == 1) return {0, 0};
    if (n == 2) return {0, parsum[1]};
    int sum1 = -(1<<30) + 10, sum2 = (1<<30) - 10;
    for (int i = 0; i < n; ++i){
        int cursum1 = parsum[i];
        int cursum2 = parsum[n - 1] - parsum[i];
        if (abs(cursum2 - cursum1) < abs(sum2 - sum1)){
            sum1 = cursum1;
            sum2 = cursum2;
        }
    }
    return {sum1, sum2};
}

pair<int, int> find_gr_dim(int ver){
    //cerr << "find_gr_dim ver: " << ver << "\n";
    check_component(ver);
    if (v[ver].size() == 0){
        return {0, 0};
    }
    int s = Dijkstra(ver);
    int f = Dijkstra(s);
    //cerr << "Gr. diam: " << d[f] << endl;
    memset(parsum, 0, sizeof(parsum));
    int cntver = init_parsums(s, f, parsum);
    pair<int, int> sums = find_mindif(cntver, parsum);
    //cerr << "sums: " << sums.first << " " << sums.second << "\n";
    return sums;
}

inline void subtask123(int *a, int *b, int *t){
    init_vectors(a, b, t);
    pair<int, int> info1, info2;
    bool lamp = false;
    for (int i = 0; i < n; ++i){
        //cerr << "checkver[" << i << "] == " << checkver[i] << endl;
        if (!checkver[i]){
            if (!lamp){
                info1 = find_gr_dim(i);
                lamp = true;
            }
            else info2 = find_gr_dim(i);
        }
    }
    int diam1 = info1.first + info1.second;
    int diam2 = info2.first + info2.second;
    int max1 = max(info1.first, info1.second);
    int max2 = max (info2.first, info2.second);
    anscost = max1 + max2 + lprice;
    anscost = max(anscost, diam1);
    anscost = max(anscost, diam2);
}

int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
    n = N;
    m = M;
    lprice = L;
    //subtask1(A, B, T);
    subtask123(A, B, T);
    return anscost;
}

    /*cout << n1 << " " << n2 << "\n";
    for (int i = 0; i < n1; ++i)
        cout << parsum1[i] << " ";
    cout << "\n";
    for (int i = 0; i < n2; ++i)
        cout << parsum2[i] << " ";
    cout << "\n";*/

/*int find_deepest(int ver){
    //cout << "ver " << ver << "\n";
    used[ver] = true;
    if (!used[v[ver][0].first])
        return find_deepest(v[ver][0].first);
    if (v[ver].size() > 1 && !used[v[ver][1].first])
        return find_deepest(v[ver][1].first);
    return ver;
}

inline void init_chain(int i, int &s, int &f){
    //cout << "i: " << i << "\n";
    used[i] = true;
    if (v[i].size() == 0) s = i;
    else s = find_deepest(v[i][0].first);
    //cerr << "found one end\n";
    if (v[i].size() <= 1) f = i;
    else f = find_deepest(v[i][1].first);
    //cerr << "found another end\n\n";
}

inline void find_chains(int &s1, int &f1, int &s2, int &f2){
    s1 = f1 = s2 = f2 = -1;
    for (int i = 0; i < n; ++i)
        if (!used[i]){
            if (s1 == -1) init_chain(i, s1, f1);
            else init_chain(i, s2, f2);
        }
}

int init_parsums(int s, int f, int *parsum){
    int cur = s, prev = -1;
    int cnt = 1;
    while (cur != f){
        for (pair<int, int> nb: v[cur])
            if (nb.first != prev){
                parsum[cnt] = parsum[cnt - 1] + nb.second;
                prev = cur;
                cur = nb.first;
                break;
            }
        cnt++;
    }
    return cnt;
}

pair<int, int> find_mindif(int n, int *parsum){
    if (n == 1) return {0, 0};
    if (n == 2) return {0, parsum[1]};
    int sum1 = -(1<<30) + 10, sum2 = (1<<30) - 10;
    for (int i = 0; i < n; ++i){
        int cursum1 = parsum[i];
        int cursum2 = parsum[n - 1] - parsum[i];
        if (abs(cursum2 - cursum1) < abs(sum2 - sum1)){
            sum1 = cursum1;
            sum2 = cursum2;
        }
    }
    return {sum1, sum2};
}

inline void subtask1(int *a, int *b, int *t){
    init_vectors(a, b, t);
    int s1, f1, s2, f2;
    find_chains(s1, f1, s2, f2);
    int n1 = init_parsums(s1, f1, parsum1);
    int n2 = init_parsums(s2, f2, parsum2);
    pair<int, int> sums1, sums2;
    sums1 = find_mindif(n1, parsum1);
    sums2 = find_mindif(n2, parsum2);
    //cout << sums1.first << " " << sums1.second << " " << sums2.first << " " << sums2.second << "\n";
    int max1 = max(sums1.first, sums1.second);
    int max2 = max(sums2.first, sums2.second);
    anscost = max1 + max2 + lprice;
    anscost = max(anscost, sums1.first + sums1.second);
    anscost = max(anscost, sums2.first + sums2.second);
}
*/

Compilation message

dreaming.cpp: In function 'int init_parsums(int, int, int*)':
dreaming.cpp:103:18: warning: unused variable 'prev' [-Wunused-variable]
  103 |     int cur = f, prev = -1;
      |                  ^~~~
# Verdict Execution time Memory Grader output
1 Correct 53 ms 10328 KB Output is correct
2 Correct 33 ms 10436 KB Output is correct
3 Correct 29 ms 9308 KB Output is correct
4 Correct 6 ms 6744 KB Output is correct
5 Correct 5 ms 6744 KB Output is correct
6 Correct 10 ms 7260 KB Output is correct
7 Correct 2 ms 6236 KB Output is correct
8 Correct 22 ms 8148 KB Output is correct
9 Correct 23 ms 8792 KB Output is correct
10 Correct 2 ms 6232 KB Output is correct
11 Correct 32 ms 9296 KB Output is correct
12 Correct 37 ms 9812 KB Output is correct
13 Correct 3 ms 6232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6232 KB Output is correct
2 Correct 2 ms 6232 KB Output is correct
3 Correct 2 ms 6236 KB Output is correct
4 Correct 2 ms 6236 KB Output is correct
5 Correct 3 ms 6236 KB Output is correct
6 Correct 2 ms 6236 KB Output is correct
7 Correct 3 ms 6236 KB Output is correct
8 Correct 2 ms 6236 KB Output is correct
9 Correct 2 ms 6236 KB Output is correct
10 Correct 2 ms 6236 KB Output is correct
11 Correct 2 ms 6236 KB Output is correct
12 Correct 2 ms 6236 KB Output is correct
13 Correct 2 ms 6236 KB Output is correct
14 Correct 3 ms 6236 KB Output is correct
15 Correct 1 ms 3928 KB Output is correct
16 Correct 2 ms 6236 KB Output is correct
17 Correct 2 ms 6236 KB Output is correct
18 Correct 2 ms 6236 KB Output is correct
19 Correct 2 ms 6236 KB Output is correct
20 Correct 2 ms 6236 KB Output is correct
21 Correct 2 ms 6236 KB Output is correct
22 Correct 2 ms 6236 KB Output is correct
23 Correct 2 ms 6236 KB Output is correct
24 Correct 2 ms 6236 KB Output is correct
25 Correct 3 ms 6240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 10328 KB Output is correct
2 Correct 33 ms 10436 KB Output is correct
3 Correct 29 ms 9308 KB Output is correct
4 Correct 6 ms 6744 KB Output is correct
5 Correct 5 ms 6744 KB Output is correct
6 Correct 10 ms 7260 KB Output is correct
7 Correct 2 ms 6236 KB Output is correct
8 Correct 22 ms 8148 KB Output is correct
9 Correct 23 ms 8792 KB Output is correct
10 Correct 2 ms 6232 KB Output is correct
11 Correct 32 ms 9296 KB Output is correct
12 Correct 37 ms 9812 KB Output is correct
13 Correct 3 ms 6232 KB Output is correct
14 Correct 2 ms 6232 KB Output is correct
15 Correct 2 ms 6232 KB Output is correct
16 Correct 2 ms 6236 KB Output is correct
17 Correct 2 ms 6236 KB Output is correct
18 Correct 3 ms 6236 KB Output is correct
19 Correct 2 ms 6236 KB Output is correct
20 Correct 3 ms 6236 KB Output is correct
21 Correct 2 ms 6236 KB Output is correct
22 Correct 2 ms 6236 KB Output is correct
23 Correct 2 ms 6236 KB Output is correct
24 Correct 2 ms 6236 KB Output is correct
25 Correct 2 ms 6236 KB Output is correct
26 Correct 2 ms 6236 KB Output is correct
27 Correct 3 ms 6236 KB Output is correct
28 Correct 1 ms 3928 KB Output is correct
29 Correct 2 ms 6236 KB Output is correct
30 Correct 2 ms 6236 KB Output is correct
31 Correct 2 ms 6236 KB Output is correct
32 Correct 2 ms 6236 KB Output is correct
33 Correct 2 ms 6236 KB Output is correct
34 Correct 2 ms 6236 KB Output is correct
35 Correct 2 ms 6236 KB Output is correct
36 Correct 2 ms 6236 KB Output is correct
37 Correct 2 ms 6236 KB Output is correct
38 Correct 3 ms 6240 KB Output is correct
39 Correct 36 ms 10568 KB Output is correct
40 Correct 37 ms 10788 KB Output is correct
41 Correct 24 ms 9692 KB Output is correct
42 Correct 7 ms 6748 KB Output is correct
43 Correct 2 ms 6236 KB Output is correct
44 Correct 2 ms 6232 KB Output is correct
45 Correct 2 ms 6232 KB Output is correct
46 Correct 2 ms 6236 KB Output is correct
47 Correct 2 ms 6308 KB Output is correct
48 Correct 2 ms 6236 KB Output is correct
49 Correct 2 ms 6236 KB Output is correct
50 Correct 2 ms 6236 KB Output is correct
51 Correct 2 ms 6236 KB Output is correct
52 Correct 2 ms 6232 KB Output is correct
53 Correct 3 ms 6236 KB Output is correct
54 Correct 2 ms 6236 KB Output is correct
55 Correct 2 ms 6236 KB Output is correct
56 Correct 3 ms 6236 KB Output is correct
57 Correct 55 ms 11156 KB Output is correct
58 Correct 53 ms 11024 KB Output is correct
59 Correct 62 ms 11052 KB Output is correct
60 Correct 49 ms 10984 KB Output is correct
61 Correct 58 ms 11096 KB Output is correct
62 Correct 54 ms 11088 KB Output is correct
63 Correct 54 ms 10956 KB Output is correct
64 Correct 50 ms 10724 KB Output is correct
65 Correct 62 ms 11192 KB Output is correct
66 Correct 64 ms 11268 KB Output is correct
67 Correct 58 ms 11108 KB Output is correct
68 Correct 47 ms 10832 KB Output is correct
69 Correct 64 ms 10960 KB Output is correct
70 Correct 52 ms 11008 KB Output is correct
71 Correct 1 ms 3672 KB Output is correct
72 Correct 3 ms 6236 KB Output is correct
73 Correct 4 ms 6236 KB Output is correct
74 Correct 4 ms 6236 KB Output is correct
75 Correct 3 ms 6236 KB Output is correct
76 Correct 3 ms 6236 KB Output is correct
77 Correct 3 ms 6236 KB Output is correct
78 Correct 3 ms 6236 KB Output is correct
79 Correct 3 ms 6232 KB Output is correct
80 Correct 68 ms 11088 KB Output is correct
81 Correct 58 ms 11092 KB Output is correct
82 Correct 52 ms 10832 KB Output is correct
83 Correct 53 ms 10832 KB Output is correct
84 Correct 2 ms 6236 KB Output is correct
85 Correct 4 ms 6312 KB Output is correct
86 Correct 3 ms 6236 KB Output is correct
87 Correct 3 ms 6236 KB Output is correct
88 Correct 4 ms 6236 KB Output is correct
89 Correct 3 ms 6236 KB Output is correct
90 Correct 3 ms 6316 KB Output is correct
91 Correct 3 ms 6236 KB Output is correct
92 Correct 3 ms 6372 KB Output is correct
93 Correct 3 ms 6488 KB Output is correct
94 Correct 2 ms 6236 KB Output is correct
95 Correct 2 ms 6236 KB Output is correct
96 Correct 3 ms 6236 KB Output is correct
97 Correct 2 ms 6232 KB Output is correct
98 Correct 2 ms 6236 KB Output is correct
99 Correct 2 ms 6236 KB Output is correct
100 Correct 2 ms 6240 KB Output is correct
101 Correct 2 ms 6212 KB Output is correct
102 Correct 2 ms 6232 KB Output is correct
103 Correct 2 ms 6236 KB Output is correct
104 Correct 5 ms 6748 KB Output is correct
105 Correct 10 ms 7260 KB Output is correct
106 Correct 2 ms 6236 KB Output is correct
107 Correct 18 ms 8028 KB Output is correct
108 Correct 24 ms 8864 KB Output is correct
109 Correct 2 ms 6236 KB Output is correct
110 Correct 32 ms 9436 KB Output is correct
111 Correct 38 ms 10072 KB Output is correct
112 Correct 2 ms 6236 KB Output is correct
113 Correct 52 ms 11604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1075 ms 8540 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6232 KB Output is correct
2 Correct 2 ms 6232 KB Output is correct
3 Correct 2 ms 6236 KB Output is correct
4 Correct 2 ms 6236 KB Output is correct
5 Correct 3 ms 6236 KB Output is correct
6 Correct 2 ms 6236 KB Output is correct
7 Correct 3 ms 6236 KB Output is correct
8 Correct 2 ms 6236 KB Output is correct
9 Correct 2 ms 6236 KB Output is correct
10 Correct 2 ms 6236 KB Output is correct
11 Correct 2 ms 6236 KB Output is correct
12 Correct 2 ms 6236 KB Output is correct
13 Correct 2 ms 6236 KB Output is correct
14 Correct 3 ms 6236 KB Output is correct
15 Correct 1 ms 3928 KB Output is correct
16 Correct 2 ms 6236 KB Output is correct
17 Correct 2 ms 6236 KB Output is correct
18 Correct 2 ms 6236 KB Output is correct
19 Correct 2 ms 6236 KB Output is correct
20 Correct 2 ms 6236 KB Output is correct
21 Correct 2 ms 6236 KB Output is correct
22 Correct 2 ms 6236 KB Output is correct
23 Correct 2 ms 6236 KB Output is correct
24 Correct 2 ms 6236 KB Output is correct
25 Correct 3 ms 6240 KB Output is correct
26 Incorrect 9 ms 6236 KB Output isn't correct
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 53 ms 10328 KB Output is correct
2 Correct 33 ms 10436 KB Output is correct
3 Correct 29 ms 9308 KB Output is correct
4 Correct 6 ms 6744 KB Output is correct
5 Correct 5 ms 6744 KB Output is correct
6 Correct 10 ms 7260 KB Output is correct
7 Correct 2 ms 6236 KB Output is correct
8 Correct 22 ms 8148 KB Output is correct
9 Correct 23 ms 8792 KB Output is correct
10 Correct 2 ms 6232 KB Output is correct
11 Correct 32 ms 9296 KB Output is correct
12 Correct 37 ms 9812 KB Output is correct
13 Correct 3 ms 6232 KB Output is correct
14 Correct 2 ms 6232 KB Output is correct
15 Correct 2 ms 6232 KB Output is correct
16 Correct 2 ms 6236 KB Output is correct
17 Correct 2 ms 6236 KB Output is correct
18 Correct 3 ms 6236 KB Output is correct
19 Correct 2 ms 6236 KB Output is correct
20 Correct 3 ms 6236 KB Output is correct
21 Correct 2 ms 6236 KB Output is correct
22 Correct 2 ms 6236 KB Output is correct
23 Correct 2 ms 6236 KB Output is correct
24 Correct 2 ms 6236 KB Output is correct
25 Correct 2 ms 6236 KB Output is correct
26 Correct 2 ms 6236 KB Output is correct
27 Correct 3 ms 6236 KB Output is correct
28 Correct 1 ms 3928 KB Output is correct
29 Correct 2 ms 6236 KB Output is correct
30 Correct 2 ms 6236 KB Output is correct
31 Correct 2 ms 6236 KB Output is correct
32 Correct 2 ms 6236 KB Output is correct
33 Correct 2 ms 6236 KB Output is correct
34 Correct 2 ms 6236 KB Output is correct
35 Correct 2 ms 6236 KB Output is correct
36 Correct 2 ms 6236 KB Output is correct
37 Correct 2 ms 6236 KB Output is correct
38 Correct 3 ms 6240 KB Output is correct
39 Correct 36 ms 10568 KB Output is correct
40 Correct 37 ms 10788 KB Output is correct
41 Correct 24 ms 9692 KB Output is correct
42 Correct 7 ms 6748 KB Output is correct
43 Correct 2 ms 6236 KB Output is correct
44 Correct 2 ms 6232 KB Output is correct
45 Correct 2 ms 6232 KB Output is correct
46 Correct 2 ms 6236 KB Output is correct
47 Correct 2 ms 6308 KB Output is correct
48 Correct 2 ms 6236 KB Output is correct
49 Correct 2 ms 6236 KB Output is correct
50 Correct 2 ms 6236 KB Output is correct
51 Correct 2 ms 6236 KB Output is correct
52 Correct 2 ms 6232 KB Output is correct
53 Correct 3 ms 6236 KB Output is correct
54 Correct 2 ms 6236 KB Output is correct
55 Correct 2 ms 6236 KB Output is correct
56 Correct 3 ms 6236 KB Output is correct
57 Correct 55 ms 11156 KB Output is correct
58 Correct 53 ms 11024 KB Output is correct
59 Correct 62 ms 11052 KB Output is correct
60 Correct 49 ms 10984 KB Output is correct
61 Correct 58 ms 11096 KB Output is correct
62 Correct 54 ms 11088 KB Output is correct
63 Correct 54 ms 10956 KB Output is correct
64 Correct 50 ms 10724 KB Output is correct
65 Correct 62 ms 11192 KB Output is correct
66 Correct 64 ms 11268 KB Output is correct
67 Correct 58 ms 11108 KB Output is correct
68 Correct 47 ms 10832 KB Output is correct
69 Correct 64 ms 10960 KB Output is correct
70 Correct 52 ms 11008 KB Output is correct
71 Correct 1 ms 3672 KB Output is correct
72 Correct 3 ms 6236 KB Output is correct
73 Correct 4 ms 6236 KB Output is correct
74 Correct 4 ms 6236 KB Output is correct
75 Correct 3 ms 6236 KB Output is correct
76 Correct 3 ms 6236 KB Output is correct
77 Correct 3 ms 6236 KB Output is correct
78 Correct 3 ms 6236 KB Output is correct
79 Correct 3 ms 6232 KB Output is correct
80 Correct 68 ms 11088 KB Output is correct
81 Correct 58 ms 11092 KB Output is correct
82 Correct 52 ms 10832 KB Output is correct
83 Correct 53 ms 10832 KB Output is correct
84 Correct 2 ms 6236 KB Output is correct
85 Correct 4 ms 6312 KB Output is correct
86 Correct 3 ms 6236 KB Output is correct
87 Correct 3 ms 6236 KB Output is correct
88 Correct 4 ms 6236 KB Output is correct
89 Correct 3 ms 6236 KB Output is correct
90 Correct 3 ms 6316 KB Output is correct
91 Correct 3 ms 6236 KB Output is correct
92 Correct 3 ms 6372 KB Output is correct
93 Correct 3 ms 6488 KB Output is correct
94 Correct 2 ms 6236 KB Output is correct
95 Correct 2 ms 6236 KB Output is correct
96 Correct 3 ms 6236 KB Output is correct
97 Correct 2 ms 6232 KB Output is correct
98 Correct 2 ms 6236 KB Output is correct
99 Correct 2 ms 6236 KB Output is correct
100 Correct 2 ms 6240 KB Output is correct
101 Correct 2 ms 6212 KB Output is correct
102 Correct 2 ms 6232 KB Output is correct
103 Correct 2 ms 6236 KB Output is correct
104 Correct 5 ms 6748 KB Output is correct
105 Correct 10 ms 7260 KB Output is correct
106 Correct 2 ms 6236 KB Output is correct
107 Correct 18 ms 8028 KB Output is correct
108 Correct 24 ms 8864 KB Output is correct
109 Correct 2 ms 6236 KB Output is correct
110 Correct 32 ms 9436 KB Output is correct
111 Correct 38 ms 10072 KB Output is correct
112 Correct 2 ms 6236 KB Output is correct
113 Correct 52 ms 11604 KB Output is correct
114 Execution timed out 1075 ms 8540 KB Time limit exceeded
115 Halted 0 ms 0 KB -