SSLOJ 1436.賽艇表演【最短路】


題目:

傳送門


題意:

給出一張圖以及在每個點看錶演所要的花費是多少
問對於每個點,在圖中選擇任意一個點使得到這個點看錶演的花費最小


分析:

我們假設出一個虛點00,將每個點與虛點相連,以到該點看錶演的花費爲權值,如此跑一邊最短路即可


代碼:

#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#define LL long long
using namespace std;
inline LL read() {
    LL d=0,f=1;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
    return d*f;
}
struct node{
	LL to,next,w;
}e[800005];
LL ls[800005],cnt=0,n=read(),m=read();
void add(LL x,LL y,LL w)
{
	e[cnt]=(node){y,ls[x],w};
	ls[x]=cnt++;
	return;
}
struct dui{
	LL num,w;
	bool operator < (const dui &www) const {return w>www.w;}
};
LL dis[200005];
void dij(LL s)
{
	for(LL i=1;i<=n;i++) dis[i]=1000000000000000ll;
	dis[s]=0; 
	priority_queue<dui> q;
	q.push((dui){s,0});
	while(q.size())
	{
		LL u=q.top().num,v=q.top().w;
		q.pop();
		if(v!=dis[u]) continue;
		for(LL i=ls[u];~i;i=e[i].next)
		  if(v+e[i].w<dis[e[i].to])
		    dis[e[i].to]=v+e[i].w,q.push((dui){e[i].to,dis[e[i].to]});
	}
	return;
}
int main()
{
	memset(ls,-1,sizeof(ls));
	for(LL i=1;i<=m;i++)
	{
		LL x=read(),y=read(),w=read()*2;
		add(x,y,w);add(y,x,w);
	}
	LL a;
	for(LL i=1;i<=n;i++) a=read(),add(0,i,a),add(i,0,a);
	dij(0);
	for(int i=1;i<=n;i++) printf("%lld ",dis[i]);
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章