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 TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
#define int long long
const ll maxN=3e5;
const ll inf=1e18;
const ll mod=1e9+7;
ll n,m;
struct edge
{
ll u,v,c,w;
}e[maxN];
// co 2 cach noi 1 la noi tu u->v
// 2 la tu 1 dinh ke cua u noi den u va tu u noi den v ( dinh ke va v cung mau )
vector<ll>g[maxN];
ll d[maxN];
priority_queue<pli,vector<pli>,greater<pli>>q;
ll mn[maxN],sum[maxN];
void solve()
{
cin >> n >> m ;
for(int i=1;i<=m;i++)
{
cin >> e[i].u >> e[i].v >> e[i].c >> e[i].w;
g[e[i].u].pb(i);
g[e[i].v].pb(i);
mn[i]=inf;
sum[i]=0;
}
q.push({0,1});
d[1]=0;
for(int i=2;i<=n;i++) d[i]=inf;
while(!q.empty())
{
ll u=q.top().se;
ll dis=q.top().fi;
q.pop();
if(dis>d[u]) continue;
for(auto id:g[u])
{
int v=u^e[id].u^e[id].v;
int w=e[id].w;
int c=e[id].c;
mn[c]=min(mn[c],d[v]);
sum[c]+=w;
}
for(auto id:g[u])
{
int v=u^e[id].u^e[id].v;
int w=e[id].w;
int c=e[id].c;
if(d[v]>d[u]+min(w,sum[c]-w))
{
d[v]=d[u]+min(w,sum[c]-w);
q.push({d[v],v});
}
if(d[v]>mn[c]+sum[c]-w)
{
d[v]=mn[c]+sum[c]-w;
q.push({d[v],v});
}
//if(v==3) cout << d[v]<<' '<<u<<' '<<mn[c]<<'\n';
//if(v==2) cout <<d[v]<<' '<<u<<' '<<d[u]<<'\n';
}
for(auto id:g[u])
{
int v=u^e[id].u^e[id].v;
int w=e[id].w;
int c=e[id].c;
mn[c]=inf;
sum[c]=0;
}
}
if(d[n]>=inf) cout << -1;
else cout << d[n];
//cout << '\n'<<d[2];
}
signed main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
Compilation message (stderr)
Main.cpp: In function 'void solve()':
Main.cpp:75:17: warning: unused variable 'v' [-Wunused-variable]
75 | int v=u^e[id].u^e[id].v;
| ^
Main.cpp:76:17: warning: unused variable 'w' [-Wunused-variable]
76 | int w=e[id].w;
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |