Submission #144008

# Submission time Handle Problem Language Result Execution time Memory
144008 2019-08-15T16:13:43 Z Vlatko Cities (BOI16_cities) C++14
0 / 100
6000 ms 17824 KB
#include <bits/stdc++.h>
using namespace std;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;

const int maxn = 100010;
const int maxm = 200010;

int n, m;
ll ans;
vector<int> special;
vector<int> adj[maxn];

struct Edge {
    int u, v, w, w0;
    inline int other(int x) {
        return x == u ? v : u;
    }
} edges[maxm];

ll dist[maxm];
int pre[maxm];

void dijkstra(int source) {
    priority_queue<pli, vector<pli>, greater<pli>> pq;
    pq.emplace(0, source);
    fill(dist, dist+n+1, 1e18);
    dist[source] = 0;
    pre[source] = -1;
    while (!pq.empty()) {
        pli top = pq.top();
        pq.pop();
        ll d = top.first;
        int u = top.second;
        if (d > dist[u]) continue;
        for (int ei : adj[u]) {
            int v = edges[ei].other(u);
            ll w = edges[ei].w;
            if (d + w < dist[v]) {
                dist[v] = d + w;
                pq.emplace(d + w, v);
                pre[v] = ei;
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int k;
    cin >> n >> k >> m;
    for (int i = 0; i < k; ++i) {
        int x;
        cin >> x;
        special.push_back(x);
    }
    for (int i = 1; i <= m; ++i) {
        cin >> edges[i].u >> edges[i].v >> edges[i].w0;
        adj[edges[i].u].push_back(i);
        adj[edges[i].v].push_back(i);
    }
    sort(special.begin(), special.end());
    ll ans = 1e18;
    do {
        for (int i = 1; i <= m; ++i) {
            edges[i].w = edges[i].w0;
        }
        // debug(special);
        ll res = 0;
        for (int i = 1; i < k; ++i) {
            dijkstra(special[i]);
            int bestnd = -1;
            for (int j = 0; j < i; ++j) {
                if (bestnd == -1 || dist[special[j]] < dist[bestnd]) {
                    bestnd = special[j];
                }
            }
            // debug(special[i], bestnd);
            res += dist[bestnd];
            while (bestnd != special[i]) {
                int ei = pre[bestnd];
                edges[ei].w = 0;
                bestnd = edges[ei].other(bestnd);
            }
        }
        ans = min(ans, res);
    } while (next_permutation(special.begin(), special.end()));
    cout << ans << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2808 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Incorrect 5 ms 2680 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1008 ms 17544 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 27 ms 2780 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5369 ms 17700 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 6059 ms 17824 KB Time limit exceeded
2 Halted 0 ms 0 KB -