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;
    }
  • 相关阅读:
    图像的熵
    Tomcat6.0中JNDI的配置oracle11g
    JAVA错误:Syntax error on token "else", delete this token
    JAVA利用飞信接口发送短信
    更换ArcGIS 9.3授权ecp文件
    Java调用C++类库JNI
    jsp将变量传递给javascript函数
    windows xp文件夹共享,取消用户名密码输入
    WPF stringformat设置
    无法识别的属性“targetFramework”。请注意属性名称区分大小写。错误解决办法
  • 原文地址:https://www.cnblogs.com/soTired/p/5369609.html
Copyright © 2011-2022 走看看