제출 #1329323

#제출 시각아이디문제언어결과실행 시간메모리
1329323antarbanikCommuter Pass (JOI18_commuter_pass)C++20
0 / 100
2094 ms19584 KiB
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define yes cout << "YES\n";
#define nl cout << endl;
#define no cout << "NO\n";
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define pb push_back
#define ppb pop_back
// #define mp make_pair
#define ff first
#define ss second
#define st string
#define fr(i, x, y) for (int i = x; i < y; i++)
#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

// #define MAX 1e18
// #define MIN -1e18

#ifndef ONLINE_JUDGE
#define debug(x) cerr << setw(15) << left << #x << ": "; _print(x); cerr << endl;
#define del cerr << '\n';
#else
#define debug(x)
#define del
#endif

void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}

template <typename T, typename V> void _print(const pair<T, V>& p) {cerr << '{'; _print(p.first); cerr << ", "; _print(p.second); cerr << '}';}
template <typename T> void _print(const vector<T>& v) {cerr << '['; for (size_t i = 0; i < v.size(); ++i) {_print(v[i]); if (i != v.size() - 1) cerr << ", ";} cerr << ']';}
template <typename T> void _print(const set<T>& s) {cerr << '{'; bool first = true; for (const auto& x : s) {if (!first) cerr << ", "; _print(x); first = false;} cerr << '}';}
template <typename T> void _print(const multiset<T>& s) {cerr << '{'; bool first = true; for (const auto& x : s) {if (!first) cerr << ", "; _print(x); first = false;} cerr << '}';}
template <typename T, typename V> void _print(const map<T, V>& m) {cerr << '{'; bool first = true; for (const auto& kv : m) {if (!first) cerr << ", "; _print(kv); first = false;} cerr << '}';}

int power(int n, int k){int result = 1;while (k > 0) {if (k & 1) {result = (result * n);}n = (n * n);k >>= 1;}return result;}
int gcd(int a, int b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}
int lcm(int a, int b) {return (a / __gcd(a, b)) * b;}
// bool isPrime(int n) {if (n <= 1) return false;if (n == 2) return true;if (n % 2 == 0) return false;for (int i = 3; i * i <= n; i += 2) {if (n % i == 0) return false;}return true;}


/*



*/

const int N = 1e5+3;
vector<pair<int,int>> G[N];
int n, m, Start, End, U, V; 




void shortestPath(int u, int v, vector<int> &path){

    vector<int> par(N, -1);
    vector<int> vis(N, 0);
    vector<int> dis(N, 1e18);
    multiset<pair<int,int>> st; // (cost, node);

    st.insert({0, u});
    dis[u] = 0;

    while(st.size() >= 1){
        auto it = *st.begin();
        int ver = it.ss, cost = it.ff;
        st.erase(st.begin());
        if(ver == v) break;
        if(vis[ver]) continue;
        vis[ver] = 1;
        for(auto e : G[ver]){
            int child = e.ff, childCost = e.ss;
            int oldCost = dis[child];
            int newCost = dis[ver] + childCost;
            if(newCost < oldCost){
                dis[child] = newCost;
                par[child] = ver;
                st.insert({newCost, child});
            }
        }
    }


    for(int i = v; i != -1; i = par[i]){
        path.pb(i);
    }
    reverse(all(path));
}


int dij(int u, int v){
    vector<int> vis(N, 0);
    vector<int> dis(N, 1e18);
    multiset<pair<int,int>> st; // (cost, node);

    st.insert({0, u});
    dis[u] = 0;

    while(st.size() >= 1){
        auto it = *st.begin();
        int ver = it.ss, cost = it.ff;
        st.erase(st.begin());
        if(ver == v) break;
        if(vis[ver]) continue;
        vis[ver] = 1;
        for(auto e : G[ver]){
            int child = e.ff, childCost = e.ss;
            int oldCost = dis[child];
            int newCost = dis[ver] + childCost;
            if(newCost < oldCost){
                dis[child] = newCost;
                st.insert({newCost, child});
            }
        }
    }

    return dis[v];
}




void solve(){
    cin>>n>>m;
    cin>>Start>>End;
    cin>>U>>V;



    for(int i = 0;i<m;++i){
        int u, v, c; cin>>u>>v>>c;
        G[u].pb({v, c});
        G[v].pb({u, c});
    }
        
    vector<int> path;

    shortestPath(Start, End, path);
    int ans = 1e18;
    for(auto e : path){
        ans = min(ans, dij(e, V));
    }
    cout<<ans;



}


int32_t main() {
    FAST



    #ifndef ONLINE_JUDGE
        freopen("error.txt", "w", stderr);
    #endif


    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
        cout << endl;
    }
}

컴파일 시 표준 에러 (stderr) 메시지

commuter_pass.cpp: In function 'int32_t main()':
commuter_pass.cpp:160:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  160 |         freopen("error.txt", "w", stderr);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...