-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lightoj 1019 - Brush (V).cpp
73 lines (69 loc) · 1.91 KB
/
Lightoj 1019 - Brush (V).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
66
67
68
69
70
71
72
73
#include<bits/stdc++.h>
#define ll long long int
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define MOD 1000000007
#define vi vector<int>
#define vll vector<ll>
#define pll pair<ll,ll>
#define pii pair<int,int>
#define all(p) p.begin(),p.end()
#define mid(s,e) (s+(e-s)/2)
#define eb emplace_back
#define ull unsigned long long
#define KAMEHAMEHA ios_base::sync_with_stdio(0);
#define RASENGAN ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int n,m;
const int INF=1e9;
int dijktra(vector< vector< pii > > graph)
{
vi dist(n+1,INF);
dist[1]=0;
priority_queue< pii,vector<pii>,greater< pii > > pq;
pq.push(make_pair(0,1));
while(!pq.empty())
{
int node=pq.top().S;
pq.pop();
for(auto x: graph[node])
{
if(dist[x.F]>dist[node]+x.S)
{
dist[x.F]=dist[node]+x.S;
pq.push(mp(dist[x.F],x.F));
}
}
}
return dist[n];
}
int main()
{
KAMEHAMEHA
// #ifdef _soumik
// freopen("input.txt", "r", stdin);
// #endif
int t;
cin>>t;
for(int cn=1;cn<=t;cn++)
{
cin>>n>>m;
vector< vector< pii > > graph(n+1);
for(int i=0;i<m;i++)
{
int u,v,w;
cin>>u>>v>>w;
graph[u].eb(v,w);
graph[v].eb(u,w);
}
int ans=dijktra(graph);
cout<<"Case "<<cn<<": ";
if(ans!=INF)
cout<<ans<<endl;
else
cout<<"Impossible"<<endl;
}
return 0;
}