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 INF 200000000000000LL
#define DIMN 100010
using namespace std;
long long d[DIMN] , ds[DIMN] , dt[DIMN];
long long dp[3][DIMN];
vector <pair <int,long long> > v[DIMN] , w[DIMN];
priority_queue <pair <long long,int> > h;
priority_queue <pair <long long , pair <int,int> > > hp;
struct idk{
int x , y;
long long z;
} mch[2*DIMN];
int n;
void dijkstra (int s , long long d[]){
int i , nod , vecin;
long long cost , c;
for (i = 1 ; i <= n ; i++)
d[i] = INF;
d[s] = 0;
h.push(make_pair( 0 , s ));
while (!h.empty()){
nod = h.top().second;
cost = -h.top().first;
h.pop();
if (d[nod] != cost)
continue;
for (i = 0 ; i < v[nod].size() ; i++){
vecin = v[nod][i].first;
c = v[nod][i].second;
if (d[vecin] > d[nod] + c){
d[vecin] = d[nod] + c;
h.push(make_pair(-d[vecin] , vecin));
}
}
}
}
int main()
{
FILE *fin = stdin;
FILE *fout = stdout;
int i , m , s , t , u , vv , nod , vecin , x , y , z , stare , idk;
long long c , cost;
fscanf (fin,"%d%d%d%d%d%d",&n,&m,&s,&t,&u,&vv);
for (i = 1 ; i <= m ; i++){
fscanf (fin,"%d%d%d",&x,&y,&z);
v[x].push_back(make_pair(y , z));
v[y].push_back(make_pair(x , z));
mch[i] = {x , y , z};
}
/// un dijkstra din s
dijkstra (s , ds);
/// un dijkstra din t
dijkstra (t , dt);
/// inca un dijkstra dar pe w intre u si v
for (i = 1 ; i <= n ; i++)
dp[0][i] = dp[1][i] = dp[2][i] = INF;
if (ds[u] + dt[u] == ds[t]){
dp[1][u] = 0;
hp.push(make_pair( 0 , make_pair(1 , u) ));
hp.push(make_pair( 0 , make_pair(-1 , u) ));
}
dp[0][u] = 0;
hp.push(make_pair( 0 , make_pair(0 , u) ));
while (!hp.empty()){
nod = hp.top().second.second;
stare = hp.top().second.first;
cost = -hp.top().first;
idk = 0;
if (stare < 0){
idk = 1;
stare = -stare;
}
hp.pop();
if (dp[stare][nod] != cost)
continue;
for (i = 0 ; i < v[nod].size() ; i++){
vecin = v[nod][i].first;
c = v[nod][i].second;
if (stare == 0){
if (ds[vecin] + dt[vecin] == ds[t]){ /// schimba starea
if (dp[1][vecin] > dp[0][nod] + c){
dp[1][vecin] = dp[0][nod] + c;
hp.push(make_pair( -dp[1][vecin] , make_pair(1 , vecin) ));
hp.push(make_pair( -dp[1][vecin] , make_pair(-1 , vecin) ));
}
}
if (dp[0][vecin] > dp[0][nod] + c){
dp[0][vecin] = dp[0][nod] + c;
hp.push(make_pair( -dp[0][vecin] , make_pair(0 , vecin) ));
}
}
else if (stare == 1){
if ((idk == 0 && ds[nod] + dt[vecin] + c == ds[t]) || (idk == 1 && ds[vecin] + dt[nod] + c == ds[t])){
if (dp[1][vecin] > dp[1][nod]){ /// pastrezi starea 1
dp[1][vecin] = dp[1][nod];
if (!idk)
hp.push(make_pair( -dp[1][vecin] , make_pair(1 , vecin) ));
else hp.push(make_pair( -dp[1][vecin] , make_pair(-1 , vecin) ));
}
}
if (dp[2][vecin] > dp[1][nod] + c){ /// schimbi starea
dp[2][vecin] = dp[1][nod] + c;
hp.push(make_pair( -dp[2][vecin] , make_pair(2 , vecin) ));
}
}
else {
if (dp[2][vecin] > dp[2][nod] + c){ /// pastrezi starea 2
dp[2][vecin] = dp[2][nod] + c;
hp.push(make_pair( -dp[2][vecin] , make_pair(2 , vecin) ));
}
}
}
}
long long sol = min(dp[0][vv] , min(dp[1][vv] , dp[2][vv]));
for (i = 1 ; i <= n ; i++)
dp[0][i] = dp[1][i] = dp[2][i] = INF;
if (ds[vv] + dt[vv] == ds[t]){
dp[1][vv] = 0;
hp.push(make_pair( 0 , make_pair(1 , vv) ));
hp.push(make_pair( 0 , make_pair(-1 , vv) ));
}
dp[0][vv] = 0;
hp.push(make_pair( 0 , make_pair(0 , vv) ));
while (!hp.empty()){
nod = hp.top().second.second;
stare = hp.top().second.first;
cost = -hp.top().first;
idk = 0;
if (stare < 0){
idk = 1;
stare = -stare;
}
hp.pop();
if (dp[stare][nod] != cost)
continue;
for (i = 0 ; i < v[nod].size() ; i++){
vecin = v[nod][i].first;
c = v[nod][i].second;
if (stare == 0){
if (ds[vecin] + dt[vecin] == ds[t]){ /// schimba starea
if (dp[1][vecin] > dp[0][nod] + c){
dp[1][vecin] = dp[0][nod] + c;
hp.push(make_pair( -dp[1][vecin] , make_pair(1 , vecin) ));
hp.push(make_pair( -dp[1][vecin] , make_pair(-1 , vecin) ));
}
}
if (dp[0][vecin] > dp[0][nod] + c){
dp[0][vecin] = dp[0][nod] + c;
hp.push(make_pair( -dp[0][vecin] , make_pair(0 , vecin) ));
}
}
else if (stare == 1){
if ((idk == 0 && ds[nod] + dt[vecin] + c == ds[t]) || (idk == 1 && ds[vecin] + dt[nod] + c == ds[t])){
if (dp[1][vecin] > dp[1][nod]){ /// pastrezi starea 1
dp[1][vecin] = dp[1][nod];
if (!idk)
hp.push(make_pair( -dp[1][vecin] , make_pair(1 , vecin) ));
else hp.push(make_pair( -dp[1][vecin] , make_pair(-1 , vecin) ));
}
}
if (dp[2][vecin] > dp[1][nod] + c){ /// schimbi starea
dp[2][vecin] = dp[1][nod] + c;
hp.push(make_pair( -dp[2][vecin] , make_pair(2 , vecin) ));
}
}
else {
if (dp[2][vecin] > dp[2][nod] + c){ /// pastrezi starea 2
dp[2][vecin] = dp[2][nod] + c;
hp.push(make_pair( -dp[2][vecin] , make_pair(2 , vecin) ));
}
}
}
}
sol = min(sol , min(dp[0][u] , min(dp[1][u] , dp[2][u])));
fprintf (fout,"%lld",sol);
return 0;
}
Compilation message (stderr)
commuter_pass.cpp: In function 'void dijkstra(int, long long int*)':
commuter_pass.cpp:38:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0 ; i < v[nod].size() ; i++){
~~^~~~~~~~~~~~~~~
commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:105:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0 ; i < v[nod].size() ; i++){
~~^~~~~~~~~~~~~~~
commuter_pass.cpp:182:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0 ; i < v[nod].size() ; i++){
~~^~~~~~~~~~~~~~~
commuter_pass.cpp:58:12: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fscanf (fin,"%d%d%d%d%d%d",&n,&m,&s,&t,&u,&vv);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:61:16: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fscanf (fin,"%d%d%d",&x,&y,&z);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |