#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,int> ii;
typedef vector<int> vi;
const int MAX_N=3e5+10;
const ll MAX=1e15;
ll ma[MAX_N];
ll dis[MAX_N];
vector<vi> G;
vector<ll> a,b,w;
int n,m;
bool pri[MAX_N];
bool bri[MAX_N],vis[MAX_N];
int num[MAX_N],low[MAX_N];
int it=0;
int otro(int id,int x){
if(a[id]==x) return b[id];
else return a[id];
}
void bridge(int u,int p){
num[u]=low[u]=it++;
vis[u]=1;
for(auto &v:G[u]){
int y=otro(v,u);
if(!vis[y]){
bridge(y,u);
if(low[y]>num[u]){
bri[v]=1;
}
low[u]=min(low[u],low[y]);
}
else
if(y!=p)
low[u]=min(low[u],num[y]);
}
}
vector<ii> bfs(int u){
for(int i=0;i<n;i++) dis[i]=MAX;
dis[u]=0;
priority_queue<ii, vector<ii>, greater<ii> > pq;
pq.push(ii(0,u));
while(!pq.empty()){
int x=pq.top().second;
int k=pq.top().first;
pq.pop();
for(auto &v:G[x]){
int y;
if(x==a[v]) y=b[v];
else y=a[v];
if(dis[y]>dis[x]+w[v]){
dis[y]=dis[x]+w[v];
pq.push(ii(dis[y],y));
}
}
}
memset(pri,0,sizeof pri);
vector<ii> path;
int st,en;
if(u==0){
en=0; st=n-1;
}
else{
st=0; en=n-1;
}
u=st;
while(u!=en){
int id=-1;
ll mi=MAX;
for(auto &v:G[u]){
int y;
if(u==a[v]) y=b[v];
else y=a[v];
if(mi>dis[y]){
id=v; mi=dis[y];
}
}
if(u==a[id]) u=b[id];
else u=a[id];
path.push_back(ii(u,id));
}
reverse(path.begin(),path.end());
return path;
}
int main(){
cin>>n>>m;
G.resize(n+1);
bool sw=0;
for(int i=0;i<m;i++){
int x,y,z; cin>>x>>y>>z;
x--; y--;
if(z!=1) sw=1;
a.push_back(x);
b.push_back(y);
w.push_back(z);
G[x].push_back(i);
G[y].push_back(i);
}
ma[m]=0;
for(int i=m-1;i>=0;i--)
ma[i]=max(ma[i+1],w[i+1]);
ll res=0;
vector<ii> path=bfs(0);
if(m==n-1){
for(int i=0;i<path.size();i++){
int id=path[i].second;
if(id==m-1) continue;
ll rp=dis[n-1]+ma[id];
res=max(res,rp);
}
}
else{
if(m==n || !sw){
if(!sw){
for(int i=0;i<path.size();i++){
if(path[i].second!=m-1)
bri[path[i].second]=1;
}
ll d[MAX_N];
for(int i=0;i<n;i++) d[i]=MAX;
d[0]=0;
queue<int> q;
q.push(0);
while(!q.empty()){
int x=q.front();
q.pop();
for(auto &v:G[x]){
if(bri[v]) continue;
int y=otro(v,x);
if(d[y]>d[x]+1){
d[y]=d[x]+1;
q.push(y);
}
}
}
res=dis[n-1];
if(dis[n-1]!=d[n-1]) res++;
}
else{
bridge(0,0);
bool us[MAX_N];
for(int i=0;i<path.size();i++){
int id=path[i].second;
us[id]=1;
}
ll s1,s0;
s1=s0=0;
for(int i=0;i<m;i++){
if(!bri[i]){
if(us[i]) s1+=w[i];
else s0+=w[i];
}
}
res=dis[n-1];
for(int i=0;i<m-1;i++){
if(us[i]){
if(bri[i]){
res=max(res,dis[n-1]+ma[i]);
}
else{
ll rp=min(dis[n-1]-s1+s0,dis[n-1]+ma[i]);
res=max(res,rp);
}
}
}
}
}
else{
for(int i=0;i<m-1;i++){
w[i]+=ma[i];
bfs(0);
res=max(res,dis[n-1]);
w[i]-=ma[i];
}
}
}
cout<<res<<endl;
}
Compilation message
Aesthetic.cpp: In function 'std::vector<std::pair<long long int, int> > bfs(int)':
Aesthetic.cpp:45:9: warning: unused variable 'k' [-Wunused-variable]
45 | int k=pq.top().first;
| ^
Aesthetic.cpp: In function 'int main()':
Aesthetic.cpp:105:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | for(int i=0;i<path.size();i++){
| ~^~~~~~~~~~~~
Aesthetic.cpp:115:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
115 | for(int i=0;i<path.size();i++){
| ~^~~~~~~~~~~~
Aesthetic.cpp:142:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
142 | for(int i=0;i<path.size();i++){
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2892 KB |
Output is correct |
2 |
Correct |
3 ms |
2892 KB |
Output is correct |
3 |
Correct |
2 ms |
2892 KB |
Output is correct |
4 |
Correct |
3 ms |
2896 KB |
Output is correct |
5 |
Correct |
2 ms |
2892 KB |
Output is correct |
6 |
Correct |
3 ms |
2892 KB |
Output is correct |
7 |
Correct |
2 ms |
2892 KB |
Output is correct |
8 |
Correct |
2 ms |
2892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2892 KB |
Output is correct |
2 |
Correct |
3 ms |
2892 KB |
Output is correct |
3 |
Correct |
2 ms |
2892 KB |
Output is correct |
4 |
Correct |
3 ms |
2896 KB |
Output is correct |
5 |
Correct |
2 ms |
2892 KB |
Output is correct |
6 |
Correct |
3 ms |
2892 KB |
Output is correct |
7 |
Correct |
2 ms |
2892 KB |
Output is correct |
8 |
Correct |
2 ms |
2892 KB |
Output is correct |
9 |
Correct |
368 ms |
3148 KB |
Output is correct |
10 |
Correct |
373 ms |
3148 KB |
Output is correct |
11 |
Correct |
277 ms |
3128 KB |
Output is correct |
12 |
Correct |
255 ms |
3148 KB |
Output is correct |
13 |
Correct |
177 ms |
3104 KB |
Output is correct |
14 |
Correct |
169 ms |
3096 KB |
Output is correct |
15 |
Correct |
176 ms |
3100 KB |
Output is correct |
16 |
Correct |
197 ms |
3104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
559 ms |
34592 KB |
Output is correct |
2 |
Correct |
539 ms |
34692 KB |
Output is correct |
3 |
Correct |
535 ms |
34344 KB |
Output is correct |
4 |
Correct |
512 ms |
34340 KB |
Output is correct |
5 |
Correct |
516 ms |
34516 KB |
Output is correct |
6 |
Correct |
542 ms |
35076 KB |
Output is correct |
7 |
Correct |
511 ms |
34936 KB |
Output is correct |
8 |
Correct |
518 ms |
35128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
582 ms |
38212 KB |
Output is correct |
2 |
Correct |
604 ms |
40400 KB |
Output is correct |
3 |
Correct |
586 ms |
40328 KB |
Output is correct |
4 |
Correct |
603 ms |
40976 KB |
Output is correct |
5 |
Correct |
576 ms |
40324 KB |
Output is correct |
6 |
Correct |
568 ms |
40456 KB |
Output is correct |
7 |
Correct |
577 ms |
40248 KB |
Output is correct |
8 |
Correct |
587 ms |
40608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
446 ms |
31880 KB |
Output is correct |
2 |
Correct |
293 ms |
33636 KB |
Output is correct |
3 |
Correct |
307 ms |
25000 KB |
Output is correct |
4 |
Correct |
289 ms |
25004 KB |
Output is correct |
5 |
Correct |
294 ms |
25136 KB |
Output is correct |
6 |
Correct |
289 ms |
24980 KB |
Output is correct |
7 |
Correct |
301 ms |
25092 KB |
Output is correct |
8 |
Correct |
291 ms |
24984 KB |
Output is correct |
9 |
Correct |
292 ms |
25252 KB |
Output is correct |
10 |
Correct |
294 ms |
25220 KB |
Output is correct |
11 |
Correct |
291 ms |
25036 KB |
Output is correct |
12 |
Correct |
433 ms |
32196 KB |
Output is correct |
13 |
Correct |
310 ms |
24988 KB |
Output is correct |
14 |
Correct |
241 ms |
33044 KB |
Output is correct |
15 |
Correct |
254 ms |
33120 KB |
Output is correct |
16 |
Correct |
424 ms |
32204 KB |
Output is correct |
17 |
Correct |
428 ms |
31504 KB |
Output is correct |
18 |
Correct |
416 ms |
31748 KB |
Output is correct |
19 |
Correct |
290 ms |
33796 KB |
Output is correct |
20 |
Correct |
291 ms |
33784 KB |
Output is correct |
21 |
Correct |
280 ms |
33796 KB |
Output is correct |
22 |
Correct |
279 ms |
33796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
446 ms |
31880 KB |
Output is correct |
2 |
Correct |
293 ms |
33636 KB |
Output is correct |
3 |
Correct |
307 ms |
25000 KB |
Output is correct |
4 |
Correct |
289 ms |
25004 KB |
Output is correct |
5 |
Correct |
294 ms |
25136 KB |
Output is correct |
6 |
Correct |
289 ms |
24980 KB |
Output is correct |
7 |
Correct |
301 ms |
25092 KB |
Output is correct |
8 |
Correct |
291 ms |
24984 KB |
Output is correct |
9 |
Correct |
292 ms |
25252 KB |
Output is correct |
10 |
Correct |
294 ms |
25220 KB |
Output is correct |
11 |
Correct |
291 ms |
25036 KB |
Output is correct |
12 |
Correct |
433 ms |
32196 KB |
Output is correct |
13 |
Correct |
310 ms |
24988 KB |
Output is correct |
14 |
Correct |
241 ms |
33044 KB |
Output is correct |
15 |
Correct |
254 ms |
33120 KB |
Output is correct |
16 |
Correct |
424 ms |
32204 KB |
Output is correct |
17 |
Correct |
428 ms |
31504 KB |
Output is correct |
18 |
Correct |
416 ms |
31748 KB |
Output is correct |
19 |
Correct |
290 ms |
33796 KB |
Output is correct |
20 |
Correct |
291 ms |
33784 KB |
Output is correct |
21 |
Correct |
280 ms |
33796 KB |
Output is correct |
22 |
Correct |
279 ms |
33796 KB |
Output is correct |
23 |
Runtime error |
1321 ms |
1048584 KB |
Execution killed with signal 9 |
24 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2892 KB |
Output is correct |
2 |
Correct |
3 ms |
2892 KB |
Output is correct |
3 |
Correct |
2 ms |
2892 KB |
Output is correct |
4 |
Correct |
3 ms |
2896 KB |
Output is correct |
5 |
Correct |
2 ms |
2892 KB |
Output is correct |
6 |
Correct |
3 ms |
2892 KB |
Output is correct |
7 |
Correct |
2 ms |
2892 KB |
Output is correct |
8 |
Correct |
2 ms |
2892 KB |
Output is correct |
9 |
Correct |
368 ms |
3148 KB |
Output is correct |
10 |
Correct |
373 ms |
3148 KB |
Output is correct |
11 |
Correct |
277 ms |
3128 KB |
Output is correct |
12 |
Correct |
255 ms |
3148 KB |
Output is correct |
13 |
Correct |
177 ms |
3104 KB |
Output is correct |
14 |
Correct |
169 ms |
3096 KB |
Output is correct |
15 |
Correct |
176 ms |
3100 KB |
Output is correct |
16 |
Correct |
197 ms |
3104 KB |
Output is correct |
17 |
Correct |
559 ms |
34592 KB |
Output is correct |
18 |
Correct |
539 ms |
34692 KB |
Output is correct |
19 |
Correct |
535 ms |
34344 KB |
Output is correct |
20 |
Correct |
512 ms |
34340 KB |
Output is correct |
21 |
Correct |
516 ms |
34516 KB |
Output is correct |
22 |
Correct |
542 ms |
35076 KB |
Output is correct |
23 |
Correct |
511 ms |
34936 KB |
Output is correct |
24 |
Correct |
518 ms |
35128 KB |
Output is correct |
25 |
Correct |
582 ms |
38212 KB |
Output is correct |
26 |
Correct |
604 ms |
40400 KB |
Output is correct |
27 |
Correct |
586 ms |
40328 KB |
Output is correct |
28 |
Correct |
603 ms |
40976 KB |
Output is correct |
29 |
Correct |
576 ms |
40324 KB |
Output is correct |
30 |
Correct |
568 ms |
40456 KB |
Output is correct |
31 |
Correct |
577 ms |
40248 KB |
Output is correct |
32 |
Correct |
587 ms |
40608 KB |
Output is correct |
33 |
Correct |
446 ms |
31880 KB |
Output is correct |
34 |
Correct |
293 ms |
33636 KB |
Output is correct |
35 |
Correct |
307 ms |
25000 KB |
Output is correct |
36 |
Correct |
289 ms |
25004 KB |
Output is correct |
37 |
Correct |
294 ms |
25136 KB |
Output is correct |
38 |
Correct |
289 ms |
24980 KB |
Output is correct |
39 |
Correct |
301 ms |
25092 KB |
Output is correct |
40 |
Correct |
291 ms |
24984 KB |
Output is correct |
41 |
Correct |
292 ms |
25252 KB |
Output is correct |
42 |
Correct |
294 ms |
25220 KB |
Output is correct |
43 |
Correct |
291 ms |
25036 KB |
Output is correct |
44 |
Correct |
433 ms |
32196 KB |
Output is correct |
45 |
Correct |
310 ms |
24988 KB |
Output is correct |
46 |
Correct |
241 ms |
33044 KB |
Output is correct |
47 |
Correct |
254 ms |
33120 KB |
Output is correct |
48 |
Correct |
424 ms |
32204 KB |
Output is correct |
49 |
Correct |
428 ms |
31504 KB |
Output is correct |
50 |
Correct |
416 ms |
31748 KB |
Output is correct |
51 |
Correct |
290 ms |
33796 KB |
Output is correct |
52 |
Correct |
291 ms |
33784 KB |
Output is correct |
53 |
Correct |
280 ms |
33796 KB |
Output is correct |
54 |
Correct |
279 ms |
33796 KB |
Output is correct |
55 |
Runtime error |
1321 ms |
1048584 KB |
Execution killed with signal 9 |
56 |
Halted |
0 ms |
0 KB |
- |