-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLightOj 1019.cpp
102 lines (99 loc) · 2.01 KB
/
LightOj 1019.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include<cstdio>
#include<vector>
#include<map>
#include<queue>
#include<utility>
using namespace std;
#define INF 100000009
#define MAX 109
vector<int>take(MAX);
vector<int>list[MAX];
map<pair<int,int>,int>maps;
class my
{
public:
int nod,wight;
};
bool operator<(my a,my b)
{
if(a.wight<b.wight) return a.wight>b.wight;
}
void clr(int m)
{
maps.clear();
int i;
for(i=1;i<=m;i++)
{
take[i]=INF;
list[i].clear();
}
}
void input(int n)
{
int i,j;
int a,b,w;
for(i=1;i<=n;i++)
{
scanf("%d %d",&a,&b);
scanf("%d",&w);
if(maps[make_pair(a,b)]==0)
{
maps[make_pair(a,b)]=w;
maps[make_pair(b,a)]=w;
list[a].push_back(b);
list[b].push_back(a);
}
else if(maps[make_pair(a,b)]>w){
maps[make_pair(a,b)]=w;
maps[make_pair(b,a)]=w;
}
}
}
void dijkstra(int dis)
{
int i,j,x_n,y_w,a_n,b_w;
my le_me;
le_me.nod=1;
le_me.wight=0;
priority_queue<my>q;
q.push(le_me);
take[1]=0;
while(!q.empty())
{
x_n=q.top().nod;
y_w=q.top().wight;
q.pop();
for(i=0;i<list[x_n].size();i++)
{
b_w=take[x_n]+maps[make_pair(x_n,list[x_n][i])];
//printf("%d=%d b_w=%d\n",list[x_n][i],take[list[x_n][i]],b_w);
if(take[list[x_n][i]]>b_w){
take[list[x_n][i]]=b_w;
if(list[x_n][i]!=dis){
le_me.nod=list[x_n][i];
le_me.wight=take[list[x_n][i]];
q.push(le_me);
}
}
}
}
}
int main()
{
int i,j,m,n,t,tcase;
while(scanf("%d",&t)==1)
{
tcase=1;
while(t--)
{
scanf("%d %d",&m,&n);
clr(m);
input(n);
dijkstra(m);
printf("Case %d: ",tcase++);
if(take[m]!=INF) printf("%d\n",take[m]);
else printf("Impossible\n");
}
}
return 0;
}