-
Notifications
You must be signed in to change notification settings - Fork 0
/
cstreet.cpp~
59 lines (57 loc) · 1.47 KB
/
cstreet.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
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int arr[1001][1001];
int visited[1001];
class CompareDist
{
public:
bool operator()(pair<int,int>lhs, const pair<int,int> rhs){
return lhs.second > rhs.second;}
};
int main() {
// your code goes here
priority_queue<pair<int,int>,vector<pair<int,int> >,CompareDist> pQ;
int t,price,m,n,a,b,wt,source;
scanf("%d",&t);
while(t--)
{
int sum=0;
scanf("%d",&price);
scanf("%d %d",&n,&m);
for(int i=0;i<m;i++)
{
scanf("%d %d %d",&a,&b,&wt);
arr[a][b] = wt;
arr[b][a] = wt;
}
source=1;
do
{
//printf("hello\n");
//printf("%d source",source);
visited[source]=1;
for(int i=1;i<=n;i++)
{
if(arr[source][i]!=0 && visited[i]==0)
{
pQ.push(make_pair(i,arr[source][i]));
}
}
while(1)
{
source=pQ.top().first;
//printf("%d ---------- SOURCE",source);
if(visited[source]==1)pQ.pop();
else if(visited[source]==0){
sum +=pQ.top().second;
pQ.pop();
break;
}
if(pQ.empty())break;
}
}while(!pQ.empty());
printf("%d",sum*price);
}
return 0;
}