http://codeforces.com/problemset/problem/938/D
这道题非常的因吹斯听,说的是n个城市,每个城市要举办一场演唱会,然后票价是ai,此外还有m条双向有权边连接各个城市,求从每个城市出发,看一场演唱会再返回的最小花费。
很明显是最短路,然而直接跑Floyd再贪心的话会超时,于是我们先给途中添加一个超级源点,这个点每个城市连一条花费为ai的边,以这个城市为起点用最快的Dijstra加优先队列实现就能过。
#include <bits/stdc++.h>
#include <cstring>
#define ll long long
#define INF 0x3f3f3f3f
#define fi first
#define se second
using namespace std;
const int maxn = 2e5+5;
vector<pair<int,ll> > edge[maxn];
bool vis[maxn];
ll dist[maxn];
void bfs(int n,int st)
{
for(int i = 1;i<=n;i++)
{
dist[i] = (1LL<<60)-1;
vis[i] = 0;
}
priority_queue<pair<ll,int>> q;
dist[st] = 0;
q.push({dist[st],st});
while(!q.empty())
{
int u = q.top().se;
q.pop();
if(vis[u]) continue;
vis[u] = 1;
for(auto pv : edge[u])
{
int v = pv.fi;
ll w = pv.se;
if(!vis[v] && dist[v] > dist[u] + w)
{
dist[v] = dist[u]+w;
q.push({-dist[v],v});
}
}
}
}
int main()
{
int n,m;
cin>>n>>m;
for(int i = 0;i<m;i++)
{
int u,v;
ll w;
cin>>u>>v>>w;
edge[u].push_back({v,2*w});
edge[v].push_back({u,2*w});
}
for(int i = 1;i<=n;i++)
{
ll a;
cin>>a;
edge[n+1].push_back({i,a});
}
bfs(n+1,n+1);
for(int i = 1;i<=n;i++)
{
cout<<dist[i]<<" ";
}
return 0 ;
}