-
Notifications
You must be signed in to change notification settings - Fork 2
/
cheer.cpp
65 lines (57 loc) · 1.32 KB
/
cheer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <cstdio>
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <utility>
#include <algorithm>
#define MAXN 10100
#define A first
#define B second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<pii>::iterator vi;
int paths,pastures;
int talk[MAXN];
vector<pii> neigh[MAXN];
bool vis[MAXN];
ll cost = 0;
priority_queue<pll,vector<pll>,greater<pll> > q;
int main(){
ifstream fin ("cheer.in");
ofstream fout ("cheer.out");
fin >> pastures >> paths;
int min = 99999;
int index = -1;
for(int x = 0;x<pastures;x++){
fin >> talk[x];
if(talk[x]<min){
index = x;
min = talk[x];
}
}
for(int x = 0;x<paths;x++){
int a,b,c;
fin >> a >> b >> c;
a--;
b--;
neigh[a].push_back(pii(c,b));
neigh[b].push_back(pii(c,a));
}
q.push(pll(talk[index],index));
while(!q.empty()){
pll NOW = q.top();
q.pop();
ll cur = NOW.B;
if(vis[cur]) continue;
cost+=NOW.A;
vis[cur] = true;
for(vi it = neigh[cur].begin();it!=neigh[cur].end();++it){
q.push(pll((*it).A+(*it).A+talk[cur]+talk[(*it).B],(*it).B));
}
}
fout << cost << "\n";
return 0;
}