poj 1986

 

Distance Queries
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 4350   Accepted: 1537
Case Time Limit: 1000MS

Description

Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same input as in "Navigation Nightmare",followed by a line containing a single integer K, followed by K "distance queries". Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing distance (measured in the length of the roads along the path between the two farms). Please answer FJ's distance queries as quickly as possible! 

Input

* Lines 1..1+M: Same format as "Navigation Nightmare" 

* Line 2+M: A single integer, K. 1 <= K <= 10,000 

* Lines 3+M..2+M+K: Each line corresponds to a distance query and contains the indices of two farms. 

Output

* Lines 1..K: For each distance query, output on a single line an integer giving the appropriate distance. 

Sample Input

7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
3
1 6
1 4
2 6

Sample Output

13
3
36

Hint

Farms 2 and 6 are 20+3+13=36 apart. 

Source

USACO 2004 February

分析:典型的lca问题,由于本人第一次遇见此类问题,就拿来练习。。。自己写了DFS+RMQ(ST),

           tarjans算法为Wingszero牛人的版本优化(原出处http://wingszero.blogbus.com/logs/61073941.html),

           在此感谢所有贴题解的牛人。。。

 

DFS+RMQ(ST):

#include<stdio.h> const int maxn=40001; struct edge { int to,dis; edge *next; }*tree[maxn]; int n,m,pos[maxn],depth[maxn],g[maxn<<1][17]={0},dist[maxn],tot; bool flag[maxn]; inline int in() { char ch; int a=0; while(!(((ch=getchar())>='0')&&(ch<='9'))); a*=10;a+=ch-'0'; while(((ch=getchar())>='0')&&(ch<='9'))a*=10,a+=ch-'0'; return a; } void out(int a) { if(a>=10)out(a/10); putchar(a%10+'0'); } void insertedge(int from, int to, int dis) { edge *newedge=new edge(); newedge->to=to;newedge->dis=dis; newedge->next=tree[from]; tree[from]=newedge; } void DFS(int cur,int dep) { if(!pos[cur])pos[cur]=tot; g[tot++][0]=cur; depth[cur]=dep; flag[cur]=0; edge *now=tree[cur]; while(now!=NULL) { if(flag[now->to]) { dist[now->to]=dist[cur]+now->dis; DFS(now->to,dep+1); g[tot++][0]=cur; } now=now->next; } } inline int minrmq(int a,int b){return(depth[a]<depth[b]?a:b);} inline void rmq() { int i,j; depth[0]=maxn; for(j=1;(1<<j)<tot;++j) for(i=0;i+(1<<j)-1<tot;++i) g[i][j]=minrmq(g[i][j-1],g[i+(1<<(j-1))][j-1]); } inline int askrmq(int l,int r) { int k=0; while(l+(1<<k)<r-(1<<k)+1)++k; return minrmq(g[l][k],g[r-(1<<k)+1][k]); } int main() { //freopen("car.in","r",stdin); //freopen("car.out","w",stdout); int from,to,dis,i; n=in();m=in(); for(i=0;i<m;++i) { from=in();to=in();dis=in(); insertedge(from,to,dis); insertedge(to,from,dis); } dist[1]=0;tot=0; for(i=1;i<=n;++i)flag[i]=1,pos[i]=0; DFS(1,0); rmq(); m=in(); for(i=0;i<m;++i) { from=in();to=in(); if(pos[from]>pos[to]) n=askrmq(pos[to],pos[from]); else n=askrmq(pos[from],pos[to]); out(dist[from]+dist[to]-dist[n]*2); putchar('/n'); } return 0; } 

tarjan:

#include<stdio.h> using namespace std; const int maxn=40001; const int maxk=10001; struct Edge{int v,w,next;}g[maxn*2],q[maxk*2]; int headg[maxn]={0},headq[maxn]={0},parent[maxn],dis[maxn],pos1,pos2,n,m,k,a,b,c; char d[10]; int ans[maxk]; bool vis[maxn]={0}; inline void add1(int a,int b,int c) { g[pos1].next=headg[a]; g[pos1].v=b; g[pos1].w=c; headg[a]=pos1++; } inline void add2(int a,int b,int c) { q[pos2].next=headq[a]; q[pos2].v=b; q[pos2].w=c; headq[a]=pos2++; } int find(int a) { if(parent[a]==-1)return a; return parent[a]=find(parent[a]); } void tarjan(int u) { vis[u]=1; int v,w; for(int i=headq[u];i;i=q[i].next) { v=q[i].v,w=q[i].w; if(vis[v])ans[w]=dis[u]+dis[v]-2*dis[find(v)]; } for(int i=headg[u];i;i=g[i].next) { v=g[i].v,w=g[i].w; if(!vis[v]) { dis[v]=dis[u]+w; tarjan(v); parent[v]=u; } } } inline int in() { char ch; int a=0; while(!(((ch=getchar())>='0')&&(ch<='9'))); a*=10;a+=ch-'0'; while(((ch=getchar())>='0')&&(ch<='9'))a*=10,a+=ch-'0'; return a; } void out(int a) { if(a>=10)out(a/10); putchar(a%10+'0'); } int main() { n=in();m=in(); for(int i=0;i<=n;++i)parent[i]=-1; pos1=pos2=1; for(int i=0;i<m;++i) { a=in();b=in();c=in(); add1(a,b,c); add1(b,a,c); } k=in(); for(int i=1;i<=k;++i) { a=in();b=in(); add2(a,b,i); add2(b,a,i); } dis[1]=0; tarjan(1); for(int i=1;i<=k;++i)out(ans[i]),putchar('/n'); } 

 

你可能感兴趣的:(struct,tree,Integer,query,input,distance)