Connections between cities

本文探讨了在战后城市重建中如何解决材料运输的问题,通过寻找城市间的最短路径来确保有效运输。使用了图论中的LCA算法和Tarjan算法进行求解,并提供了具体的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Connections between cities

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 896 Accepted Submission(s): 236
 
Problem 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.
 
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.
 
 
Source
2009 Multi-University Training Contest 8 - Host by BJNU
 
Recommend
gaojie
 
/*
用vector炸了,还是用邻接表卡过的
*/
#include<bits/stdc++.h>
using namespace std; #define M 10007 #define N 2222212 int bin[M],dis[M],vis[M],cur[N],root[M]; int s1[N],s2[N],t[N],d[N],p[N]; int n,m,ne,cnt; int find(int x) { while(x!=bin[x]) x=bin[x]; return x; } void add(int u,int v,int w,int h[]) { t[ne]=v,d[ne]=w,p[ne]=h[u],h[u]=ne++; t[ne]=u,d[ne]=w,p[ne]=h[v],h[v]=ne++; } /* LCA算法以某一个节点作为根节点,开始遍历,遍历到一个结点先判断与它相关的结点是不是有已经被访问过的, 如果有的话,判断两个结点是不是在同一棵树上,如果是的话就保留最近公共祖先的距离,如果不是的话,就是不能到达,距离就赋值为-1 */ void LCA(int u) { root[u]=cnt; bin[u]=u; vis[u]=1; //遍历查询树 for(int i=s2[u];i;i=p[i]) { int v=t[i]; if(vis[v]) { if(root[u]==root[v])//在同一棵树下 { int rt=find(v);//最近公共祖先 cur[d[i]]=dis[u]+dis[v]-2*dis[rt]; } else cur[d[i]]=-1; } } //遍历城市图 for(int i=s1[u];i;i=p[i]) { int v=t[i]; if(!vis[v]) { dis[v]=dis[u]+d[i]; LCA(v); bin[v]=u;//路径压缩 } } } int main() { //freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin); int n,m,q,i,j; while(scanf("%d%d%d",&n,&m,&q)!=EOF) { for(i=1,ne=1;i<=n;i++) { s1[i]=s2[i]=vis[i]=cur[i]=root[i]=bin[i]=0; } int u,v,w; for(i=1;i<=m;i++) { scanf("%d%d%d",&u,&v,&w); add(u,v,w,s1); } for(i=1;i<=q;i++) { scanf("%d%d",&u,&v); add(u,v,i,s2); } for(i=1,cnt=1;i<=n;i++,cnt++) if(!vis[i]) { dis[i]=0; LCA(i); } for(i=1;i<=q;i++) if(cur[i]>=0) printf("%d\n",cur[i]); else printf("Not connected\n"); } return 0; }
#include<bits/stdc++.h>
#define N 10005
using namespace std;
int n,m,c;
int bin[N];
int root[N];//表示点i的根节点
int roo;//表示当前是以哪个结点为根节点遍历的
int dis[N];//标记结点i到根结点的距离
int vis[N];//标记i点是否被访问过
int cur[N];//表示第几组解
int op[N][N];//表示解是第几组
struct node
{
    int v,val;
    node(){}
    node(int a,int b)
    {
        v=a;
        val=b;
    }
};
vector<node>edge[N];
vector<node>edg[N];
int findx(int x)
{
    while(x!=bin[x])
        x=bin[x];
    return x;
}
/*
Tarjan算法以某一个节点作为根节点,开始遍历,遍历到一个结点先判断与它相关的结点是不是有已经被访问过的,
如果有的话,判断两个结点是不是在同一棵树上,如果是的话就保留最近公共祖先的距离,如果不是的话,就是不能到达,距离就赋值为-1

*/
int LCA(int u)
{
    //首先遍历查询树
    root[u]=roo;
    vis[u]=1;
    bin[u]=u;
    for(int i=0;i<edg[u].size();i++)
    {
        int nex=edg[u][i].v;
        if(vis[nex]==1)//这个点遍历过了
        {
            if(root[u]==root[nex])
            {
                int rt=findx(nex);
                cur[op[u][nex]]=dis[u]+dis[nex]-2*dis[rt];//
            }
            else
                cur[op[u][nex]]=-1;
        }
    }
    //然后就是遍历程城市树
    for(int i=0;i<edge[u].size();i++)
    {
        int nex=edge[u][i].v;
        if(vis[nex]) continue;
        dis[nex]=dis[u]+edge[u][i].val;//父节点到根节点的距离,加上到父节点的距离
        LCA(nex);
        bin[nex]=u;//路径压缩
    }
}
int x,y,val;
void inti()
{
    for(int i=0;i<=n;i++)
    {
        edge[i].clear();
        edg[i].clear();
        bin[i]=i;
        cur[i]=-1;
        dis[i]=0;
        vis[i]=0;
    }
}
int main()
{
    //freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
    while(scanf("%d%d%d",&n,&m,&c)!=EOF)
    {
        inti();
        //构建城市的图
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&x,&y,&val);
            edge[x].push_back(node(y,val));
            edge[y].push_back(node(x,val));
        }
        //构建查询树
        for(int i=0;i<c;i++)
        {
            scanf("%d%d",&x,&y);
            op[x][y]=op[y][x]=i;
            edg[x].push_back(node(y,0));
            edg[y].push_back(node(x,0));
        }
        for(int i=1;i<=n;i++)
        {
            if(!vis[i])
            {
                roo=i;
                dis[i]=0;
                LCA(i);
            }
        }
        for(int i=0;i<c;i++)
        {
            if(cur[i]==-1)
                puts("Not connected");
            else
                printf("%d\n",cur[i]);
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/wuwangchuxin0924/p/6114145.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值