-
Notifications
You must be signed in to change notification settings - Fork 2
/
11374.cpp
232 lines (214 loc) · 5.99 KB
/
11374.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//BISMILLAHIRRAHMANIRRAHIM
/*
manus tar shopner soman boro
Author :: Shakil Ahmed
.............AUST_CSE27.........
prob ::
Type ::
verdict::
*/
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define pi acos(-1.0)
#define ff first
#define ss second
#define re return
#define QI queue<int>
#define SI stack<int>
#define SZ(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define sqr(x) ((x) * (x))
#define ms(a,b) memset((a),(b),sizeof(a))
#define G() getchar()
#define MAX3(a,b,c) max(a,max(b,c))
#define II ( { int a ; read(a) ; a; } )
#define LL ( { Long a ; read(a) ; a; } )
#define DD ({double a; scanf("%lf", &a); a;})
double const EPS=3e-8;
using namespace std;
#define FI freopen ("input.txt", "r", stdin)
#define FO freopen ("output.txt", "w", stdout)
typedef long long Long;
typedef long long int64;
typedef unsigned long long ull;
typedef vector<int> vi ;
typedef set<int> si;
typedef vector<Long>vl;
typedef pair<int,int>pii;
typedef pair<string,int>psi;
typedef pair<Long,Long>pll;
typedef pair<double,double>pdd;
typedef vector<pii> vpii;
// For loop
#define forab(i, a, b) for (__typeof (b) i = (a) ; i <= b ; ++i)
#define rep(i, n) forab (i, 0, (n) - 1)
#define For(i, n) forab (i, 1, n)
#define rofba(i, a, b) for (__typeof (b)i = (b) ; i >= a ; --i)
#define per(i, n) rofba (i, 0, (n) - 1)
#define rof(i, n) rofba (i, 1, n)
#define forstl(i, s) for (__typeof ((s).end ()) i = (s).begin (); i != (s).end (); ++i)
template< class T > T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }
template< class T > T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
//Fast Reader
template<class T>inline bool read(T &x){int c=getchar();int sgn=1;while(~c&&c<'0'||c>'9'){if(c=='-')sgn=-1;c=getchar();}for(x=0;~c&&'0'<=c&&c<='9';c=getchar())x=x*10+c-'0'; x*=sgn; return ~c;}
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction
//int dx[]={2,1,-1,-2,-1,1};int dy[]={0,1,1,0,-1,-1}; //Hexagonal Direction
/* ************************************** My code start here ****************************************** */
const int MX = 505 ;
const int INF = 1<<29 ;
int dis[2][MX] , par[2][MX] , deg[MX] , adj[MX][MX*MX+10] , cost[MX][MX] , vis[MX];
int n ,s , t , m , k ;
void ini()
{
rep(i,n+2)
{
deg[i] = 0 ;
par[0][i] = par[1][i] = -1 ;
dis[0][i] = dis[1][i] = INF ;
rep(j,n+2) cost[i][j] = INF ;
cost[i][i] = 0 ;
}
}
void input()
{
m = II;
rep(i,m)
{
int x = II ;
int y = II ;
int c = II ;
cost[x][y] = cost[y][x] = min(cost[x][y],c);
adj[x][deg[x]++] = y ;
adj[y][deg[y]++] = x ;
}
}
void dijkstra( int s , int idx , int e)
{
// printf("start\n");
priority_queue < pii > pq ;
pq.push(mp(0,s));
ms(vis,0);
dis[idx][s] = 0 ;
while(!pq.empty())
{
pii pr = pq.top();
pq.pop();
int c = -1 * pr.ff ;
int u = pr.ss ;
// printf("u :: %d c:: %d\n",u,c);
if( u == e ) break ;
//vis[u] = 1 ;
rep(i,deg[u])
{
int nx = adj[u][i];
// if( vis[nx] ) continue ;
int nc = cost[u][nx];
if( dis[idx][nx] > nc + c )
{
dis[idx][nx] = nc + c ;
par[idx][nx] = u ;
pq.push(mp(-(nc+c),nx));
}
}
}
// printf("end\n");
}
int main()
{
// I will always use scanf and printf
// May be i won't be a good programmer but i will be a good human being
bool sp = 0 ;
while( scanf("%d %d %d",&n,&s,&t)==3)
{
if(sp) puts("");
sp = 1 ;
ini();
input();
dijkstra(s,0,t);
dijkstra(t,1,s);
k = II ;
int ans = dis[0][t];
int x , y , z , tag1 = -1 , tag2 = -1 ;
rep(i,k)
{
x = II , y = II , z = II ;
// printf("x::%d y :: %d dis[0][x]:: %d dis[1][y]::%d z :: %d\n",x , y , dis[0][x],dis[1][y],z);
if( dis[0][x] < INF && dis[1][y] < INF && dis[0][x] + dis[1][y] + z < ans )
{
tag1 = x ;
tag2 = y ;
ans = dis[0][x] + dis[1][y] + z ;
}
if( dis[0][y] < INF && dis[1][x] < INF && dis[0][y] + dis[1][x] + z < ans )
{
tag1 = y ;
tag2 = x ;
ans = dis[0][y] + dis[1][x] + z ;
}
}
vector < int > pres ;
if( tag1 == -1 ) // first one is good
{
// stack < int > st ;
//printf("here tag1 \n");
int cur = t ;
while( par[0][cur] != -1 )
{
pres.pb(cur);
cur = par[0][cur];
//if( par[0][cur] == -1 ) break ;
}
pres.pb(cur);
}
else
{
int cur = tag2 ;
// printf(" tag2 :: %d par[1][tag2]::%d\n",tag2,par[1][tag2]);
while( par[1][cur] != -1 )
{
pres.pb(cur);
cur = par[1][cur];
// printf("cur :: %d\n",cur);
// break ;
}
pres.pb(cur);
reverse(pres.begin(),pres.end());
cur = tag1 ;
while(par[0][cur] != -1 )
{
pres.pb(cur);
cur = par[0][cur];
// printf("cur :: %d\n",cur);
// if( par[0][cur] == -1 ) break ;
}
pres.pb(cur);
}
reverse(pres.begin(),pres.end());
int sz = pres.size();
for ( int i = 0 ; i < sz ; i++ )
{
if(i) printf(" ");
printf("%d",pres[i]);
}
puts("");
if( tag1 == -1 )
{
printf("Ticket Not Used\n");
}
else printf("%d\n",tag1);
printf("%d\n",ans);
}
return 0;
}
/*
2
17
9 3 5 2 6 6 3 0 8 6 0 4 4 8 7 2 1
1 2 3 3 3 1 2 1 1 3 1 3 3 2 1 3 1
12
4 6 9 0 2 7 3 0 9 6 8 1
2 3 1 1 1 3 2 1 3 3 2 3
*/