E - Connections between cities
Crawling in process...
Crawling failed
Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Appoint description:
Description
After World War X, a lot of cities have been seriously damaged, and we need to rebuild those cities. However, some materials needed can only be produced in certain places. So we need to transport these materials from city to city. For most of roads had been totally destroyed during the war, there might be no path between two cities, no circle exists as well.
Now, your task comes. After giving you the condition of the roads, we want to know if there exists a path between any two cities. If the answer is yes, output the shortest path between them.
Now, your task comes. After giving you the condition of the roads, we want to know if there exists a path between any two cities. If the answer is yes, output the shortest path between them.
Input
Input consists of multiple problem instances.For each instance, first line contains three integers n, m and c, 2<=n<=10000, 0<=m<10000, 1<=c<=1000000. n represents the number of cities numbered from 1 to n. Following m lines, each line has three integers i, j and k, represent a road between city i and city j, with length k. Last c lines, two integers i, j each line, indicates a query of city i and city j.
Output
For each problem instance, one line for each query. If no path between two cities, output “Not connected”, otherwise output the length of the shortest path between them.
Sample Input
5 3 2
1 3 2
2 4 3
5 2 3
1 4
4 5
Sample Output
Not connected
6
Hint
Hint
Huge input, scanf recommended.
思路:LCA离线
#include<iostream>
#include<cstring>
#include<cstdio>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
using namespace std;
const int nn=1e4+9;
const int mm=2e6+nn+nn;
class Edge
{
public:int v,next,w;
}e[mm];
int head[nn],qhead[nn],edge,ans[mm],dis[nn];
bool vis[nn];
int rt[nn],N,M,C,id[nn];
void data()
{
clr(head,-1);clr(qhead,-1);edge=0;
}
void add(int u,int v,int w,int*h)
{
e[edge].v=v;e[edge].w=w;e[edge].next=h[u];h[u]=edge++;
}
int find(int x)
{
if(rt[x]^x)
rt[x]=find(rt[x]);
return rt[x];
}
void tarjan(int u,int bcc)
{ int v;
id[u]=bcc;
vis[u]=1;rt[u]=u;
for(int i=head[u];~i;i=e[i].next)
{
v=e[i].v;
if(vis[v])continue;
dis[v]=dis[u]+e[i].w;
tarjan(v,bcc);rt[v]=u;
}
for(int i=qhead[u];~i;i=e[i].next)
{
v=e[i].v;
if(!vis[v])continue;
ans[e[i].w]=(id[u]==id[v])?dis[u]+dis[v]-dis[find(v)]*2:-1;
}
}
void getans()
{ clr(vis,0);clr(id,0);
int bcc=0;
FOR(i,1,N)
if(!vis[i])
dis[i]=0,tarjan(i,++bcc);
}
int main()
{ int a,b,c;
while(~scanf("%d%d%d",&N,&M,&C))
{
data();
FOR(i,1,M)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c,head);add(b,a,c,head);
}
FOR(i,1,C)
{
scanf("%d%d",&a,&b);add(a,b,i,qhead);add(b,a,i,qhead);
}
getans();
FOR(i,1,C)
if(ans[i]<0)printf("Not connected\n");
else printf("%d\n",ans[i]);
}
}
#include<iostream>
#include<cstring>
#include<cstdio>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
#define ll(x) (1<<x)
using namespace std;
const int nn=4e4+9;
const int mm=4e4+9;
class Edge
{
public:int v,next,w;
}e[mm];
int head[nn],edge,dis[nn],to[nn],dfs_clock;
int vis[nn],rmq[nn][30];
int rt[nn],N,M,C,bit[nn];
void data()
{
clr(head,-1);edge=0;
}
void add(int u,int v,int w,int*h)
{
e[edge].v=v;e[edge].w=w;e[edge].next=h[u];h[u]=edge++;
}
int find(int x)
{
if(rt[x]^x)
rt[x]=find(rt[x]);
return rt[x];
}
void uni(int a,int b)
{
a=find(a);b=find(b);
rt[a]=b;
}
void dfs(int u,int dep)//一遍欧拉路径
{ int v;
to[dfs_clock]=u;//存欧拉路径
dis[u]=dep;
vis[u]=dfs_clock++;
for(int i=head[u];~i;i=e[i].next)
{
v=e[i].v;
if(vis[v]==-1)
{
dfs(v,dep+e[i].w);
to[dfs_clock++]=u;
}
}
}
void ST(int N)
{
bit[0]=-1;
FOR(i,1,N)bit[i]=(i&(i-1))==0?bit[i-1]+1:bit[i-1];
FOR(i,0,N)
rmq[i][0]=dis[ to[i] ];
FOR(i,1,bit[N])
for(int j=0;j+ll(i)-1<=N;++j)
rmq[j][i]=min(rmq[j][i-1],rmq[j+ll(i-1)][i-1]);
}
int RMQ(int l,int r)
{
int t=bit[r-l+1];
r-=ll(t)-1;
return min(rmq[l][t],rmq[r][t]);
}
int main()
{ int a,b,c;
while(~scanf("%d%d%d",&N,&M,&C))
{
data();clr(vis,-1);
FOR(i,0,N)rt[i]=i;
FOR(i,1,M)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c,head);add(b,a,c,head);
uni(a,b);
}
FOR(i,1,N)///虚点 0 ,虚边得有值,不然查到0不一定是根点
if(rt[i]==i)
add(0,i,1,head),add(i,0,1,head);
dfs_clock=0;
dfs(0,0);
ST(dfs_clock-1);
FOR(i,1,C)
{
scanf("%d%d",&a,&b);
int ta=vis[a];
int tb=vis[b];
if(ta>tb)swap(ta,tb);
int ddd=RMQ(ta,tb);
if(ddd==0)printf("Not connected\n");
else printf("%d\n",dis[a]+dis[b]-2*ddd);
}
}
}
本文深入探讨了信息技术领域的多个细分技术领域,包括前端开发、后端开发、移动开发、游戏开发等,提供了关于大数据开发、开发工具、嵌入式硬件、嵌入式电路知识、嵌入式开发环境、音视频基础、音视频直播流媒体、图像处理AR特效、AI音视频处理、测试、基础运维、DevOps、操作系统、云计算厂商、自然语言处理、区块链、隐私计算、文档协作与知识管理、版本控制、项目管理与协作工具、有监督学习、无监督学习、半监督学习、强化学习、数据安全、数据挖掘、数据结构、算法、非IT技术、自动推理、人工神经网络与计算、自动驾驶、数据分析、数据工程、程序设计方法、数据库理论、代码管理工具等主题。每个部分都详细阐述了其核心概念、关键技术及其应用实例。
1万+

被折叠的 条评论
为什么被折叠?



