牛客IOI周赛26-普及组 最短路

link:https://ac.nowcoder.com/acm/problem/219918
在这里插入图片描述
思路:
建立虚拟原点 31个点,如果a[i]>>k&1 就和k连一条边权为a[i]的无向边,比赛时候没想到这种建图=-=。

// Problem: 最短路
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/11233/D
// Memory Limit: 524288 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
#define IL inline
#define x first
#define y second
typedef long long ll;
using namespace std;
const int N=200010;
const int M=1000010;
const ll inf=0x3f3f3f3f3f3f3f3f;
int h[N],e[M],ne[M],idx,w[M];
ll dist[N];
int a[N];
void add(int a,int b,int c)
{
	e[idx]=b,ne[idx]=h[a],w[idx]=c,h[a]=idx++;
}
bool st[N];
void dijkstra()
{
	priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>>q;
	q.push({0,1});
	dist[1] = 0;
	while(q.size())
	{
		auto t  = q.top();
		q.pop();
		if(st[t.y])	continue;
		st[t.y]=1;
		for(int i=h[t.y];~i;i=ne[i])
		{
			int  j = e[i];
			if(dist[j]>dist[t.y]+w[i])
			{
				dist[j]=dist[t.y]+w[i];
				q.push({dist[j],j});
			}
		}
	}
}
int main()
{
	memset(h,-1,sizeof h);
	int n;
	cin >> n ;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=1;i<=n;i++)
		for(int j=0;j<=30;j++)
			if(a[i]>>j&1)
				add(i,j+n+1,a[i]),add(j+n+1,i,a[i]);
		memset(dist,0x3f,sizeof dist);
		dijkstra();
	for(int i=1;i<=n;i++)
		if(dist[i]==inf)
			cout<<-1<<" ";
		else
		cout<<dist[i]<<" ";
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值