zoukankan      html  css  js  c++  java
  • Lca hdu 2874 Connections between cities

    Connections between cities

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 3937    Accepted Submission(s): 1144

    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
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<string>
    #include<queue>
    #include<vector>
    #include<stdlib.h>
    #include<set>
    #include<map>
    #include<cmath>
    using namespace std;
    #define maxn 10000+5
    int pre[maxn][20],vit[maxn],n,m,c,deep[maxn];
    __int64 lenth[maxn][20];
    vector<int >ko[maxn],dis[maxn];
    void dfs(int x,int de)
    {
        vit[x]=1;
        deep[x]=de;
        for(int i=0; i<ko[x].size(); i++)
        {
            int to=ko[x][i];
            if(!vit[to])
            {
                pre[to][0]=x;
                lenth[to][0]=dis[x][i];
                dfs(to,de+1);
            }
        }
    }
    int main()
    {
    
        int i,j;
        while(cin>>n>>m>>c)
        {
            for(i=1; i<=n; i++)
            {
                ko[i].clear();
                dis[i].clear();
            }
            for(i=1; i<=m; i++)
            {
                int u,v,l;
                scanf("%d%d%d",&u,&v,&l);
                ko[u].push_back(v);
                dis[u].push_back(l);
                ko[v].push_back(u);
                dis[v].push_back(l);
            }
            memset(vit,0,sizeof(vit));
            memset(pre,-1,sizeof(pre));
            memset(lenth,0,sizeof(lenth));
            memset(deep,0,sizeof(deep));
            for(i=1; i<=n; i++)
                if(!vit[i])
                {
                    pre[i][0]=0;
                    dfs(i,1);
                }
    
            for(i=1; i<20; i++)
                for(j=1; j<=n; j++)
                {
                    if(pre[j][i-1]!=-1)
                    {
                        pre[j][i]=pre[ pre[j][i-1] ][i-1];
                        lenth[j][i]=lenth[j][i-1]+lenth[ pre[j][i-1] ][i-1];
                    }
                }
            while(c--)
            {
                int u,v,dd,num=0;
                __int64 mm=0;
                scanf("%d%d",&u,&v);
                if(deep[u]<deep[v]) swap(u,v);
                dd=deep[u]-deep[v];
                while(dd) //使 u v处于同一点
                {
                    if(dd&1)
                    {
                        mm+=lenth[u][num];
                        u=pre[u][num];
                    }
                    ++num;
                    dd>>=1;
                }
                if(u==v)
                {
                    if(u>0)
                        printf("%I64d
    ",mm);
                    else
                        printf("Not connected
    ");
                    continue;
                }
                while(u!=v)
                {
                    int If;
                    for(i=0; i<20; i++)
                    {
                        if(pre[u][i]==pre[v][i])
                           {
                               If=i;
                               break;
                           }
                    }
                    if(If==0)
                    {
                        if(pre[u][0]>0)
                        {
                            mm+=lenth[u][0]+lenth[v][0];
                            printf("%I64d
    ",mm);
                        }
                        else
                           printf("Not connected
    ");
                        break;
                    }
                    else
                    {
                        mm+=lenth[u][If-1]+lenth[v][If-1];
                        u=pre[u][If-1];
                        v=pre[v][If-1];
                    }
                }
    
            }
        }
    }
    
    Not connected
    6
     
  • 相关阅读:
    vs运行代码版本不一致删除缓存
    spring framework 各版本源码下载地址
    类型初始值设定项引发异常
    datagridview 点击列标题排序
    软媒魔方u盘装系统
    sql中exists,not exists的用法
    java web第一个Servlet程序
    《ASP.NET 本质论》HttpApplication的处理管道 ,HttpMoudle,HttpHandler
    如何正确设置 Informix GLS 及 CSDK 语言环境
    Linux网络流量控制工具—Netem
  • 原文地址:https://www.cnblogs.com/ainixu1314/p/3608730.html
Copyright © 2011-2022 走看看