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>
#define ll long long
#define int ll
#define pii pair<int,int>
#define tiii tuple<int,int,int>
#define tiiii tuple<int,int,bool,int>
using namespace std;
int INF = numeric_limits<ll>::max()/2;
int32_t main(){
int n, m;
cin >> n >> m;
vector<vector<tiii>> adj(n);
vector<int> colorAmt(m);
for(int i = 0; i < m; i++){
int a, b, c, v;
cin >> a >> b >> c >> v;
c--;
a--;b--;
adj[a].push_back({b,c,v});
adj[b].push_back({a,c,v});
colorAmt[c]++;
}
// Dijkstra
// We use the following Idea: We can either change the current edge or all other edges. If we change all other edges, we need to keep track
// of which ones we changed
vector<bitset<100000>> changed(n);
vector<int> dist(n, INF);
// distance | vertex | color | cost | precessor
priority_queue<tiiii, vector<tiiii>, greater<tiiii>> pq;
pq.push({0,0,0,0});
while(pq.size()){
int d, v, c , pre;
tie(d,v,c, pre) = pq.top(); pq.pop();
if(d >= dist[v]) continue;
dist[v] = d;
changed[v] = changed[pre];
if(c) changed[v][pre] = c;
for(auto p : adj[v]){
int s = 0;
// get the sum of taking all other edges
for(auto e : adj[get<0>(p)]){
int u, co, we;
tie(u,co,we) = e;
if(co != get<1>(e) && get<0>(e) != get<0>(p)) continue;
s += we;
}
// only taking one edge
if(get<2>(p) < s){
pq.push({get<2>(p)+d, get<0>(p), 0, v});
}else{
pq.push({s+d, get<0>(p), 1, v});
}
}
// either change the color of this edge or all others
}
if(dist[n-1]>=INF) dist[n-1] = -1;
cout << dist[n-1] << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |