思路:我们可以开虚拟节点来连接两个结点权值二进制1的个数相同的两个结点,由于1e9内1的个数不超过30,所以开30个虚拟节点,第i个结点连向二进制1的个数为i的点,然后跑dij即可。
代码:
int n, m;
struct d
{
int u, w;
};
int f(int x){
int cnt = 0;
while(x){
if(x & 1)
cnt ++;
x /= 2;
}
return cnt;
}
vector<d>e[100100];
int c[100010], dist[100010], st[100010];
const int C = 100010;
void dij(){
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>q;
memset(dist,0x3f,sizeof dist);
q.push({0, 1});
dist[1] = 0;
while(q.size()){
auto t = q.top();
q.pop();
int d = t.first, u = t.second;
if(st[u])
continue;
st[u] = 1;
for(auto [x,w] : e[u]){
if(d + w < dist[x]){
dist[x] = d + w;
q.push({x,dist[x]});
}
}
}
}
void solve(){
cin >> n >> m;
for(int i = 1;i <= n;i ++){
cin >> c[i];
e[i].push_back({f(c[i]) + C, c[i]});
e[f(c[i]) + C].push_back({i, 0});
}
for(int i = 0;i < m;i ++){
int u,v,w;
cin >> u >> v >> w;
e[u].push_back({v,w});
e[v].push_back({u,w});
}
dij();
if(dist[n] == 0x3f3f3f3f)
cout << "gou yun hao";
else
cout << dist[n];
}