Submission #1111175

# Submission time Handle Problem Language Result Execution time Memory
1111175 2024-11-11T15:44:43 Z cpptowin Relay Marathon (NOI20_relaymarathon) C++17
100 / 100
1481 ms 233468 KB
#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf (int)1e10
#define bitcount(x) __builtin_popcountll(x)
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define ss(x) (int)x.size()
#define UNIQUE(v) v.erase(unique(all(v)),v.end())
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
void add(int &x, int k)
{
    x += k;
    x %= mod;
    if(x < 0) x += mod;
}
void del(int &x, int k)
{
    x -= k;
    x %= mod;
    if(x < 0) x += mod;
}
int n,m,k;
vii ke[maxn];
vi special;
int d[maxn];
int sources[maxn];
pii dist[maxn];
int mindist;
pii node;
void dijkstra(vi inp)
{
    mindist = inf;
    fo(i,1,n) d[i] = inf;
    priority_queue<pii,vii,greater<pii>> q;
    for(int it : inp)
    {
        d[it] = 0;
        sources[it] = it;
        q.push({0,it});
    }
    while(ss(q))
    {
        auto [du,u] = q.top();
        q.pop();
        if(du != d[u]) continue;
        for(auto [v,w] : ke[u])
        {
            if(minimize(d[v],du + w))
            {
                q.push({d[v],v});
                sources[v] = sources[u];
            }
        }
    }
    fo(u,1,n) for(auto [v,w] : ke[u]) if(sources[u] != sources[v])
        {
            if(minimize(mindist,d[u] + w + d[v])) node = {sources[u],sources[v]};
        }
}
bool ok[maxn];
pii dij(int inp)
{
    pii minn;
    minn.fi = inf;
    fo(i,1,n) d[i] = inf;
    d[inp] = 0;
    priority_queue<pii,vii,greater<pii>> q;
    q.push({0,inp});
    while(q.size())
    {
        auto [du,u] = q.top();
        q.pop();
        if(d[u] != du) continue;
        for(auto [v,w] : ke[u])
        {
            if(d[v] > d[u] + w)
            {
                d[v] = d[u] + w;
                q.push({d[v],v});
            }
        }
    }
    for(int it : special) if(ok[it])
        {
            if(minimize(minn.fi,d[it])) minn.se = it;
        }
    return minn;
}
main()
{
#define name "TASK"
    if (fopen(name ".inp", "r"))
    {
        freopen(name ".inp", "r", stdin);
        freopen(name ".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> m >> k;
    special.resize(k);
    fo(i,1,m)
    {
        int u,v,w;
        cin >> u >> v >> w;
        ke[u].pb(v,w);
        ke[v].pb(u,w);
    }
    int res = inf;
    for(int &it : special) cin >> it,ok[it] = 1;
    dijkstra(special);
    int dd = mindist;
    vi cc;
    pii xx = node;
    for(int it : special) if(it != node.fi and it != node.se) cc.pb(it);
    dijkstra(cc);
    minimize(res,mindist + dd);
    node = xx;
    ok[node.fi] = ok[node.se] = 0;
    pii d1 = dij(node.fi);
    ok[d1.se] = 0;
    minimize(res,dij(node.se).fi + d1.fi);
    ok[d1.se] = 1;
    swap(node.fi,node.se);
    d1 = dij(node.fi);
    ok[d1.se] = 0;
    minimize(res,dij(node.se).fi + d1.fi);
    cout << res;
}

Compilation message

RelayMarathon.cpp:125:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  125 | main()
      | ^~~~
RelayMarathon.cpp: In function 'int main()':
RelayMarathon.cpp:130:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  130 |         freopen(name ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
RelayMarathon.cpp:131:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  131 |         freopen(name ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 31312 KB Output is correct
2 Correct 6 ms 31312 KB Output is correct
3 Correct 6 ms 27216 KB Output is correct
4 Correct 6 ms 27216 KB Output is correct
5 Correct 6 ms 27216 KB Output is correct
6 Correct 6 ms 25304 KB Output is correct
7 Correct 6 ms 27216 KB Output is correct
8 Correct 6 ms 27216 KB Output is correct
9 Correct 6 ms 31312 KB Output is correct
10 Correct 5 ms 29264 KB Output is correct
11 Correct 6 ms 29264 KB Output is correct
12 Correct 5 ms 25312 KB Output is correct
13 Correct 7 ms 25168 KB Output is correct
14 Correct 11 ms 25168 KB Output is correct
15 Correct 7 ms 25344 KB Output is correct
16 Correct 12 ms 25168 KB Output is correct
17 Correct 10 ms 25168 KB Output is correct
18 Correct 11 ms 27216 KB Output is correct
19 Correct 10 ms 25168 KB Output is correct
20 Correct 8 ms 25168 KB Output is correct
21 Correct 13 ms 25336 KB Output is correct
22 Correct 12 ms 25168 KB Output is correct
23 Correct 10 ms 25280 KB Output is correct
24 Correct 12 ms 25168 KB Output is correct
25 Correct 10 ms 25168 KB Output is correct
26 Correct 7 ms 25168 KB Output is correct
27 Correct 8 ms 25288 KB Output is correct
28 Correct 9 ms 25168 KB Output is correct
29 Correct 9 ms 25168 KB Output is correct
30 Correct 11 ms 25380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 31312 KB Output is correct
2 Correct 6 ms 31312 KB Output is correct
3 Correct 6 ms 27216 KB Output is correct
4 Correct 6 ms 27216 KB Output is correct
5 Correct 6 ms 27216 KB Output is correct
6 Correct 6 ms 25304 KB Output is correct
7 Correct 6 ms 27216 KB Output is correct
8 Correct 6 ms 27216 KB Output is correct
9 Correct 6 ms 31312 KB Output is correct
10 Correct 5 ms 29264 KB Output is correct
11 Correct 6 ms 29264 KB Output is correct
12 Correct 5 ms 25312 KB Output is correct
13 Correct 7 ms 25168 KB Output is correct
14 Correct 11 ms 25168 KB Output is correct
15 Correct 7 ms 25344 KB Output is correct
16 Correct 12 ms 25168 KB Output is correct
17 Correct 10 ms 25168 KB Output is correct
18 Correct 11 ms 27216 KB Output is correct
19 Correct 10 ms 25168 KB Output is correct
20 Correct 8 ms 25168 KB Output is correct
21 Correct 13 ms 25336 KB Output is correct
22 Correct 12 ms 25168 KB Output is correct
23 Correct 10 ms 25280 KB Output is correct
24 Correct 12 ms 25168 KB Output is correct
25 Correct 10 ms 25168 KB Output is correct
26 Correct 7 ms 25168 KB Output is correct
27 Correct 8 ms 25288 KB Output is correct
28 Correct 9 ms 25168 KB Output is correct
29 Correct 9 ms 25168 KB Output is correct
30 Correct 11 ms 25380 KB Output is correct
31 Correct 11 ms 25180 KB Output is correct
32 Correct 12 ms 25168 KB Output is correct
33 Correct 11 ms 25168 KB Output is correct
34 Correct 13 ms 25512 KB Output is correct
35 Correct 7 ms 25168 KB Output is correct
36 Correct 9 ms 25548 KB Output is correct
37 Correct 10 ms 25680 KB Output is correct
38 Correct 7 ms 25168 KB Output is correct
39 Correct 25 ms 30544 KB Output is correct
40 Correct 12 ms 26192 KB Output is correct
41 Correct 10 ms 25168 KB Output is correct
42 Correct 14 ms 26272 KB Output is correct
43 Correct 10 ms 25424 KB Output is correct
44 Correct 8 ms 25168 KB Output is correct
45 Correct 11 ms 25168 KB Output is correct
46 Correct 30 ms 30528 KB Output is correct
47 Correct 14 ms 25936 KB Output is correct
48 Correct 24 ms 30552 KB Output is correct
49 Correct 25 ms 32848 KB Output is correct
50 Correct 10 ms 25168 KB Output is correct
51 Correct 11 ms 25288 KB Output is correct
52 Correct 12 ms 25336 KB Output is correct
53 Correct 18 ms 28040 KB Output is correct
54 Correct 26 ms 30544 KB Output is correct
55 Correct 10 ms 25168 KB Output is correct
56 Correct 7 ms 25300 KB Output is correct
57 Correct 9 ms 25168 KB Output is correct
58 Correct 28 ms 31048 KB Output is correct
59 Correct 8 ms 25180 KB Output is correct
60 Correct 11 ms 25424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 31560 KB Output is correct
2 Correct 18 ms 26704 KB Output is correct
3 Correct 1012 ms 174724 KB Output is correct
4 Correct 475 ms 94520 KB Output is correct
5 Correct 111 ms 39160 KB Output is correct
6 Correct 98 ms 36324 KB Output is correct
7 Correct 137 ms 40492 KB Output is correct
8 Correct 56 ms 31724 KB Output is correct
9 Correct 96 ms 35376 KB Output is correct
10 Correct 69 ms 33344 KB Output is correct
11 Correct 1141 ms 172988 KB Output is correct
12 Correct 68 ms 33740 KB Output is correct
13 Correct 306 ms 71356 KB Output is correct
14 Correct 158 ms 43556 KB Output is correct
15 Correct 1039 ms 177460 KB Output is correct
16 Correct 36 ms 32080 KB Output is correct
17 Correct 724 ms 136260 KB Output is correct
18 Correct 8 ms 26960 KB Output is correct
19 Correct 1050 ms 188356 KB Output is correct
20 Correct 119 ms 39788 KB Output is correct
21 Correct 103 ms 39472 KB Output is correct
22 Correct 61 ms 32844 KB Output is correct
23 Correct 27 ms 29776 KB Output is correct
24 Correct 745 ms 148956 KB Output is correct
25 Correct 108 ms 37660 KB Output is correct
26 Correct 51 ms 31468 KB Output is correct
27 Correct 94 ms 33968 KB Output is correct
28 Correct 19 ms 28240 KB Output is correct
29 Correct 148 ms 40108 KB Output is correct
30 Correct 242 ms 56156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 31312 KB Output is correct
2 Correct 6 ms 31312 KB Output is correct
3 Correct 6 ms 27216 KB Output is correct
4 Correct 6 ms 27216 KB Output is correct
5 Correct 6 ms 27216 KB Output is correct
6 Correct 6 ms 25304 KB Output is correct
7 Correct 6 ms 27216 KB Output is correct
8 Correct 6 ms 27216 KB Output is correct
9 Correct 6 ms 31312 KB Output is correct
10 Correct 5 ms 29264 KB Output is correct
11 Correct 6 ms 29264 KB Output is correct
12 Correct 5 ms 25312 KB Output is correct
13 Correct 7 ms 25168 KB Output is correct
14 Correct 11 ms 25168 KB Output is correct
15 Correct 7 ms 25344 KB Output is correct
16 Correct 12 ms 25168 KB Output is correct
17 Correct 10 ms 25168 KB Output is correct
18 Correct 11 ms 27216 KB Output is correct
19 Correct 10 ms 25168 KB Output is correct
20 Correct 8 ms 25168 KB Output is correct
21 Correct 13 ms 25336 KB Output is correct
22 Correct 12 ms 25168 KB Output is correct
23 Correct 10 ms 25280 KB Output is correct
24 Correct 12 ms 25168 KB Output is correct
25 Correct 10 ms 25168 KB Output is correct
26 Correct 7 ms 25168 KB Output is correct
27 Correct 8 ms 25288 KB Output is correct
28 Correct 9 ms 25168 KB Output is correct
29 Correct 9 ms 25168 KB Output is correct
30 Correct 11 ms 25380 KB Output is correct
31 Correct 11 ms 25180 KB Output is correct
32 Correct 12 ms 25168 KB Output is correct
33 Correct 11 ms 25168 KB Output is correct
34 Correct 13 ms 25512 KB Output is correct
35 Correct 7 ms 25168 KB Output is correct
36 Correct 9 ms 25548 KB Output is correct
37 Correct 10 ms 25680 KB Output is correct
38 Correct 7 ms 25168 KB Output is correct
39 Correct 25 ms 30544 KB Output is correct
40 Correct 12 ms 26192 KB Output is correct
41 Correct 10 ms 25168 KB Output is correct
42 Correct 14 ms 26272 KB Output is correct
43 Correct 10 ms 25424 KB Output is correct
44 Correct 8 ms 25168 KB Output is correct
45 Correct 11 ms 25168 KB Output is correct
46 Correct 30 ms 30528 KB Output is correct
47 Correct 14 ms 25936 KB Output is correct
48 Correct 24 ms 30552 KB Output is correct
49 Correct 25 ms 32848 KB Output is correct
50 Correct 10 ms 25168 KB Output is correct
51 Correct 11 ms 25288 KB Output is correct
52 Correct 12 ms 25336 KB Output is correct
53 Correct 18 ms 28040 KB Output is correct
54 Correct 26 ms 30544 KB Output is correct
55 Correct 10 ms 25168 KB Output is correct
56 Correct 7 ms 25300 KB Output is correct
57 Correct 9 ms 25168 KB Output is correct
58 Correct 28 ms 31048 KB Output is correct
59 Correct 8 ms 25180 KB Output is correct
60 Correct 11 ms 25424 KB Output is correct
61 Correct 54 ms 31560 KB Output is correct
62 Correct 18 ms 26704 KB Output is correct
63 Correct 1012 ms 174724 KB Output is correct
64 Correct 475 ms 94520 KB Output is correct
65 Correct 111 ms 39160 KB Output is correct
66 Correct 98 ms 36324 KB Output is correct
67 Correct 137 ms 40492 KB Output is correct
68 Correct 56 ms 31724 KB Output is correct
69 Correct 96 ms 35376 KB Output is correct
70 Correct 69 ms 33344 KB Output is correct
71 Correct 1141 ms 172988 KB Output is correct
72 Correct 68 ms 33740 KB Output is correct
73 Correct 306 ms 71356 KB Output is correct
74 Correct 158 ms 43556 KB Output is correct
75 Correct 1039 ms 177460 KB Output is correct
76 Correct 36 ms 32080 KB Output is correct
77 Correct 724 ms 136260 KB Output is correct
78 Correct 8 ms 26960 KB Output is correct
79 Correct 1050 ms 188356 KB Output is correct
80 Correct 119 ms 39788 KB Output is correct
81 Correct 103 ms 39472 KB Output is correct
82 Correct 61 ms 32844 KB Output is correct
83 Correct 27 ms 29776 KB Output is correct
84 Correct 745 ms 148956 KB Output is correct
85 Correct 108 ms 37660 KB Output is correct
86 Correct 51 ms 31468 KB Output is correct
87 Correct 94 ms 33968 KB Output is correct
88 Correct 19 ms 28240 KB Output is correct
89 Correct 148 ms 40108 KB Output is correct
90 Correct 242 ms 56156 KB Output is correct
91 Correct 137 ms 33636 KB Output is correct
92 Correct 1399 ms 213128 KB Output is correct
93 Correct 236 ms 41996 KB Output is correct
94 Correct 1046 ms 136172 KB Output is correct
95 Correct 9 ms 34128 KB Output is correct
96 Correct 10 ms 30800 KB Output is correct
97 Correct 281 ms 51528 KB Output is correct
98 Correct 142 ms 35288 KB Output is correct
99 Correct 85 ms 42992 KB Output is correct
100 Correct 1376 ms 233468 KB Output is correct
101 Correct 679 ms 104648 KB Output is correct
102 Correct 665 ms 103960 KB Output is correct
103 Correct 1124 ms 170408 KB Output is correct
104 Correct 1292 ms 187180 KB Output is correct
105 Correct 109 ms 34312 KB Output is correct
106 Correct 1184 ms 167984 KB Output is correct
107 Correct 481 ms 62588 KB Output is correct
108 Correct 220 ms 40212 KB Output is correct
109 Correct 18 ms 28752 KB Output is correct
110 Correct 62 ms 31568 KB Output is correct
111 Correct 207 ms 40736 KB Output is correct
112 Correct 1469 ms 229552 KB Output is correct
113 Correct 1402 ms 233252 KB Output is correct
114 Correct 223 ms 43688 KB Output is correct
115 Correct 636 ms 95488 KB Output is correct
116 Correct 1481 ms 209116 KB Output is correct
117 Correct 1122 ms 167800 KB Output is correct
118 Correct 1048 ms 147000 KB Output is correct
119 Correct 1305 ms 190812 KB Output is correct
120 Correct 865 ms 120556 KB Output is correct