#include <bits/stdc++.h>
#include "dreaming.h"
using namespace std;
vector<vector<pair<int,int> > > adj;
vector<bool> visited,in;
vector<int> cmp,st;
queue<int> q;
queue<pair<int,pair<int,int> > > pq;
int solve(int x,int n)
{
int sz = 0;
q.push(x);
visited[x] = true;
in.assign(n,false);
while(!q.empty())
{
int p = q.front();
q.pop();
sz++;
in[p] = true;
for(int i = 0;i < adj[p].size();i++) if(!visited[adj[p][i].second]){ visited[adj[p][i].second] = true; q.push(adj[p][i].second); }
}
for(int i = 0;i < n;i++) if(in[i] and adj[i].size()<=1) pq.push({0,{i,i}});
if(sz==1) return 0;
while(!pq.empty())
{
int w = pq.front().first,p = pq.front().second.first,pr = pq.front().second.second;
pq.pop();
st[p] = max(st[p],w);
for(int i = 0;i < adj[p].size();i++) if(adj[p][i].second!=pr) pq.push({adj[p][i].first+w,{adj[p][i].second,p}});
}
int ans = INT_MAX;
for(int i = 0;i < n;i++) if(in[i]) ans = min(ans,st[i]);
return ans;
}
int travelTime(int n,int m,int l,int a[],int b[],int t[])
{
adj.resize(n);
visited.resize(n);
st.resize(n);
for(int i = 0;i < m;i++)
{
adj[a[i]].push_back({t[i],b[i]});
adj[b[i]].push_back({t[i],a[i]});
}
for(int i = 0;i < n;i++)
{
if(visited[i]) continue;
cmp.push_back(solve(i,n));
}
sort(cmp.begin(),cmp.end());
reverse(cmp.begin(),cmp.end());
if(cmp.size()==1) return cmp[0];
else if(cmp.size()==2) return cmp[0]+cmp[1]+l;
else return max(cmp[0]+cmp[1]+l,cmp[1]+cmp[2]+l+l);
}
//
//int main()
//{
// int n,m,l;
// cin >> n >> m >> l;
// int a[m],b[m],t[m];
// for(int i = 0;i < m;i++) cin >> a[i] >> b[i] >> t[i];
// cout << travelTime(n,m,l,a,b,t);
//}
Compilation message
dreaming.cpp: In function 'int solve(int, int)':
dreaming.cpp:23:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i < adj[p].size();i++) if(!visited[adj[p][i].second]){ visited[adj[p][i].second] = true; q.push(adj[p][i].second); }
~~^~~~~~~~~~~~~~~
dreaming.cpp:32:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i < adj[p].size();i++) if(adj[p][i].second!=pr) pq.push({adj[p][i].first+w,{adj[p][i].second,p}});
~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
51 ms |
6904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
51 ms |
6904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
51 ms |
6904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1080 ms |
5360 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
51 ms |
6904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
51 ms |
6904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |