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>
//code
#define fl(i,x,y,z) for(int i=x;i<=y;i=i+z)
#define fn(i,x,y,z) for(int i=x;i>=y;i=i-z)
#define rep(i,x,y) for(int i=x;i<y;i=i+1)
#define all(v) v.begin(),v.end()
#define pb push_back
#define tle cout<<"tle"<<endl
#define edl cout<<"\n"
#define el "\n"
#define getbit(x,i) ((x>>i)&1)
#define bitcnt __builtin_popcount
//ham
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
#define ld long double
#define inf 0x3f3f3f3f
//#define int long long
using namespace std;
const ll mod=1e9+7;
int n,m;
vector<pii> a[100005];
vector<pair <int,pii>> g[100005];
int d[100005];
int t[100005];
bool vis[100005];
priority_queue< pair < int,int > , vector < pair < int ,int > > , greater < pair < int , int > > > q;
void dij(int x)
{
fl(i,1,n,1) d[i] = 1e9;
d[x] = 0;
q.push({0, x});
while (!q.empty())
{
auto p1 = q.top();
int W = p1.fi;
int x = p1.se;
q.pop();
if (vis[x])
continue;
vis[x] = 1;
for (auto p2 : a[x])
{
int y = p2.fi;
int w = p2.se;
if (vis[y] || d[y] < W + w)
continue;
d[y] = W + w;
q.push({W + w, y});
}
}
}
void inp()
{
cin >> n >> m;
fl(i,1,m,1)
{
int u,v,c,p;
cin >> u >> v >> c >> p;
g[u].push_back({c, {v, p}});
g[v].push_back({c, {u, p}});
a[u].push_back({v, p});
a[v].push_back({u, p});
}
}
void sol()
{
int cur = n;
fl(i,1,n,1)
{
sort(g[i].begin(), g[i].end());
for (auto it : g[i])
t[it.fi] += it.se.se;
for (auto it : g[i])
{
if (!vis[it.fi])
{
++cur;
a[i].push_back({cur,0});
}
vis[it.fi] = 1;
a[cur].push_back({it.se.fi, t[it.fi] - it.se.se});
a[it.se.fi].push_back({cur, 0});
}
for (auto it : g[i])
{
t[it.fi] = 0;
vis[it.fi] = 0;
}
}
dij(1);
if (d[n]==1e9) cout<<"-1";
else cout<<d[n];
}
signed main()
{
// freopen("task.inp","r",stdin);
// freopen("task.out","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
inp();
sol();
return 0;
}
/* /\_/\
( ._. )
/ >V< \
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |