Submission #1245818

#TimeUsernameProblemLanguageResultExecution timeMemory
1245818vuxxcodeCities (BOI16_cities)C++20
74 / 100
6096 ms114776 KiB

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int, int> pii;
typedef pair<ll,pll> lll;
#define fi first
#define se second
void maxi (int &x, int y) {x = max (x, y);}
void mini (int &x, int y) {x = min (x, y);}

const ll maxN = 2e5 + 5, maxLog = 20, mod = 1e9 + 7, inf64 = 1e18, maxK = 5;

class Compare {
public:
	bool operator()(lll a, lll b) {
		if(a. se. se != b. se. se) return a. se. se > b. se. se;
		if(a. fi != b. fi) return a. fi > b. fi;
		return a. se. fi > b. se. fi;
	}
};

ll n, k, m, a [maxK], dij [maxN] [1 << maxK];
vector <pii> adj [maxN];
priority_queue <lll, vector <lll>, Compare> q;

signed main () {
    ios::sync_with_stdio (0);
    cin. tie (0);
    cout. tie (0);
    #define task "untitled1"
    if (fopen (task".inp", "r")) {
        freopen (task".inp", "r", stdin);
        freopen (task".out", "w", stdout);
    }
    cin >> n >> k >> m;
    for (int i = 0; i < k; i ++) cin >> a [i];
    for (int i = 1, u, v, w; i <= m; i ++) {
        cin >> u >> v >> w;
        adj [u]. push_back ({v, w});
        adj [v]. push_back ({u, w});
    }
    memset (dij, 0x3f, sizeof dij);
    for (int i = 0; i < k; i ++) {
        dij [a [i]] [1 << i] = 0;
        q. push ({0ll, {a [i], (1 << i)}});
    }
    while (!q. empty ()) {
        ll dis = q. top (). fi;
        pll pos = q. top (). se;
        q. pop ();
        if (dis != dij [pos. fi] [pos. se]) continue;
        for (int i = 0; i < (1 << k); i ++) if (!(pos. se & i)){
            if (dij [pos. fi] [i | pos. se] > dij [pos. fi] [i] + dij [pos. fi] [pos. se]) {
                dij [pos. fi] [i | pos. se] = dij [pos. fi] [i] + dij [pos. fi] [pos. se];
                q. push ({dij [pos. fi] [i | pos. se], {pos. fi, (i | pos. se)}});
            }
        }
        for (pll i : adj [pos. fi]) {
            if (dij [i. fi] [pos. se] > dij [pos. fi] [pos. se] + i. se) {
                dij [i. fi] [pos. se] = dij [pos. fi] [pos. se] + i. se;
                q. push ({dij [i. fi] [pos. se],  {i. fi, pos. se}});
            }
        }
    }
    ll ans = inf64;
    for (int i = 1; i <= n; i ++) mini (ans, dij [i] [(1 << k) - 1]);
    cout << ans;
}

Compilation message (stderr)

cities.cpp: In function 'int main()':
cities.cpp:35:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 |         freopen (task".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
cities.cpp:36:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |         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...
#Verdict Execution timeMemoryGrader output
Fetching results...