# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1182498 | Joon_Yorigami | 꿈 (IOI13_dreaming) | C++20 | 0 ms | 0 KiB |
#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int travelTime(int n, int m, int l, int a[], int b[], int t[]) {
using pii = pair<ll, ll>;
vector<vector<ll>> neighbours;
map<pair<ll,ll>,ll> edgeweight;
vector<vector<ll>>().swap(neighbours);
map<pair<ll,ll>,ll>().swap(edgeweight);
for(int i=0;i<n;i++)
{
neighbours.push_back(vector<ll>());
}
ll x,y,z;
for(int i=0;i<m;i++)
{
x=a[i];
y=b[i];
z=t[i];
neighbours[x].push_back(y);
neighbours[y].push_back(x);
edgeweight[make_pair(min(x,y),max(x,y))]=z;
}
set<ll> seen;
vector<ll> radii;
for(int i=0;i<n;i++)
{
if(seen.find(i)==seen.end())
{
stack<ll> q;
vector<ll> path(n,-1);
vector<ll> weight(n,-1);
set<ll> currseen;
ll ep1,ep2,curr,total,currfurthest,currfurthestdistance;
q.push(i);
weight[i]=0;
currfurthest=-1;
currfurthestdistance=-1;
while(!q.empty())
{
curr = q.top();
q.pop();
seen.insert(curr);
for(auto neighbour:neighbours[curr])
{
if(seen.find(neighbour)==seen.end())
{
weight[neighbour]=weight[curr]+edgeweight[make_pair(min(neighbour,curr),max(neighbour,curr))];
q.push(neighbour);
}
}
}
for(int i=0;i<n;i++)
{
if(weight[i]>currfurthestdistance)
{
ep1=i;
currfurthestdistance=weight[i];
}
}
ep2=ep1;
currfurthestdistance=-1;
total=0;
vector<ll>(n,-1).swap(weight);
weight[ep1]=0;
set<ll>().swap(currseen);
stack<ll>({ep1}).swap(q);
while(!q.empty())
{
curr = q.top();
q.pop();
currseen.insert(curr);
for(auto neighbour:neighbours[curr])
{
if(currseen.find(neighbour)==currseen.end())
{
path[neighbour]=curr;
weight[neighbour]=weight[curr]+edgeweight[make_pair(min(neighbour,curr),max(neighbour,curr))];
q.push(neighbour);
}
}
}
for(int i=0;i<n;i++)
{
if(weight[i]>currfurthestdistance)
{
ep2=i;
currfurthestdistance=weight[i];
}
}
ll x,y,a,b;
x=max(weight[ep1],weight[ep2]-weight[ep1]);
y=min(weight[ep1],weight[ep2]-weight[ep1]);
ll p1=ep2;
while(p1!=ep1)
{
a=max(weight[p1],weight[ep2]-weight[p1]);
b=min(weight[p1],weight[ep2]-weight[p1]);
if(a<x)
{
x=a;
y=b;
}
p1=path[p1];
}
radii.push_back(x);
radii.push_back(y);
}
}
sort(radii.begin(),radii.end(),greater<long long>());
if(m==n-1)
{
return radii[0]+radii[1];
}
return radii[0]+radii[1]+l;
}
int main()
{
int n,m,l,k;
cin >> n >> m >> l;
int a[n];
int b[n];
int t[n];
for(int i=0;i<n;i++)
{
cin >> k;
a[i] = k;
cin >> k;
b[i] = k;
cin >> k;
t[i] = k;
}
//cout << "owo" << endl;
cout << travelTime(n,m,l,a,b,t) << endl;
}