Connections between cities (hdu 2874 LCA)

Connections between cities

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


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 
 


題意:n個點m條邊c次詢問,每次詢問(u,v)之間的最短距離,若不聯通,輸出“Not connected”,否則輸出最短距離,沒有環。

思路:LCA,但是給的圖是一個森林,每次找到子樹Tarjan,還有一種方法就是添加根root,把所有樹連成一棵樹再一次LCA。數組開小了它卻返回TLE,鬱悶我半天。。。

代碼:

#include <iostream>
#include <functional>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

#define INF 0x3f3f3f3f
#define mod 1000000009
const int maxn = 10005;
const int MAXN = 200005;
const int MAXM = 200010;
const int N = 1005;

struct Edge
{
    int u,v,w,next;
}edge[MAXN];

struct Query
{
    int q,id,next;
}query[2000000+10];

int n,m,c;
int head[maxn],hed[maxn];
int father[maxn],dis[maxn];
int tot1,tot2;
bool vis[maxn],vis2[maxn];
int ans[1000000+10];

void init()
{
    tot1=tot2=0;
    memset(head,-1,sizeof(head));
    memset(hed,-1,sizeof(hed));
    memset(vis2,false,sizeof(vis2));
    memset(dis,0,sizeof(dis));
    memset(ans,-1,sizeof(ans));
    for (int i=0;i<=n;i++)
        father[i]=i;
}

void addedge(int u,int v,int w)
{
    edge[tot1].u=u;
    edge[tot1].v=v;
    edge[tot1].w=w;
    edge[tot1].next=head[u];
    head[u]=tot1++;
}

void addQuery(int u,int v,int id)
{
    query[tot2].q=v;
    query[tot2].id=id;
    query[tot2].next=hed[u];
    hed[u]=tot2++;
}

int find_father(int x)
{
    if (x!=father[x])
        father[x]=find_father(father[x]);
    return father[x];
}

void Union(int u,int v)
{
    father[find_father(v)]=find_father(u);
}

void Tarjan(int u,int d)
{
    vis[u]=true;
    vis2[u]=true;
    dis[u]=d;
    for (int i=head[u];~i;i=edge[i].next)
    {
        int v=edge[i].v;
        if (vis[v]) continue;
        Tarjan(v,d+edge[i].w);
        Union(u,v);
    }
    for (int i=hed[u];~i;i=query[i].next)
    {
        int v=query[i].q;
        if (!vis[v]) continue;
        ans[query[i].id]=dis[u]+dis[v]-2*dis[find_father(v)];
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
#endif
    int i,j,u,v,w;
    while (~scanf("%d%d%d",&n,&m,&c))
    {
        init();
        for (i=0;i<m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            vis2[v]=true;
            addedge(u,v,w);
            addedge(v,u,w);
        }
        for (i=0;i<c;i++)
        {
            scanf("%d%d",&u,&v);
            addQuery(u,v,i);
            addQuery(v,u,i);
        }
        for (i=1;i<=n;i++)
        {
            if (!vis2[i])
            {
                memset(vis,false,sizeof(vis));
                Tarjan(i,0);
            }
        }
        for (i=0;i<c;i++)
        {
            if (ans[i]==-1) printf("Not connected\n");
            else printf("%d\n",ans[i]);
        }
    }
    return 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章