#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
#define PB push_back
#define F first
#define S second
#define INF 1000000001
int n,m,dp[100005],l[100005],r[100005];
vector<P>g[100005];
bool vis[100005];
void dfs1(int v,int p){
vis[v]=true;
dp[v]=0;
for(int i=0;i<g[v].size();i++){
int u=g[v][i].F,c=g[v][i].S;
if(u==p)continue;
dfs1(u,v);
dp[v]=max(dp[v],dp[u]+c);
}
}
int dfs2(int v,int p,int x){
for(int i=0;i<g[v].size();i++)l[i]=0,r[i]=0;
for(int i=0;i<g[v].size();i++){
int u=g[v][i].F,c=g[v][i].S;
if(u==p){
dp[v]=max(dp[v],x+c);
l[i]=x+c,r[i]=x+c;
}else l[i]=dp[u]+c,r[i]=dp[u]+c;
}
int res=dp[v];
for(int i=1;i<g[v].size();i++)l[i]=max(l[i],l[i-1]);
for(int i=g[v].size()-1;i>0;i--)r[i-1]=max(r[i-1],r[i]);
for(int i=0;i<g[v].size();i++){
int u=g[v][i].F;
if(u==p)continue;
int c=0;
if(i>0)c=max(c,l[i-1]);
if(i+1<g[v].size())c=max(c,r[i+1]);
res=min(res,dfs2(u,v,c));
}
return res;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
n=N,m=M;
for(int i=0;i<m;i++){
g[A[i]].PB(P(B[i],T[i]));
g[B[i]].PB(P(A[i],T[i]));
}
vector<int>p;
for(int i=0;i<n;i++){
if(!vis[i]){
dfs1(i,-1);
p.PB(dfs2(i,-1,0));
}
}
sort(p.begin(),p.end(),greater<int>());
for(int i=1;i<p.size();i++)p[i]+=L;
p.PB(0);
sort(p.begin(),p.end(),greater<int>());
return p[0]+p[1];
}
Compilation message
dreaming.cpp: In function 'void dfs1(int, int)':
dreaming.cpp:15:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[v].size();i++){
~^~~~~~~~~~~~
dreaming.cpp: In function 'int dfs2(int, int, int)':
dreaming.cpp:23:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[v].size();i++)l[i]=0,r[i]=0;
~^~~~~~~~~~~~
dreaming.cpp:24:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[v].size();i++){
~^~~~~~~~~~~~
dreaming.cpp:32:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1;i<g[v].size();i++)l[i]=max(l[i],l[i-1]);
~^~~~~~~~~~~~
dreaming.cpp:34:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[v].size();i++){
~^~~~~~~~~~~~
dreaming.cpp:39:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(i+1<g[v].size())c=max(c,r[i+1]);
~~~^~~~~~~~~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:58:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1;i<p.size();i++)p[i]+=L;
~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
66 ms |
12280 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
66 ms |
12280 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
66 ms |
12280 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
6324 KB |
Output is correct |
2 |
Correct |
31 ms |
6264 KB |
Output is correct |
3 |
Correct |
33 ms |
6264 KB |
Output is correct |
4 |
Correct |
31 ms |
6264 KB |
Output is correct |
5 |
Correct |
32 ms |
6280 KB |
Output is correct |
6 |
Correct |
33 ms |
6776 KB |
Output is correct |
7 |
Correct |
32 ms |
6484 KB |
Output is correct |
8 |
Correct |
31 ms |
6264 KB |
Output is correct |
9 |
Correct |
32 ms |
6264 KB |
Output is correct |
10 |
Correct |
32 ms |
6392 KB |
Output is correct |
11 |
Correct |
4 ms |
2652 KB |
Output is correct |
12 |
Correct |
10 ms |
3824 KB |
Output is correct |
13 |
Correct |
9 ms |
3956 KB |
Output is correct |
14 |
Correct |
9 ms |
3828 KB |
Output is correct |
15 |
Correct |
9 ms |
3832 KB |
Output is correct |
16 |
Correct |
9 ms |
3828 KB |
Output is correct |
17 |
Correct |
9 ms |
3828 KB |
Output is correct |
18 |
Correct |
10 ms |
3832 KB |
Output is correct |
19 |
Correct |
9 ms |
3828 KB |
Output is correct |
20 |
Correct |
4 ms |
2684 KB |
Output is correct |
21 |
Correct |
4 ms |
2680 KB |
Output is correct |
22 |
Correct |
4 ms |
2808 KB |
Output is correct |
23 |
Correct |
9 ms |
3828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
66 ms |
12280 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
66 ms |
12280 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |