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>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define pii pair<int,int>
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define mem(a,i) memset(a,i,sizeof(a))
#define sz(s) (int)s.size()
#define int ll
#define y1 yy
#define maksim gay
#define ppb pop_back
#define gcd(a,b) __gcd(a,b)
#define in insert
const int dx[4]={-1,0,1,0};
const int dy[4]={0,-1,0,1};
const int inf=1e16;
const int N=5e4+100;
const int MAX=210;
const int mod=1e9+7;
int n,m;
int a[N],b[N],c[N],dv[N];
vector<pii> g[MAX];
vector<pii> g1[MAX];
int use[MAX];
int cmp[MAX];
vector<int> top;
void dfs(int v){
use[v]=1;
for(auto to:g[v]){
if(!use[to.F])dfs(to.F);
}
top.pb(v);
}
void dfs1(int v,int c){
cmp[v]=c;
for(auto to:g1[v]){
if(!cmp[to.F]){
dfs1(to.F,c);
}
}
}
int dp[MAX][MAX];
int ban[N];
int d[MAX];
int solve(int x){
int ans=0;
vector<pii> g[MAX];
for(int i=1;i<=m;i++){
if(i==x){
g[b[i]].pb({a[i],c[i]});
}
else{
g[a[i]].pb({b[i],c[i]});
}
}
{
for(int i=1;i<=n;i++)d[i]=inf;
d[1]=0;
mem(use,0);
for(int i=1;i<=n;i++){
int v=-1;
for(int j=1;j<=n;j++){
if(!use[j]&&(v==-1||d[j]<d[v])){
v=j;
}
}
use[v]=1;
for(auto to:g[v]){
if(d[to.F]>d[v]+to.S){
d[to.F]=d[v]+to.S;
}
}
}
ans+=d[n];
}
{
for(int i=1;i<=n;i++)d[i]=inf;
d[n]=0;
mem(use,0);
for(int i=1;i<=n;i++){
int v=-1;
for(int j=1;j<=n;j++){
if(!use[j]&&(v==-1||d[j]<d[v])){
v=j;
}
}
use[v]=1;
for(auto to:g[v]){
if(d[to.F]>d[v]+to.S){
d[to.F]=d[v]+to.S;
}
}
}
ans+=d[1];
}
// cout<<x<<" "<<ans<<"\n";
return ans;
}
vector<int> reb;
vector<int> fff;
void dfs2(int v,int en){
use[v]=1;
if(v==en){
// cout<<sz(fff)<<"\n";
for(int x:fff)reb.pb(x);
return;
}
for(auto to:g1[v]){
if(use[to.F]||dp[en][to.F]+c[to.S]!=dp[en][v])continue;
fff.pb(to.S);
dfs2(to.F,en);
fff.ppb();
}
}
void SUB1(){
mem(use,0);
dfs2(1,n);
mem(use,0);
dfs2(n,1);
int ans=min(inf,dp[1][n]+dp[n][1]);
for(int x:reb){
ans=min(ans,solve(x)+dv[x]);
ban[x]=1;
}
// cout<<sz(reb)<<"\n";
for(int i=1;i<=m;i++){
if(!ban[i]){
int c1n=dp[1][n];
{
c1n=min(c1n,dp[1][b[i]]+c[i]+dp[a[i]][n]);
// cout<<1<<" "<<b[i]<<" "<<a[i]<<" "<<n<<"\n";
}
int cn1=dp[n][1];
{
cn1=min(cn1,dp[n][b[i]]+c[i]+dp[a[i]][1]);
// cout<<n<<" "<<b[i]<<" "<<a[i]<<" "<<1<<"\n";
}
// cout<<i<<" "<<c1n<<" "<<cn1<<"\n";
ans=min(ans,c1n+cn1+dv[i]);
}
}
if(ans!=inf)cout<<ans<<"\n";
else cout<<-1<<"\n";
}
void solve(){
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>a[i]>>b[i]>>c[i]>>dv[i];
g[a[i]].pb({b[i],c[i]});
g1[b[i]].pb({a[i],i});
}
for(int i=1;i<=n;i++){
if(!use[i]){
dfs(i);
}
}
int cnt=0;
reverse(all(top));
for(int x:top){
if(!cmp[x]){
dfs1(x,++cnt);
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i!=j)dp[i][j]=inf;
}
}
for(int i=1;i<=m;i++){
dp[a[i]][b[i]]=min(dp[a[i]][b[i]],c[i]);
}
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]);
}
}
}
// cout<<-1<<"\n";
// return;
SUB1();
}
main(){
// freopen("prizes.in", "r", stdin);
// freopen("prizes.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
// cin>>t;
while(t--){
solve();
}
}
Compilation message (stderr)
ho_t4.cpp:207:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
207 | main(){
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |