(离线算法 LCA) hdu 2874

Connections between cities

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


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
 

 

 

MLE了,但是代码的思路是正确的

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
struct node
{
    int v,w;
    node(int _v,int _w) : v(_v),w(_w) {}
};
vector<node> e[10001],query[10001];
int n,m,q,dist[10001],ans[1000001],fa[10001],mark[10001];
bool vis[10001];
int find(int x)
{
    if(x!=fa[x])
        fa[x]=find(fa[x]);
    return fa[x];
}
void Union(int x,int y)
{
    int fx,fy;
    fx=find(x),fy=find(y);
    if(fx!=fy)
        fa[fy]=fx;
}
void tarjan(int u,int val)
{
    vis[u]=1;
    dist[u]=val;
    for(int i=0;i<e[u].size();i++)
    {
        int v=e[u][i].v;
        int w=e[u][i].w;
        if(vis[v]) continue;
        tarjan(v,val+w);
        Union(u,v);
    }
    for(int i=0;i<query[u].size();i++)
    {
        int v=query[u][i].v;
        int num=query[u][i].w;
        if(!vis[v]||mark[find(v)])
            continue;
        ans[num]=dist[u]+dist[v]-2*dist[find(v)];
    }
}
int main()
{
    while(scanf("%d%d%d",&n,&m,&q)!=EOF)
    {
        for(int i=1;i<=n;i++)
        {
            fa[i]=i;
            vis[i]=0;
            mark[i]=0;
            dist[i]=0;
            e[i].clear();
            query[i].clear();
        }
        for(int i=0;i<m;i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            e[x].push_back(node(y,z));
            e[y].push_back(node(x,z));
        }
        for(int i=0;i<q;i++)
        {
            int x,y;
            ans[i]=-1;
            scanf("%d%d",&x,&y);
            query[x].push_back(node(y,i));
            query[y].push_back(node(x,i));
        }
        for(int i=1;i<=n;i++)
        {
            if(!vis[i])
            {
                tarjan(i,0);
                mark[i]=1;
            }
        }
        for(int i=0;i<q;i++)
        {
            if(ans[i]==-1)
                printf("Not connected\n");
            else
                printf("%d\n",ans[i]);
        }
    }
    return 0;
}

  

posted @ 2015-05-14 09:09  waterfull  阅读(156)  评论(0编辑  收藏  举报