码迷,mamicode.com
首页 > 其他好文 > 详细

CF938D Buy a Ticket dijkstra

时间:2019-10-14 14:35:31      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:add   pre   efi   dde   ++   node   continue   str   ace   

考试T1,建一个反图跑一个最短路就好了~

code:

#include <bits/stdc++.h> 
#define ll long long  
#define N 200002 
#define M 600009 
#define setIO(s) freopen(s".in","r",stdin) , freopen(s".out","w",stdout)  
using namespace std; 
int n,m,edges,s,t;         
ll val[M], d[N];     
int hd[N],to[M],nex[M],done[N];        
struct Node 
{
    int u; 
	ll dis;  
    Node(int u=0,ll dis=0):u(u),dis(dis){}  
    bool operator<(Node b) const { return b.dis<dis; }       
};      
priority_queue<Node>q;   
void addedge(int u,int v,ll c) 
{
	nex[++edges]=hd[u],hd[u]=edges,to[edges]=v,val[edges]=c;    
}
void dijkstra() 
{
	memset(d,0x3f,sizeof(d));    
	d[s]=0ll;   
	q.push(Node(s,0ll));   
	while(!q.empty()) 
	{ 
		Node e=q.top(); q.pop();   
		int u=e.u;  
		if(done[u]) continue;  
		done[u]=1;   
		for(int i=hd[u];i;i=nex[i]) 
		{ 
			int v=to[i];    
			if(d[u]+val[i]<d[v]) 
			{
				d[v]=d[u]+val[i];   
				q.push(Node(v, d[v]));    
			}
		}
	}
}
int main() 
{ 
	// setIO("movie"); 
	int i,j;   
	scanf("%d%d",&n,&m);              
	for(i=1;i<=m;++i) 
	{         
		int u,v; 
		ll c; 
		scanf("%d%d%lld",&u,&v,&c);  
		if(u==v) continue;   
		addedge(u,v,1ll*2*c), addedge(v,u,1ll*2*c);    
	}   
	s=0;         
	for(i=1;i<=n;++i)  
	{     
		ll w; 
		scanf("%lld",&w),addedge(s,i,1ll*w);       
	}   
	dijkstra(); 
	for(i=1;i<=n;++i) printf("%lld ",d[i]);         
	return 0;    
}

  

CF938D Buy a Ticket dijkstra

标签:add   pre   efi   dde   ++   node   continue   str   ace   

原文地址:https://www.cnblogs.com/guangheli/p/11671151.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!