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 fileio() freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define fio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define allr(x) x.rbegin(), x.rend()
#define cmprs(x) sort(all(x)),x.erase(unique(all(x)),x.end())
#define endl "\n"
#define sp " "
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define rz resize
#define sz(a) (int)(a.size())
#define clr(a) a.clear()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef tuple<int, int, int> tpi;
typedef tuple<ll, ll, ll> tpl;
typedef pair<double, ll> pdl;
typedef pair<double, int> pdi;
const int dx[] = {1,-1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
const ll MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int MAX = 101010; // PLZ CHK!
typedef array<ll,3> arr;
int N,M;
ll D[MAX];
map<int,vector<arr>> G[MAX];
map<int,ll> D2[MAX],C[MAX];
int main() {
fio();
cin>>N>>M;
for (int i=0; i<M; i++) {
int a,b,c,p;
cin>>a>>b>>c>>p;
G[a][c].pb({b,c,p});
G[b][c].pb({a,c,p});
C[a][c]+=p, C[b][c]+=p;
}
fill(D,D+MAX,LINF);
D[1]=0;
priority_queue<arr,vector<arr>,greater<arr>> pq;
pq.push({D[1],1,0});
while (!pq.empty()) {
auto [dst,cur,col]=pq.top(); pq.pop();
if (col) {
if (dst!=D2[cur][col]) continue;
for (auto [nxt,clr,prc]:G[cur][col]) {
ll cst=C[cur][col]-prc;
if (D[nxt]>D2[cur][col]+cst) {
D[nxt]=D2[cur][col]+cst;
pq.push({D[nxt],nxt,0});
}
}
}
else {
if (dst!=D[cur]) continue;
for (auto [tt,vtx]:G[cur]) {
for (auto [nxt,clr,prc]:vtx) {
ll cst1=C[cur][clr]-prc;
if (D[nxt]>D[cur]+cst1) {
D[nxt]=D[cur]+cst1;
pq.push({D[nxt],nxt,0});
}
ll cst2=prc;
if (D[nxt]>D[cur]+cst2) {
D[nxt]=D[cur]+cst2;
pq.push({D[nxt],nxt,0});
}
if (D2[nxt].find(clr)==D2[nxt].end() || D2[nxt][clr]>D[cur]) {
D2[nxt][clr]=D[cur];
pq.push({D2[nxt][clr],nxt,clr});
}
}
}
}
}
cout<<(D[N]==LINF?-1:D[N]);
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... |