Buy a Ticket(超级源点+Dij)
Describe
Musicians of a popular band "Flayer" have announced that they are going to "make their exit" with a world tour. Of course, they will visit Berland as well.
There are n cities in Berland. People can travel between cities using two-directional train routes; there are exactly m routes, i-th route can be used to go from city v i to city u i (and from u i to v i), and it costs w i coins to use this route.
Each city will be visited by "Flayer", and the cost of the concert ticket in i-th city is a i coins.
You have friends in every city of Berland, and they, knowing about your programming skills, asked you to calculate the minimum possible number of coins they have to pay to visit the concert. For every city i you have to compute the minimum number of coins a person from city i has to spend to travel to some city j (or possibly stay in city i), attend a concert there, and return to city i (if j ≠ i).
Formally, for every you have to calculate , where d(i, j) is the minimum number of coins you have to spend to travel from city i to city j. If there is no way to reach city j from city i, then we consider d(i, j) to be infinitely large.
Input
The first line contains two integers n and m (2 ≤ n ≤ 2·105, 1 ≤ m ≤ 2·105).
Then m lines follow, i-th contains three integers v i, u i and w i (1 ≤ v i, u i ≤ n, v i ≠ u i, 1 ≤ w i ≤ 1012) denoting i-th train route. There are no multiple train routes connecting the same pair of cities, that is, for each (v, u) neither extra (v, u) nor (u, v) present in input.
The next line contains n integers a 1, a 2, ... a k (1 ≤ a i ≤ 1012) — price to attend the concert in i-th city.
Output
Print n integers. i-th of them must be equal to the minimum number of coins a person from city i has to spend to travel to some city j (or possibly stay in city i), attend a concert there, and return to city i (if j ≠ i).
Translate
流行乐队“Flayer”将在n个城市开演唱会 这n个城市的人都想去听演唱会 每个城市的票价不同 于是这些人就想是否能去其他城市听演唱会更便宜(去要路费的)
输入格式: 第一行包含两个整数n和m
接下来m行 每行三个数 u v k 表示u城市到v城市要k元
接下来n个数 表每个城市的票价
Input
4 2
1 2 4
2 3 7
6 20 1 25
Output
6 14 1 25
Input
3 3
1 2 1
2 3 1
1 3 1
30 10 20
Output
12 10 12
Solution
这道题代码不难,主要是思路很巧妙,需要建一个超级源点,我们可以让每个节点上的人从这个超级源点出发,这个超级源点与每个节点都连有一条边,边权是该点的值,这样由源点出发到每个节点 i 最短路就相当于是从节点 i 出发到花费最少的一个点并参加音乐会所花费的最小值,当然还没有回到 i ,怎样才能求算上回到 i 的最小值呢,我们将 n 个点之间的边的边权乘 2 跑最短路就行了。
因此处理方法为:
m 个边,边权*2存边;各点权作为一个边权与超级源点建边,从超级源点跑单源最短路,输出 1~n 每个节点的最短路即可。
注意:
全部都是双向边,边数要搞对,maxn*4,别忘了超级源点也和每个边连有双向边。
Code
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
const ll Inf=1e13;
ll dis[maxn];
int n,m,cnt,head[maxn];
bool vis[maxn];
struct Edge{int to,next;ll val;}e[800000+5];
void Add(int x,int y,ll z){
e[++cnt].to=y;
e[cnt].val=z;
e[cnt].next=head[x];
head[x]=cnt;
}
void Dij(int S){
priority_queue<pair<ll,int> > q;
for(int i=1;i<=n;++i)dis[i]=Inf;
dis[S]=0;q.push(make_pair(0,S));
while(!q.empty()){
int x=q.top().second;q.pop();
if(vis[x])continue;
vis[x]=1;
for(int i=head[x];i;i=e[i].next){
int v=e[i].to;ll w=e[i].val;
if(!vis[v]&&dis[v]>dis[x]+w){
dis[v]=dis[x]+w;
q.push(make_pair(-dis[v],v));
}
}
}
}
int main(){
scanf("%d%d",&n,&m);
int x,y;ll z;
for(int i=1;i<=m;++i){
scanf("%d%d%lld",&x,&y,&z);
Add(x,y,(z<<1));Add(y,x,(z<<1));
}
for(int i=1;i<=n;++i){
scanf("%lld",&z);
Add(i,n+1,z);Add(n+1,i,z);
}
Dij(n+1);
for(int i=1;i<=n;++i)
printf("%lld ",dis[i]);
return 0;
}