# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
64500 | TadijaSebez | Cities (BOI16_cities) | C++11 | 5451 ms | 64328 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 <stdio.h>
#include <set>
#include <vector>
#include <queue>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
const ll inf=1e18;
const int N=100050;
ll min(ll a, ll b){ return a>b?b:a;}
ll dp[N][1<<5];
vector<pair<int,int> > E[N];
//set<pair<ll,int> > ord;
priority_queue<pair<ll,int> > pq;
void Extend(int x, int n)
{
int i;
for(i=1;i<=n;i++) pq.push(mp(-dp[i][x],i));//ord.insert(mp(dp[i][x],i));
while(pq.size())
{
//int u=ord.begin()->second;
//ord.erase(ord.begin());
int u=pq.top().second;
ll h=-pq.top().first;
pq.pop();
if(h!=dp[u][x]) continue;
for(i=0;i<E[u].size();i++)
{
int v=E[u][i].first;
int w=E[u][i].second;
if(dp[v][x]>dp[u][x]+w)
{
//ord.erase(mp(dp[v][x],v));
dp[v][x]=dp[u][x]+w;
pq.push(mp(-dp[v][x],v));
//ord.insert(mp(dp[v][x],v));
}
}
}
}
int count(int x){ int ret=0;while(x) ret+=x&1,x>>=1;return ret;}
int p[5];
int main()
{
int n,k,m,i,j,u,v,w;
scanf("%i %i %i",&n,&k,&m);
for(i=0;i<k;i++) scanf("%i",&p[i]);
for(i=1;i<=m;i++) scanf("%i %i %i",&u,&v,&w),E[u].pb(mp(v,w)),E[v].pb(mp(u,w));
for(i=0;i<1<<k;i++) for(j=1;j<=n;j++) dp[j][i]=inf;
for(i=0;i<k;i++) dp[p[i]][1<<i]=0;
int mask;
for(mask=1;mask<1<<k;mask++)
{
//printf("%i :D\n",mask);
if(count(mask)>1)
{
for(j=(mask-1)&mask;j;j=(j-1)&mask)
{
int l=mask^j;
//printf("%i %i\n",mask,j);
for(i=1;i<=n;i++) dp[i][mask]=min(dp[i][mask],dp[i][j]+dp[i][l]);
}
}
Extend(mask,n);
}
mask--;
ll sol=inf;
for(i=1;i<=n;i++) sol=min(sol,dp[i][mask]);
printf("%lld\n",sol);
return 0;
}
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... |