# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
380280 |
2021-03-20T20:23:14 Z |
peijar |
Robot (JOI21_ho_t4) |
C++17 |
|
3000 ms |
50892 KB |
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1e18;
signed main(void)
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int nbSommets, nbAretes;
cin >> nbSommets >> nbAretes;
vector<vector<tuple<int, int, int>>> adj(nbSommets+1);
vector<vector<int>> couleursAdjacentes(nbSommets+1);
vector<vector<int>> totCostCol(nbSommets+1);
for (int iArete = 0; iArete < nbAretes; ++iArete)
{
int iFrom, iTo, col, cost;
cin >> iFrom >> iTo >> col >> cost;
adj[iFrom].emplace_back(iTo, col, cost);
adj[iTo].emplace_back(iFrom, col, cost);
couleursAdjacentes[iFrom].push_back(col);
couleursAdjacentes[iTo].push_back(col);
}
auto getIdCol = [&](int iSommet, int iCol)
{
return lower_bound(couleursAdjacentes[iSommet].begin(), couleursAdjacentes[iSommet].end(), iCol) - couleursAdjacentes[iSommet].begin();
};
for (int iSommets = 1; iSommets <= nbSommets; ++iSommets)
{
sort(couleursAdjacentes[iSommets].begin(), couleursAdjacentes[iSommets].end());
couleursAdjacentes[iSommets].resize(unique(couleursAdjacentes[iSommets].begin(), couleursAdjacentes[iSommets].end())-couleursAdjacentes[iSommets].begin());
totCostCol[iSommets].resize(couleursAdjacentes[iSommets].size());
for (auto [to, col, cost] : adj[iSommets])
totCostCol[iSommets][getIdCol(iSommets, col)] += cost;
sort(adj[iSommets].begin(), adj[iSommets].end());
}
vector<vector<int>> dis(nbSommets+1);
for (int iSommets(1); iSommets <= nbSommets; ++iSommets)
dis[iSommets].assign(1 + adj[iSommets].size(), INF);
auto getIdAdj = [&](int iSommet, int iFrere)
{
return 1 + (lower_bound(adj[iFrere].begin(), adj[iFrere].end(), make_tuple(iSommet, 0, 0)) - adj[iFrere].begin());
};
dis[1][0] = 0;
priority_queue<tuple<int,int, int>> pq;
pq.emplace(0, 1, 0);
while (!pq.empty())
{
auto [curCost, iSommet, iFrom] = pq.top(); pq.pop();
curCost = -curCost;
if (dis[iSommet][iFrom] > curCost) continue;
iFrom--;
for (auto [iFrere, iCol, coutChange] : adj[iSommet])
{
// Soit on change la couleur :
int idFrere = getIdAdj(iSommet, iFrere);
assert(get<0>(adj[iFrere][idFrere-1]) == iSommet);
if (dis[iFrere][idFrere] > curCost + coutChange)
{
dis[iFrere][idFrere] = curCost + coutChange;
pq.emplace(-dis[iFrere][idFrere], iFrere, idFrere);
}
// Soit on change toutes les autres :
int nxtCost = curCost + totCostCol[iSommet][getIdCol(iSommet, iCol)] - coutChange;
if (iFrom != -1 and get<1>(adj[iSommet][iFrom]) == iCol)
nxtCost -= get<2>(adj[iSommet][iFrom]);
if (dis[iFrere][0] > nxtCost)
{
dis[iFrere][0] = nxtCost;
pq.emplace(-nxtCost, iFrere, 0);
}
}
}
int sol = INF;
for (auto v : dis[nbSommets])
sol = min(sol, v);
if (sol == INF) sol = -1;
cout << sol << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
5 ms |
492 KB |
Output is correct |
8 |
Correct |
2 ms |
492 KB |
Output is correct |
9 |
Correct |
82 ms |
876 KB |
Output is correct |
10 |
Correct |
48 ms |
876 KB |
Output is correct |
11 |
Correct |
32 ms |
956 KB |
Output is correct |
12 |
Correct |
16 ms |
1004 KB |
Output is correct |
13 |
Correct |
31 ms |
1004 KB |
Output is correct |
14 |
Correct |
30 ms |
1004 KB |
Output is correct |
15 |
Correct |
2 ms |
620 KB |
Output is correct |
16 |
Correct |
4 ms |
748 KB |
Output is correct |
17 |
Correct |
3 ms |
748 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
16 ms |
748 KB |
Output is correct |
20 |
Correct |
2 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
296 ms |
17628 KB |
Output is correct |
2 |
Correct |
103 ms |
8804 KB |
Output is correct |
3 |
Correct |
752 ms |
26208 KB |
Output is correct |
4 |
Correct |
181 ms |
13920 KB |
Output is correct |
5 |
Execution timed out |
3070 ms |
50892 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
5 ms |
492 KB |
Output is correct |
8 |
Correct |
2 ms |
492 KB |
Output is correct |
9 |
Correct |
82 ms |
876 KB |
Output is correct |
10 |
Correct |
48 ms |
876 KB |
Output is correct |
11 |
Correct |
32 ms |
956 KB |
Output is correct |
12 |
Correct |
16 ms |
1004 KB |
Output is correct |
13 |
Correct |
31 ms |
1004 KB |
Output is correct |
14 |
Correct |
30 ms |
1004 KB |
Output is correct |
15 |
Correct |
2 ms |
620 KB |
Output is correct |
16 |
Correct |
4 ms |
748 KB |
Output is correct |
17 |
Correct |
3 ms |
748 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
16 ms |
748 KB |
Output is correct |
20 |
Correct |
2 ms |
620 KB |
Output is correct |
21 |
Correct |
296 ms |
17628 KB |
Output is correct |
22 |
Correct |
103 ms |
8804 KB |
Output is correct |
23 |
Correct |
752 ms |
26208 KB |
Output is correct |
24 |
Correct |
181 ms |
13920 KB |
Output is correct |
25 |
Execution timed out |
3070 ms |
50892 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |