This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3,unroll-loops,Ofast")
//#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
#define MAX 200005
#define pb push_back
#define mp make_pair
#define int long long
#define f first
#define s second
#define vi vector<int>
#define pii pair<int,int>
#define si set<int>
#define vpii vector<pair<int,int>>
const int mod = 1e9+7;
const int INF = 1e18;
// myMap.begin()->first : key
// myMap.begin()->second : value
int epow(int a,int b){int ans=1;while(b){if(b&1) ans*=a;a*=a;b>>=1;ans%=mod;a%=mod;}return ans%mod;}
int gcd(int a,int b) {if(a<b)swap(a,b);while(b){int tmp=b;b=a%b;a=tmp;}return a;}
int mul(long long a,long long b){return ((a%mod)*(b%mod))%mod;}
int sum(long long a,long long b){return ((a%mod)+(b%mod))%mod;}
// typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
// typedef
// tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_multiset;
void solve(){
int n,m; cin>>n>>m;
vector<array<int,3>> adj[n+5];
for(int i=1;i<=m;i++){
int a,b,c,d; cin>>a>>b>>c>>d;
adj[a].pb({b,c,d}); adj[b].pb({a,c,d});
}
vector<vi> dp(n+2,vi(m+2,INF)); dp[1][0]=0;
priority_queue<array<int,4>,vector<array<int,4>>,greater<array<int,4>>> pq; pq.push({0,1,0,0});//dist,node,newcol,par
while(pq.size()){
auto node = pq.top(); pq.pop();
vi cnt(m+2),tot(m+2);
for(auto u : adj[node[1]]){
if(u[0]!=node[3]) cnt[u[1]]++,tot[u[1]]+=u[2];
else if(node[3]) cnt[node[2]]++,tot[node[2]]+=u[2];
}
// if(node[3]) cnt[node[2]]++;
// cout<<node[0]<<" "<<node[1]<<" "<<node[2]<<" "<<node[3]<<endl;
for(auto u : adj[node[1]]){
if(u[0]==node[3]) continue; // nodeu degisitirp gidebilir!!
for(int i=1;i<=m;i++){
int c=INF;
if(u[1]==i){
if(cnt[i]==0) c=u[2];
else c=tot[i]-u[2];
// c=tot[i]-u[2];
}
else{
if(cnt[i]==0) c=u[2];
else c=tot[i]+u[2];
// c=tot[i]+u[2];
}
if(dp[u[0]][i] > node[0]+c ){
dp[u[0]][i] = node[0]+c;
pq.push({dp[u[0]][i],u[0],i,node[1]});
}
}
}
}
int ans=INF;
for(int i=1;i<=m;i++) ans=min(ans,dp[n][i]);
if(ans==INF) ans=-1;
cout<<ans<<endl;
// cout<<dp[2][1]<<endl;
return;
}
int32_t main(/*int32_t argc, char* argv[]*/){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("dining.in", "r", stdin);
// freopen("dining.out", "w", stdout);
int t=1;
// cin >> t;
while (t--) solve();
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... |