【基础Tarjan_LCA(森林)】Connections between cities

Connections between cities

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


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.
 
解法:Tarjan_LCA+并查集(判断森林)
  这一题处理不好的话,很容易爆内存的,在记录询问点的边表结构的时候,需要设置好变量。设置太多的变量就会爆内存,所以在每次找到一个最近公共祖先的时候,就需要记录答案了的。
代码:2015.8.12
  1 #include<iostream>
  2 #include<stdio.h>
  3 #include<algorithm>
  4 #include <string.h>
  5 #define MAX 10010
  6 #define MM 1000010
  7 using namespace std;
  8 int Dis[MAX];/*记录到根的距离*/
  9 int ID[MAX];/*判断是否同一个集合*/
 10 int fa[MAX];/*记录父节点*/
 11 struct edge{int to,next,v;}edg[MAX*2],omg[MM*2];
 12 int first[MAX];/*图的节点*/
 13 int first_s[MAX];/*询问的节点*/
 14 int sign;
 15 bool Vis[MAX];/*点标记*/
 16 void Cread(int N)/*初始化*/
 17 {
 18     for(int i=0;i<=N;i++)
 19     {
 20         first[i]=first_s[i]=Dis[i]=0;
 21         Vis[i]=true;
 22         fa[i]=ID[i]=i;
 23     }sign=1;
 24 }
 25 int Find(int x)/*寻找父亲节点*/
 26 {
 27     if(fa[x]!=x)fa[x]=Find(fa[x]);
 28     return fa[x];
 29 }
 30 int Find_P(int x)/*寻找集合*/
 31 {
 32     if(ID[x]!=x)ID[x]=Find_P(ID[x]);
 33     return ID[x];
 34 }
 35 void add_e(int x,int y,int z)/*添加图的边*/
 36 {
 37     edg[sign].to=y;
 38     edg[sign].v=z;
 39     edg[sign].next=first[x];
 40     first[x]=sign++;
 41 }
 42 
 43 void add_o(int x,int y,int z)/*添加查询的边*/
 44 {
 45     omg[sign].to=y;
 46     omg[sign].v=z;
 47     omg[sign].next=first_s[x];
 48     first_s[x]=sign++;
 49 }
 50 
 51 void Tarjan_LCA(int n)
 52 {
 53     Vis[n]=false;
 54     fa[n]=n;
 55     for(int i=first_s[n];i;i=omg[i].next)
 56     {
 57         int TMD=omg[i].to;
 58         if(!Vis[TMD])/*访问被标记过的询问的连接点*/
 59         {
 60             omg[i].v=Dis[n]+Dis[TMD]-2*Dis[Find(TMD)];/*记录答案*/
 61             if(i%2==0)omg[i-1].v=omg[i].v;/*记录答案*/
 62         }
 63     }
 64     for(int i=first[n];i;i=edg[i].next)
 65     {
 66         int TMD=edg[i].to;
 67         if(Vis[TMD])/*访问还没有被标记过的点*/
 68         {
 69             Dis[TMD]=Dis[n]+edg[i].v;
 70             Tarjan_LCA(TMD);
 71             fa[TMD]=n;
 72         }
 73     }
 74 }
 75 void Update(int a,int b)
 76 {
 77     int A=Find_P(a);
 78     int B=Find_P(b);
 79     if(A!=B)ID[A]=B;
 80 }
 81 int main()
 82 {
 83     int T,n,m,k,i,j,a,b,c,A,B;
 84     while(scanf("%d%d%d",&n,&m,&k)!=EOF)
 85     {
 86         Cread(n);/*初始化*/
 87         for(i=1;i<=m;i++)
 88         {
 89             scanf("%d%d%d",&a,&b,&c);
 90             Update(a,b);    /*合并集合*/
 91             add_e(a,b,c);add_e(b,a,c);
 92         }
 93         for(i=1,sign=1;i<=k;i++)
 94         {
 95             scanf("%d %d",&a,&b);
 96             if(ID[a]!=ID[b])/*判断是否为同一棵树*/
 97             {
 98                 add_o(a,b,-1);/*表示两点不在同一棵树上*/
 99                 add_o(b,a,-1);
100             }
101             else
102             {
103                 add_o(a,b,0);/*表示两点在同一棵树上*/
104                 add_o(b,a,0);
105             }
106         }
107         for(i=1;i<=n;i++)
108         {
109             if(ID[i]==i)/*寻找根节点*/
110             {
111                 memset(Vis,true,sizeof(Vis));/*还原标记,防止连接所询问的错误的点*/
112                 Tarjan_LCA(i);
113             }
114         }
115         for(i=1;i<sign;i+=2)
116         {   /*因为边是双向的,我这里设置的是答案保存在奇数编号的边数上*/
117             if(omg[i].v!=-1)/*没有连接,说明两点没有公共祖先*/
118                 printf("%d\n",omg[i].v);/*输出两点之间的距离*/
119             else   printf("Not connected\n");/*两点没有连接*/
120         }
121     }
122     return 0;
123 }
View Code

 

posted @ 2015-08-11 23:26  Wurq  阅读(193)  评论(0编辑  收藏  举报