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("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2")
#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
struct edg {
long long a , b , c , p , ind;
};
struct edgg {
long long b , c , p , ind , sum;
};
long long n , m , dp [600005] , summ [200005];
map < int , vector < edgg > > gr [100005];
map < int , long long > sum [100005];
vector < edg > edgs;
int main ()
{
scanf ( "%lld%lld" , &n , &m );
for ( int i = 1 ; i <= n ; i ++ )
{
edgs . push_back ( { i , 0 , 0 , 0 , i - 1 } );
}
for ( int i = 0 ; i < m ; i ++ )
{
long long x , y , c , p;
scanf ( "%lld%lld%lld%lld" , &x , &y , &c , &p );
gr [y][c] . push_back ( { x , c , p , edgs . size () } );
edgs . push_back ( { x , y , c , p , edgs . size () } );
gr [x][c] . push_back ( { y , c , p , edgs . size () } );
edgs . push_back ( { y , x , c , p , edgs . size () } );
sum [x][c] += p;
sum [y][c] += p;
}
for ( int i = 0 ; i <= n + m + m + 1 ; i ++ ) dp [i] = 1e16;
dp [0] = 0;
priority_queue < pair < long long , int > > q;
q . push ( { 0 , 0 } );
long long ans = 1e16;
while ( q . size () )
{
pair < long long , int > p = q . top ();
q . pop ();
int ind = p . second;
if ( dp [ind] != - p . first ) continue;
int x = edgs [ind] . a , last = edgs [ind] . b;
if ( x == n && last == 0 )
{
ans = min ( ans , dp [ind] );
break;
}
if ( last == 0 )
{
for ( auto xx : gr [x] )
{
for ( auto uu : xx . second )
{
long long u = uu . b , c = uu . c , p = uu . p , summ = sum [x][c] - p;
int indd = u - 1;
if ( dp [ind] + summ < dp [indd] )
{
dp [indd] = dp [ind] + summ;
q . push ( { - dp [indd] , indd } );
}
if ( dp [ind] + p < dp [indd] )
{
dp [indd] = dp [ind] + p;
q . push ( { - dp [indd] , indd } );
}
indd = uu . ind;
if ( dp [ind] < dp [indd] )
{
dp [indd] = dp [ind];
q . push ( { - dp [indd] , indd } );
}
}
}
}
else
{
for ( auto uu : gr [x][ edgs [ind] . c ] )
{
long long u = uu . b , c = uu . c , p = uu . p , summ = sum [x][c] - p;
int indd = u - 1;
if ( dp [ind] + summ < dp [indd] )
{
dp [indd] = dp [ind] + summ;
q . push ( { - dp [indd] , indd } );
}
}
}
}
if ( ans == 1e16 ) printf ( "-1" );
else printf ( "%lld" , ans );
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:34:59: warning: narrowing conversion of 'edgs.std::vector<edg>::size()' from 'std::vector<edg>::size_type' {aka 'long unsigned int'} to 'long long int' [-Wnarrowing]
34 | gr [y][c] . push_back ( { x , c , p , edgs . size () } );
| ~~~~~~~~~~~~^~
Main.cpp:35:58: warning: narrowing conversion of 'edgs.std::vector<edg>::size()' from 'std::vector<edg>::size_type' {aka 'long unsigned int'} to 'long long int' [-Wnarrowing]
35 | edgs . push_back ( { x , y , c , p , edgs . size () } );
| ~~~~~~~~~~~~^~
Main.cpp:37:59: warning: narrowing conversion of 'edgs.std::vector<edg>::size()' from 'std::vector<edg>::size_type' {aka 'long unsigned int'} to 'long long int' [-Wnarrowing]
37 | gr [x][c] . push_back ( { y , c , p , edgs . size () } );
| ~~~~~~~~~~~~^~
Main.cpp:38:58: warning: narrowing conversion of 'edgs.std::vector<edg>::size()' from 'std::vector<edg>::size_type' {aka 'long unsigned int'} to 'long long int' [-Wnarrowing]
38 | edgs . push_back ( { y , x , c , p , edgs . size () } );
| ~~~~~~~~~~~~^~
Main.cpp:23:11: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
23 | scanf ( "%lld%lld" , &n , &m );
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:32:15: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | scanf ( "%lld%lld%lld%lld" , &x , &y , &c , &p );
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |