-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVecinosN2V2
101 lines (84 loc) · 1.9 KB
/
VecinosN2V2
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
#include <bits/stdc++.h>
#define MAX 10000
#define INF -1
using namespace std;
int n,m,x,y,a,b,c;
int d1[MAX], d2[MAX];
bool v1[MAX], v2[MAX];
vector< vector< pair<int,int> > > lista(MAX);
queue< pair<int,int> > Q;
void init(){
for(int i=1;i<=n;i++){
v1[i]=false;
v2[i]=false;
d1[i]=INF;
d2[i]=INF;
}
}
void r1(int act, int ady, int peso){
if(peso + d1[act] > d1[ady]){
d1[ady] = peso + d1[act];
Q.push(make_pair(ady, d1[ady]));
}
}
void dij1(){
int act,ady,peso;
d1[x]=0;
Q.push(make_pair(x,0));
while(!Q.empty()){
act = Q.front().first;
Q.pop();
if(v1[act]) continue;
v1[act] = true;
for(int i=0;i<lista[act].size();i++){
ady = lista[act][i].first;
peso = lista[act][i].second;
if(!v1[ady])
r1(act,ady,peso);
}
}
}
void r2(int act, int ady, int peso){
if(peso + d2[act] > d2[ady]){
d2[ady] = peso + d2[act];
Q.push(make_pair(ady, d2[ady]));
}
}
void dij2(){
int act,ady,peso;
d2[y]=0;
Q.push(make_pair(y,0));
while(!Q.empty()){
act = Q.front().first;
Q.pop();
if(v2[act]) continue;
v2[act] = true;
for(int i=0;i<lista[act].size();i++){
ady = lista[act][i].first;
peso = lista[act][i].second;
if(!v2[ady])
r2(act,ady,peso);
}
}
}
int main(){
ifstream fin("in.txt");
fin >> n >> m >> x >> y;
while(fin >> a >> b >> c){
lista[a].push_back(make_pair(b,c));
lista[b].push_back(make_pair(a,c));
}
init();
dij1();
dij2();
int xr=0,yr=0;
for(int i=1;i<=n;i++){
if(i != x && i != y){
if(d1[i] > d2[i])
xr++;
else if(d2[i] > d1[i])
yr++;
}
}
cout << xr << ' ' << yr << endl;
}