HDU 2874 Connections between cities (LCA offline && online rmq,4 level) __rmq&lca

Source: Internet
Author: User
E-connections between cities crawling in process ... crawling failed Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64D &%i64u Submit Status appoint Description:system Crawler (2013-05-30)

Description after World War X, a lot of cities have been seriously, and we damaged to need rebuild. However, some materials needed can only is produced in certain places. So we need to transport this materials from the city to the city. For most of roads had been totally destroyed during the war, there might is no path between two cities, no circle exists a S.
Now, your task comes. After giving your condition of the roads, we want to know if there exists a path between any two. If The answer is yes, output the shortest path between them.  

Input input consists of multiple problem instances. For each instance, the 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, has three integers I, j and K, represent a road between City I and City J, with length K. Las T c lines, two integers i, j each line, indicates a query of the city I and City J.

Output for each problem instance and 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.
 
Idea: LCA off-line
#include <iostream> #include <cstring> #include <cstdio> #define for (I,A,B) for (int i=a;i<=b;++i) #
Define CLR (f,z) memset (f,z,sizeof (f)) using namespace Std;
const int nn=1e4+9;
const int MM=2E6+NN+NN; Class Edge {Public:int v,next,w;}
E[MM];
int HEAD[NN],QHEAD[NN],EDGE,ANS[MM],DIS[NN];
BOOL Vis[nn];
int RT[NN],N,M,C,ID[NN]; void data () {CLR (head,-1); CLR (qhead,-1); edge=0} void Add (int u,int v,int w,int*h) {E[edge].v=v;e[edge].w=w;e[edge].
next=h[u];h[u]=edge++;
  int find (int x) {if (rt[x]^x) Rt[x]=find (rt[x]);
return rt[x];
  } void Tarjan (int u,int bcc) {int v;
  ID[U]=BCC;
  Vis[u]=1;rt[u]=u;
    for (int i=head[u];~i;i=e[i].next) {v=e[i].v;
    if (Vis[v]) continue;
    DIS[V]=DIS[U]+E[I].W;
  Tarjan (V,BCC); rt[v]=u;
    for (int i=qhead[u];~i;i=e[i].next) {v=e[i].v;
    if (!vis[v]) continue;
  Ans[e[i].w]= (Id[u]==id[v])? Dis[u]+dis[v]-dis[find (v)]*2:-1;
  } void Getans () {clr (vis,0); CLR (id,0);
  int bcc=0;
for (I,1,n) if (!vis[i])  Dis[i]=0,tarjan (I,++BCC);
  int main () {int a,b,c;
    while (~SCANF ("%d%d%d", &n,&m,&c)) {data ();
      for (i,1,m) {scanf ("%d%d%d", &a,&b,&c);
    Add (a,b,c,head); add (B,a,c,head);
    for (i,1,c) {scanf ("%d%d", &a,&b), add (A,b,i,qhead), add (B,a,i,qhead);
    } Getans ();
    for (I,1,c) if (ans[i]<0) printf (' Not connected\n ');
  else printf ("%d\n", Ans[i]);
 }
}

LCA Online Turn RMQ
#include <iostream> #include <cstring> #include <cstdio> #define for (I,A,B) for (int i=a;i<=b;++i) #
Define CLR (f,z) memset (f,z,sizeof (f)) #define LL (x) (1<<x) using namespace std;
const int nn=4e4+9;
const int mm=4e4+9; Class Edge {Public:int v,next,w;}
E[MM];
int head[nn],edge,dis[nn],to[nn],dfs_clock;
int vis[nn],rmq[nn][30];
int RT[NN],N,M,C,BIT[NN]; void data () {CLR (head,-1); edge=0} void Add (int u,int v,int w,int*h) {E[edge].v=v;e[edge].w=w;e[edge].next=h[u];h[u]
=edge++;
  int find (int x) {if (rt[x]^x) Rt[x]=find (rt[x]);
return rt[x];
  } void Uni (int a,int b) {a=find (a); B=find (b);
Rt[a]=b;
  void Dfs (int u,int dep)/over Euler path {int V;
  to[dfs_clock]=u;//the DIS[U]=DEP of the existence of Euler path;
  vis[u]=dfs_clock++;
    for (int i=head[u];~i;i=e[i].next) {v=e[i].v;
      if (vis[v]==-1) {DFS (V,DEP+E[I].W);
    To[dfs_clock++]=u;
  }} void ST (int N) {bit[0]=-1;
  for (I,1,n) bit[i]= (i& (i-1)) ==0?bit[i-1]+1:bit[i-1]; for (I,0,n) Rmq[i][0]=dis[To[i]];
for (I,1,bit[n]) for (int j=0;j+ll (i) -1<=n;++j) rmq[j][i]=min (Rmq[j][i-1],rmq[j+ll (i-1)][i-1]);
  int rmq (int l,int r) {int t=bit[r-l+1];
  R-=ll (t)-1;
return min (rmq[l][t],rmq[r][t]);
  int main () {int a,b,c;
    while (~SCANF ("%d%d%d", &n,&m,&c)) {data (); CLR (vis,-1);
    for (i,0,n) rt[i]=i;
      for (i,1,m) {scanf ("%d%d%d", &a,&b,&c);
      Add (a,b,c,head); add (B,a,c,head);
    Uni (A,B);
    for (i,1,n)///virtual point 0, the virtual edge has a value, or find 0 is not necessarily the root point if (rt[i]==i) Add (0,i,1,head), add (I,0,1,head);
    dfs_clock=0;
    DFS (0,0);
    ST (dfs_clock-1);
      for (i,1,c) {scanf ("%d%d", &a,&b);
      int Ta=vis[a];
      int tb=vis[b];
      if (TA&GT;TB) swap (TA,TB);
      int DDD=RMQ (TA,TB);
      if (ddd==0) printf ("Not connected\n");
    else printf ("%d\n", dis[a]+dis[b]-2*ddd);
 }
  }
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.