zoukankan      html  css  js  c++  java
  • hdoj2874 -- Connections between cities(LCA--tarjan离线)

    Connections between cities

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


    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
     
    Recommend
    gaojie   |   We have carefully selected several similar problems for you:  2873 2876 2872 2875 2877
     
    最短路;
    #include <cstdio>
    #include <cstring>
    
    #define N 10010
    #define M 20010
    #define C 2000010
    
    struct Edge{
        int to, next, dis;
        Edge() {}
        Edge(int to, int dis, int next): to(to), dis(dis), next(next){};
    }E[M];
    
    struct Edge2{
        int to, next, w;
        Edge2() {}
        Edge2(int to, int w, int next): to(to), w(w), next(next) {};
    }E2[C];
    
    int n, m, c, tot, tot2;
    int head[N], head2[N], dis[N], f[N], vis[N];
    
    void addEdge(int u, int v, int dis){ 
        E[tot]= Edge(v, dis, head[u]);
        head[u]=tot++;
        E[tot]= Edge(u, dis, head[v]);
        head[v]=tot++;
    }
    
    void addEdge2(int u, int v){
        E2[tot2]= Edge2(v, -1, head2[u]);
        head2[u]=tot2++;
        E2[tot2]= Edge2(u, -1, head2[v]);
        head2[v]=tot2++;
    }
    /*void addEdge(int u, int v, int dis) {
        E[tot].to = v; E[tot].next = head[u]; E[tot].dis = dis; head[u] = tot++;
        u = u ^ v; v = u ^ v; u = u ^ v;
        E[tot].to = v; E[tot].next = head[u]; E[tot].dis = dis; head[u] = tot++;
    }
    
    void addEdge2(int u, int v) {
        E2[tot2].to = v; E2[tot2].next = head2[u]; E2[tot2].w = -1; head2[u] = tot2++;
        u = u ^ v; v = u ^ v; u = u ^ v;
        E2[tot2].to = v; E2[tot2].next = head2[u]; E2[tot2].w = -1; head2[u] = tot2++;
    }*/
    void init()
    {
        memset(head, -1, sizeof(head));
        memset(head2, -1, sizeof(head2));
        tot=tot2=0;
        
        int u, v, d;
        for(int i=0; i< m; i++)
        {
            scanf("%d%d%d", &u, &v, &d);
            addEdge(u, v, d); 
        } 
        
        for(int i=0; i<c; i++)
        {
            scanf("%d%d", &u, &v);
            addEdge2(u, v);
        }
        memset(vis, 0, sizeof(vis));
    }
    
    int find(int x){
        return x==f[x]?  x: f[x]=find(f[x]);
    }
    
    void tarjan(int u, int time)
    {
        vis[u]= time;
        f[u]= u;
        
        int v;
        for(int i=head[u]; ~i; i=E[i].next)
        {
            v=E[i].to;
            if(vis[v])
                continue;
            dis[v]= dis[u]+ E[i].dis;
            tarjan(v, time);
            f[v]= u;    
        } 
        
        for(int i= head2[u]; ~i; i=E2[i].next)
        {
            v=E2[i].to;
            if(vis[v]== time)
                E2[i].w= E2[i^1].w = dis[u]+dis[v]- 2*dis[find(v)];
        }
    } 
    void solve()
    {
        int cnt=1;
        for(int i=1; i<=n; i++){
            if(!vis[i]){
                dis[i]= 0;
                tarjan(i, cnt); 
            } 
            cnt++;
        }
        
        for(int i=0; i< tot2; i +=2){
            if(E2[i].w == -1)
                printf("Not connected
    ");
            else
                printf("%d
    ", E2[i].w)  ;
        }
    }
    int main()
    {
        while(scanf("%d%d%d", &n, &m, &c) != EOF)
        {
            init();
            solve();
        }
        return 0;
    }
  • 相关阅读:
    WM_COMMAND 和 WM_NOTIFY 的区别
    C 游戏所要看的书
    Source Insight中文字体设置
    在 windows7 中使用 vs2003 时,“在文件中查找”导致无响应的问题
    解决VS2008 调试启动特别慢
    c++ 浅谈 new 的使用
    Access界面基础操作
    与孩子一起学编程12章
    YT工作日志-0911
    两种方式遍历二叉树--递归方式和非递归方式
  • 原文地址:https://www.cnblogs.com/soTired/p/5369609.html
Copyright © 2011-2022 走看看