# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1102642 | 12345678 | Robot (JOI21_ho_t4) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int nx=1e5+5;
ll n, m, u, v, c, p, mn=1e18, cnt;
priority_queue<tuple<ll, ll, ll, ll>, vector<tuple<ll, ll, ll, ll>>, greater<tuple<ll, ll, ll, ll>>> pq;
vector<pair<ll, ll>> d[nx];
map<ll, ll> mp[nx], dp[nx][2];
ll getval(ll idx, ll c, ll t)
{
if (dp[idx][t].find(c)==dp[idx][t].end()) return 1e18;
return dp[idx][t][c];
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>m;
for (int i=1; i<=m; i++) cin>>u>>v>>c>>p, d[u].push_back({v, c}), d[v].push_back({u, c}), mp[u][c]++, mp[v][c]++;
dp[1][0][-1]=0;
pq.push({0, 1, -1, 0});
while (!pq.empty())
{
auto [cst, u, p, t]=pq.top();
pq.pop();
if (u==n) return cout<<cst, void();
for (auto [v, c]:d[u])
{
if (mp[u][c]>2||((mp[u][c]==2)&&p!=c))
{
if (cst+1<getval(v, c, 1)) dp[v][1][c]=cst+1, pq.push({cst+1, v, c, 1});
}
else if (mp[u][c]==2&&p==c)
{
if (t==1)
{
if (cst<getval(v, c, 0)) dp[v][0][c]=cst, pq.push({cst, v, c, 0});
}
else
{
if (cst+1<getval(v, c, 1)) dp[v][1][c]=cst+1, pq.push({cst+1, v, c, 1});
}
}
else
{
if (cst<getval(v, c, 0)) dp[v][0][c]=cst, pq.push({cst, v, c, 0});
}
}
}
cout<<-1;
}