CF938D Buy a Ticket dijkstra
考试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; }