Submission #1281745

#TimeUsernameProblemLanguageResultExecution timeMemory
1281745sitingfakeCommuter Pass (JOI18_commuter_pass)C++20
100 / 100
469 ms26356 KiB
#include<bits/stdc++.h>
using namespace std;

// define

#define execute cerr << " Time: " << fixed << setprecision(6) << (1.0 * clock() / CLOCKS_PER_SEC) << "s\n";
#define ll long long
#define ii pair <ll , ll>
#define iii pair <ll , ii>
#define se second
#define fi first
#define all(v) (v).begin() , (v).end()
#define Unique(v) sort(all(v)) , v.resize(unique(all(v)) - v.begin())
#define bit(x,i) (((x) >> (i)) & 1LL)
#define flip(x,i) ((x) ^ (1LL << (i)))
#define ms(d,x) memset(d , x , sizeof(d))
#define exist __exist
#define ends __ends
#define visit visited
#define left __left
#define right __right
#define sitingfake 1
#define orz 1
//constant

const long long mod = 1e9 + 7;
const long long linf = 4557430888798830399LL;
const long long nlinf = -4485090715960753727LL;
const int inf = 1061109567;
const int ninf = -1044266559;
const int dx[] = {0 , -1 , 0 , 1};
const int dy[] = {-1 , 0 , 1 , 0};

template<typename T> bool maximize(T &a, const T &b)
{
    if(a < b) {a = b; return 1;}
    return 0;
}

template<typename T> bool minimize(T &a, const T &b)
{
    if(a > b) {a = b; return 1;}
    return 0;
}

void Plus(ll & a ,ll b)
{
    b %= mod;
    a += b;
    if(a < 0) a += mod;
    a %= mod;
    return;
}

void Mul(ll & a, ll b)
{
    (a *= (b % mod)) %= mod;
    return;
}

//code
const int maxn = 1e5 + 7;

int S , T , U , V;

ll dist[maxn][3] , distS[maxn] , distT[maxn];

vector <ii> a[maxn];
int n , m;

void Dijkstra(int source , ll dist[])
{
    for(int i = 1; i <= n; i++)
    {
        dist[i] = linf;
    }
    dist[source] = 0;
    priority_queue <ii , vector <ii> , greater <ii>> pq;
    pq.push({0 , source});
    while(!pq.empty())
    {
        int u = pq.top().se;
        ll cost = pq.top().fi;
        pq.pop();
        if(cost > dist[u]) continue;
        for(ii i : a[u])
        {
            int v = i.fi;
            ll w = i.se;
            if(minimize(dist[v] , dist[u] + w))
            {
                pq.push({dist[v] , v});
            }
        }
    }
}

bool check(int u , int v , ll w)
{
    return distS[u] + distT[v] + w == distS[T];
}
void solve(void)
{
    cin >> n >> m;
    cin >> S >> T;
    cin >> U >> V;

    for(int i = 1; i <= m; i++)
    {
        int u , v , w;
        cin >> u >> v >> w;
        a[u].push_back(ii(v , w));
        a[v].push_back(ii(u , w));
    }

    Dijkstra(S , distS);
    Dijkstra(T , distT);

    ms(dist , 0x3f);
    dist[U][0] = 0;

    priority_queue <iii , vector <iii> , greater <iii>> pq;
    pq.push({0 , {U , 0}});

    while(!pq.empty())
    {
        int u = pq.top().se.fi;
        int d = pq.top().se.se;
        ll cost = pq.top().fi;
        pq.pop();
        if(cost > dist[u][d]) continue;

        if(d == 0 || d == 2)
        {
            for(ii i : a[u])
            {
                int v = i.fi;
                ll w = i.se;
                if(minimize(dist[v][d] , dist[u][d] + w))
                {
                    pq.push({dist[v][d] , {v , d}});
                }
            }
        }

        if(d == 1)
        {
            for(ii i : a[u])
            {
                int v = i.fi;
                int w = i.se;
                if(check(u , v , w))
                {
                    if(minimize(dist[v][d] , dist[u][d]))
                    {
                        pq.push({dist[v][d] , {v , d}});
                    }
                }
                if(minimize(dist[v][2] , dist[u][d] + w))
                {
                    pq.push({dist[v][2] , {v , 2}});
                }
            }
        }
        if(d == 0)
        {
            for(ii i : a[u])
            {
                int v = i.fi;
                ll w = i.se;
                if(check(u , v , w))
                {
                    if(minimize(dist[v][1] , dist[u][0]))
                    {
                        pq.push({dist[v][1] , {v , 1}});
                    }
                }
            }
        }
    }

    ll ans = min({dist[V][0] , dist[V][1] , dist[V][2]});

    ms(dist , 0x3f);
    dist[V][0] = 0;

    pq.push({0 , {V , 0}});

    while(!pq.empty())
    {
        int u = pq.top().se.fi;
        int d = pq.top().se.se;
        ll cost = pq.top().fi;
        pq.pop();
        if(cost > dist[u][d]) continue;

        if(d == 0 || d == 2)
        {
            for(ii i : a[u])
            {
                int v = i.fi;
                ll w = i.se;
                if(minimize(dist[v][d] , dist[u][d] + w))
                {
                    pq.push({dist[v][d] , {v , d}});
                }
            }
        }

        if(d == 1)
        {
            for(ii i : a[u])
            {
                int v = i.fi;
                int w = i.se;
                if(check(u , v , w))
                {
                    if(minimize(dist[v][d] , dist[u][d]))
                    {
                        pq.push({dist[v][d] , {v , d}});
                    }
                }
                if(minimize(dist[v][2] , dist[u][d] + w))
                {
                    pq.push({dist[v][2] , {v , 2}});
                }
            }
        }
        if(d == 0)
        {
            for(ii i : a[u])
            {
                int v = i.fi;
                ll w = i.se;
                if(check(u , v , w))
                {
                    if(minimize(dist[v][1] , dist[u][0]))
                    {
                        pq.push({dist[v][1] , {v , 1}});
                    }
                }
            }
        }
    }

    cout << min({ans , dist[U][0] , dist[U][1] , dist[U][2]});


}
/**
**/
signed main()
{
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);

   #define task ""

   if(fopen(task".inp","r"))
   {
       freopen(task".inp","r",stdin);
       freopen(task".out","w",stdout);
   }

   int tc = 1;
//   cin >> tc;
   while(tc--) solve();

//   execute;
}



Compilation message (stderr)

commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:262:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  262 |        freopen(task".inp","r",stdin);
      |        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:263:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  263 |        freopen(task".out","w",stdout);
      |        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...