# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
91816 | Mercenary | Cities (BOI16_cities) | C++11 | 2578 ms | 44972 KiB |
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>
using namespace std;
#define taskname "BOI16CITIES"
#define pb push_back
typedef long double ld;
typedef long long ll;
const int maxn = 1e5 + 5;
const int maxs = 1 << 5;
typedef pair<int,int> ii;
typedef pair<ll,int> lli;
const ll inf = 1e18;
#define mp make_pair
ll d[maxs][maxn];
int n , m , k;
vector<ii> adj[maxn];
void dij(ll d[maxn])
{
priority_queue<lli,vector<lli>,greater<lli>> pq;
for(int i = 1 ; i <= n ; ++i)pq.push(mp(d[i] , i));
while(!pq.empty())
{
auto u = pq.top();pq.pop();
if(d[u.second] != u.first)continue;
for(auto c : adj[u.second])
{
if(d[c.first] > u.first + c.second)
{
d[c.first] = u.first + c.second;
pq.push(mp(d[c.first],c.first));
}
}
}
}
void enter()
{
cin >> n >> k >> m;
for(int i = 0 ; i < (1 << k) ; ++i)
for(int j = 1 ; j <= n ; ++j)
d[i][j] = inf;
for(int i = 0 ; i < k ; ++i)
{
int u;cin >> u;
d[1 << i][u] = 0;
}
for(int i = 1 ; i <= m ; ++i)
{
int u , v , c;cin >> u >> v >> c;
adj[u].pb(mp(v,c));
adj[v].pb(mp(u,c));
}
}
void solve()
{
for(int i = 0 ; i < (1 << k) ; ++i)
{
for(int j = i ; j > 0 ; j = (j - 1) & i)
for(int t = 1 ; t <= n ; ++t)
d[i][t] = min(d[i][t] , d[j][t] + d[i ^ j][t]);
dij(d[i]);
}
ll res = inf;
for(int i = 1 ; i <= n ; ++i)res = min(res , d[(1 << k) - 1][i]);
cout << res;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
if(fopen(taskname".INP","r"))
freopen(taskname".INP", "r",stdin) ,
freopen(taskname".OUT", "w",stdout);
enter();
solve();
}
Compilation message (stderr)
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |