이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define pii pair<int,int>
#define f first
#define s second
#define ll long long
using namespace std;
const int mxn=1e5+10;
const int inf=1e9+10;
vector<int> adj[mxn];
vector<int> cost[mxn];
int dst[mxn];
vector<int> g;
void dijkstra()
{
priority_queue<pii> pq;
for(int i=0;i<g.size();i++)
{
pq.push({0,g[i]});
dst[g[i]]=0;
}
int vis[mxn]={};
while(!pq.empty())
{
int u=pq.top().s;
pq.pop();
if(vis[u])continue;
vis[u]=1;
for(int i=0;i<adj[u].size();i++)
{
int v=adj[u][i];
if(dst[u]+cost[u][i]<dst[v])
{
dst[v]=dst[u]+cost[u][i];
pq.push({-dst[v],v});
}
}
}
}
int col[mxn]={};
priority_queue<pair<int,pii>> edg;
vector<int> mst[mxn];
vector<int> cst[mxn];
void add(int n)
{
for(int i=1;i<=n;i++)
{
for(int j=0;j<adj[i].size();j++)
{
int v=adj[i][j];
if(i<v)edg.push({min(dst[i],dst[v]),{i,v}});
}
}
}
int parent[mxn];
int findrep(int u)
{
if(parent[u]==u)return u;
return parent[u]=findrep(parent[u]);
}
void _union(int u,int v)
{
parent[findrep(u)]=findrep(v);
}
int pa[22][mxn];
int dp[22][mxn];
int level[mxn]={};
void dfs(int cur,int prev,int v)
{
pa[0][cur]=prev;
dp[0][cur]=v;
level[cur]=level[prev]+1;
for(int i=0;i<mst[cur].size();i++)
{
if(mst[cur][i]==prev)continue;
dfs(mst[cur][i],cur,cst[cur][i]);
}
}
void init_lca(int n)
{
for(int i=1;i<18;i++)
{
for(int j=1;j<=n;j++)
{
if(pa[i-1][j]!=-1)
{
pa[i][j]=pa[i-1][pa[i-1][j]];
dp[i][j]=min(dp[i-1][j],dp[i-1][pa[i-1][j]]);
}
}
}
}
int findlca(int u,int v)
{
if(level[u]<level[v])swap(u,v);
int diff=level[u]-level[v];
for(int i=0;i<18;i++)
if((diff>>i)&1)u=pa[i][u];
if(u==v)return u;
for(int i=17;i>=0;i--)
{
if(pa[i][u]!=pa[i][v])
{
u=pa[i][u];
v=pa[i][v];
}
}
return pa[0][u];
}
int getmin(int u,int v)
{
int ans=inf;
if(level[u]<level[v])swap(u,v);
int diff=level[u]-level[v];
for(int i=0;i<18;i++)
{
if((diff>>i)&1)
{
ans=min(ans,dp[i][u]);
u=pa[i][u];
}
}
return ans;
}
int main()
{
for(int i=1;i<mxn;i++)parent[i]=i;
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++)
{
int u,v,c;
cin>>u>>v>>c;
adj[u].push_back(v);
adj[v].push_back(u);
cost[u].push_back(c);
cost[v].push_back(c);
dst[u]=dst[v]=inf;
}
int k;
cin>>k;
for(int i=0;i<k;i++)
{
int t;
cin>>t;
g.push_back(t);
}
dijkstra();
// cout<<dst[2]<<endl;
add(n);
while(!edg.empty())
{
int w=edg.top().f;
int u=edg.top().s.f;
int v=edg.top().s.s;
edg.pop();
if(findrep(u)==findrep(v))continue;
_union(u,v);
mst[u].push_back(v);
mst[v].push_back(u);
cst[u].push_back(w);
cst[v].push_back(w);
}
dfs(1,0,dst[1]);
init_lca(n);
int q;
cin>>q;
while(q--)
{
int u,v;
cin>>u>>v;
int lca=findlca(u,v);
int ans=min(getmin(u,lca),getmin(v,lca));
//cout<<lca<<endl;
ans=min(ans,dst[u]);
ans=min(ans,dst[v]);
ans=min(ans,dst[lca]);
cout<<ans<<endl;
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
plan.cpp: In function 'void dijkstra()':
plan.cpp:19:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g.size();i++)
~^~~~~~~~~
plan.cpp:35:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<adj[u].size();i++)
~^~~~~~~~~~~~~~
plan.cpp: In function 'void add(int)':
plan.cpp:58:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<adj[i].size();j++)
~^~~~~~~~~~~~~~
plan.cpp: In function 'void dfs(int, int, int)':
plan.cpp:89:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<mst[cur].size();i++)
~^~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |