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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long int lld;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define trav(a,v) for(auto a:v)
lld inp[1000000][4];
const lld INF=1e18;
void solve(){
int n,m;
cin>>n>>m;
map<pair<int,int>,int> M;
rep(i,0,m){
rep(j,0,4)cin>>inp[i][j];
inp[i][0]--;
inp[i][1]--;
M[{inp[i][0],inp[i][2]}]+=inp[i][3];
M[{inp[i][1],inp[i][2]}]+=inp[i][3];
}
vector<vector<pair<int,lld> > >nei(n);
rep(i,0,m){
nei[inp[i][0]].push_back({inp[i][1],min(inp[i][3],M[{inp[i][0],inp[i][2]}]-inp[i][3])});
nei[inp[i][1]].push_back({inp[i][0],min(inp[i][3],M[{inp[i][1],inp[i][2]}]-inp[i][3])});
}
vector<lld> dist(n,INF);
dist[0]=0;
priority_queue<pair<lld,int> >pq;
pq.push({0,0});
while(!pq.empty()){
lld d=-pq.top().first;
int v=pq.top().second;
pq.pop();
if(d>dist[v])continue;
trav(u,nei[v]){
if(dist[u.first]>u.second+d){
dist[u.first]=u.second+d;
pq.push({-dist[u.first],u.first});
}
}
}
if(dist[n-1]<INF)cout<<dist[n-1]<<"\n";
else cout<<"-1\n";
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int tt=1;
//cin>>tt;
while(tt--){
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |