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 int long long
#define arr array<int, 4>
vector<vector<arr>> graph;
vector<int> fc;
vector<int> d1;
vector<int> d2;
int32_t main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int n, m;
cin >> n >> m;
graph.assign(n, vector<arr>());
fc.assign(n, -1);
d1.assign(n, 1e18);
d2.assign(n, 1e18);
for (int j = 0; j < m; j++)
{
int a, b, c, d;
cin >> a >> b >> c >> d;
a--; b--;
graph[a].push_back({b, c, d, 0});
}
priority_queue<arr, vector<arr>, greater<arr>> pq;
pq.push({0, 0, 0, 0});
pq.push({0,1,0, 0});
while (pq.size())
{
arr a = pq.top();
pq.pop();
int i = a[2];
int c = a[1];
int d = a[0];
int p = a[3];
if (fc[i]==c) continue;
if (fc[i]==-1)
{
fc[i] = c;
d1[i] = d;
}
else
{
if (d2[i]<=d) continue;
d2[i] = d;
}
map<int, int> cost;
for (arr ele:graph[i])
{
if (ele[0]==p) cost[c] +=ele[2];
else cost[ele[1]] += ele[2];
}
int ava1 = -1;
int ava2 = -1;
int avac1 = 0;
int avac2 = 0;
for (int j = 0; j < m; j++)
{
if (cost[j]==0)
{
if (ava1==-1) ava1 = j;
else ava2=j;
if (ava2!=-1) break;
}
}
if (ava1==-1) avac1=1e18;
if (ava2==-1) avac2=1e18;
for (int j = 0; j < m; j++)
{
if (cost[j]<avac1)
{
ava2 = ava1;
avac2 = avac1;
avac1=cost[j];
ava1=j;
}
else if (cost[j]<avac2)
{
ava2 = j;
avac2=cost[j];
}
}
for (arr ele:graph[i])
{
if (ele[0]==p) continue;
pq.push({d+cost[ele[1]]-ele[2], ele[1], ele[0], i});
pq.push({d+ele[2]+avac1, ava1, ele[0], i});
pq.push({d+ele[2]+avac2, ava2, ele[0], i});
}
}
if (d1[n-1]==1e18) cout << -1;
else cout << d1[n-1];
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |