#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<vector<pair<int,int>>>g(3e5+5);
vector<long long>dp2(3e5+5,-1);
vector<long long>dp(3e5+5,-1);
signed main()
{
int n,m;
cin >> n>>m;
// cin >> n >> m;
int flag = 0;
vector<pair<int,int>>e(m);
for(int i = 1;i<=m;i++)
{
int a,b,w;
cin >> a >> b >> w;
// if((a==1 && b ==n ) || (a==n && b ==1))flag = 1;
//cout << 'o';
g[a].push_back({b,i});
g[b].push_back({a,i});
e[i-1] = {a,b};
//cout << 'o';
}
//dfs();
dp[1] =0;
queue<int>q;
q.push(1);
while(!q.empty())
{
int v = q.front();
q.pop();
for(auto u : g[v])
{
if(dp[u.first] == -1)
{
q.push(u.first);
dp[u.first] = dp[v] + 1;
}
}
}
dp2[n] =0;
//queue<int>q;
q.push(n);
while(!q.empty())
{
int v = q.front();
q.pop();
for(auto u : g[v])
{
if(dp2[u.first] == -1)
{
q.push(u.first);
dp2[u.first] = dp2[v] + 1;
}
}
}
int mn = 1e9+5;
int num = 0;
int mn2 = 1e9+5;
int k = 0;
bool s =0;
for(auto i : e)
{
k++;
if(k==n)
{
if(dp[i.first] + dp2[i.second]+1 <= mn+mn2+1)
{
mn = dp[i.first];
mn2 = dp2[i.second];
num = 1;
s = 1;
}
}
if(dp[i.first] == mn && dp2[i.second]== mn2)
{
num++;
}
else if(dp[i.first] + dp2[i.second]+1 <mn+mn2+1)
{
mn = dp[i.first];
mn2 = dp2[i.second];
num = 1;
}
// cout << dp[i.first] + dp2[i.second]+1 << '\n';
}
cout << mn+mn2+1 + (num == 1 && !s);
}
Compilation message
Aesthetic.cpp: In function 'int main()':
Aesthetic.cpp:13:7: warning: unused variable 'flag' [-Wunused-variable]
13 | int flag = 0;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
12188 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
12188 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
293 ms |
36460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
275 ms |
36692 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
212 ms |
35384 KB |
Output is correct |
2 |
Incorrect |
179 ms |
33088 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
212 ms |
35384 KB |
Output is correct |
2 |
Incorrect |
179 ms |
33088 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
12188 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |