#include <stdio.h>
#include <string.h>
#include <queue>
#include <vector>
#include <algorithm>
#include "dreaming.h"
using namespace std;
class e {
public:
int v, w;
e(int v, int w)
:v(v),w(w)
{}
};
vector<e> con[100001];
int n, m, l, d[100001];
bool vis[100001];
int solve(int pre, int u, int up) {
int mx1=up, mx2=-1, v1=-1, v2=-1;
for(int i=0;i<con[u].size();i++) {
int v=con[u][i].v, w=con[u][i].w;
if(pre==v) continue;
if(d[v]+w>=mx1) {
mx2=mx1;
v2=v1;
mx1=d[v]+w;
v1=v;
} else if(d[v]+w>mx2) {
mx2=d[v]+w;
v2=v;
}
}
int ret=mx1;
for(int i=0;i<con[u].size();i++) {
int v=con[u][i].v, w=con[u][i].w;
if(pre==v) continue;
if(v==v1) ret=min(ret,solve(u,v,mx2+w));
else ret=min(ret,solve(u,v,mx1+w));
}
return ret;
}
void dfs(int pre, int u) {
vis[u]=true;
for(int i=0;i<con[u].size();i++) {
int v=con[u][i].v, w=con[u][i].w;
if(pre==v) continue;
dfs(u,v);
d[u]=max(d[u],d[v]+w);
}
}
int dia(int u, bool fst) {
memset(d,-1,sizeof(d));
queue<int> q;
q.push(u);
d[u]=0;
while(!q.empty()) {
int u=q.front();
q.pop();
for(int i=0;i<con[u].size();i++) {
int v=con[u][i].v, w=con[u][i].w;
if(d[v]<0) {
d[v]=d[u]+w;
q.push(v);
}
}
}
int mx=-1, tar;
for(int i=1;i<=n;i++) {
if(d[i]>mx) {
mx=d[i]; tar=i;
}
}
if(fst) return dia(tar,false);
else return mx;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
n=N; m=M; l=L;
for(int i=0;i<m;i++) {
int u, v, w;
u=A[i]; v=B[i]; w=T[i];
u++; v++;
con[u].push_back(e(v,w));
con[v].push_back(e(u,w));
}
if(m==n-1) {
return dia(1,true);
}
vector<int> rt;
for(int i=1;i<=n;i++) if(!vis[i]) {
rt.push_back(i);
dfs(-1,i);
}
vector<int> v;
for(int i=0;i<rt.size();i++)
v.push_back(solve(-1,rt[i],0));
sort(v.begin(),v.end());
for(int i=0;i<v.size()-1;i++) v[i]+=l;
sort(v.begin(),v.end());
return v.back()+v[v.size()-2];
}
Compilation message
dreaming.cpp: In function 'int solve(int, int, int)':
dreaming.cpp:24:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<con[u].size();i++) {
~^~~~~~~~~~~~~~
dreaming.cpp:38:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<con[u].size();i++) {
~^~~~~~~~~~~~~~
dreaming.cpp:23:29: warning: variable 'v2' set but not used [-Wunused-but-set-variable]
int mx1=up, mx2=-1, v1=-1, v2=-1;
^~
dreaming.cpp: In function 'void dfs(int, int)':
dreaming.cpp:49:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<con[u].size();i++) {
~^~~~~~~~~~~~~~
dreaming.cpp: In function 'int dia(int, bool)':
dreaming.cpp:65:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<con[u].size();i++) {
~^~~~~~~~~~~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:101:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<rt.size();i++)
~^~~~~~~~~~
dreaming.cpp:104:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v.size()-1;i++) v[i]+=l;
~^~~~~~~~~~~
dreaming.cpp: In function 'int dia(int, bool)':
dreaming.cpp:79:30: warning: 'tar' may be used uninitialized in this function [-Wmaybe-uninitialized]
if(fst) return dia(tar,false);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
57 ms |
11128 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
57 ms |
11128 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
57 ms |
11128 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
6144 KB |
Output is correct |
2 |
Correct |
28 ms |
6136 KB |
Output is correct |
3 |
Correct |
30 ms |
6136 KB |
Output is correct |
4 |
Correct |
26 ms |
6144 KB |
Output is correct |
5 |
Correct |
26 ms |
6136 KB |
Output is correct |
6 |
Correct |
30 ms |
6644 KB |
Output is correct |
7 |
Correct |
31 ms |
6264 KB |
Output is correct |
8 |
Correct |
27 ms |
6008 KB |
Output is correct |
9 |
Correct |
29 ms |
6136 KB |
Output is correct |
10 |
Correct |
28 ms |
6268 KB |
Output is correct |
11 |
Correct |
3 ms |
2688 KB |
Output is correct |
12 |
Correct |
10 ms |
4336 KB |
Output is correct |
13 |
Correct |
10 ms |
4344 KB |
Output is correct |
14 |
Correct |
9 ms |
4472 KB |
Output is correct |
15 |
Correct |
10 ms |
4344 KB |
Output is correct |
16 |
Correct |
9 ms |
4344 KB |
Output is correct |
17 |
Correct |
8 ms |
4088 KB |
Output is correct |
18 |
Correct |
10 ms |
4344 KB |
Output is correct |
19 |
Correct |
9 ms |
4216 KB |
Output is correct |
20 |
Correct |
4 ms |
3072 KB |
Output is correct |
21 |
Correct |
3 ms |
2688 KB |
Output is correct |
22 |
Correct |
4 ms |
2816 KB |
Output is correct |
23 |
Correct |
10 ms |
4216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
57 ms |
11128 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
57 ms |
11128 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |