Codeforces - Buy a Ticket

题目链接:Codeforces - Buy a Ticket


因为到的点是相同的,所以直接建反边即可。


AC代码:

#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+10;
int n,m,a[N],d[N],s,vis[N];
int head[N],nex[N<<2],to[N<<2],w[N<<2],tot;
inline void add(int a,int b,int c){
	to[++tot]=b; nex[tot]=head[a]; w[tot]=c; head[a]=tot;
}
void Dijkstra(){
	priority_queue<pair<int,int> > q; q.push({0,s}); memset(d,0x3f,sizeof d); d[s]=0;
	while(q.size()){
		int u=q.top().second;	q.pop();
		if(vis[u])	continue;	vis[u]=1;
		for(int i=head[u];i;i=nex[i])	if(d[to[i]]>d[u]+w[i]){
			d[to[i]]=d[u]+w[i];	q.push({-d[to[i]],to[i]});
		}
	}
}
signed main(){
	cin>>n>>m;	s=n+1;
	for(int i=1,a,b,c;i<=m;i++)	
		scanf("%lld %lld %lld",&a,&b,&c),add(b,a,2*c),add(a,b,2*c);
	for(int i=1;i<=n;i++)	scanf("%lld",&a[i]),add(s,i,a[i]);
	Dijkstra();
	for(int i=1;i<=n;i++)	printf("%lld ",d[i]);
	return 0;
}
发布了725 篇原创文章 · 获赞 244 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/weixin_43826249/article/details/104856729