1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| typedef long long ll;
#define Turnoff std::ios::sync_with_stdio(false) #define P pair<ll,int> const ll Max=3e5+5; const ll Mod=998857459; const int inf=0x3f3f3f3f;
class Solution { public:
bool vis[Max]; vector<P>mp[Max]; ll dp[(1<<5)+5][Max]; void ShoutCut(ll* dis,int n){ priority_queue<P,vector<P>,greater<P> >q; for(int i=1;i<=n;i++){ if(dis[i]!=inf)q.push({dis[i],i}); vis[i]=0; } while(!q.empty()){ P point=q.top(); q.pop(); int now=point.second; if(vis[now])continue; vis[now]=1; for(auto to:mp[now]){ if(dis[to.first]>dis[now]+to.second){ dis[to.first]=dis[now]+to.second; q.push({dis[to.first],to.first}); } } } } long long cost(int n, vector<int> &k, vector<vector<int>> &m) { int Kland=k.size(); for(auto edge:m){ int u=edge[0],v=edge[1],w=edge[2]; mp[u].push_back({v,w}); mp[v].push_back({u,w}); } memset(dp,inf,sizeof dp); for(int i=0;i<Kland;i++)dp[1<<i][k[i]]=0; for(int S=1,up=1<<Kland;S<up;S++){ for(int start=1;start<=n;start++){ for(int S0=S&(S-1);S0;S0=S&(S0-1)){ dp[S][start]=min(dp[S][start],dp[S0][start]+dp[S^S0][start]); } } ShoutCut(dp[S],n); } return dp[(1<<Kland)-1][k[0]]; } };
|