您的位置:首页 > 其它

hdu2874 Connections between cities (LCA离线)

2016-08-05 11:15 211 查看

Connections between cities

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


[align=left]Problem Description[/align] 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.
[align=left]Input[/align] 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.
[align=left]Output[/align] 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.
[align=left]Sample Input[/align]
5 3 2

1 3 2

2 4 3

5 2 3

1 4

4 5

[align=left]Sample Output[/align]
Not connected

6


Hint

Hint

Huge input, scanf recommended.



这道题就是LCA离线的裸题,但是很容易错。

首先很容易超了内存,我们不能太浪费了,要省着点用。

另外我这一段开始写错了。

if(!used[to]) {
if(now[f]==-1) {
now[to]=-1;
} else
now[to]=now[f]+a[i].dis;
tarjan(to);
fa[to]=f;
}


因为tarjan是离线处理,通过递归的思想去解决的,一开始我把计算距离的放在了tarjan(to)的下面,然后只有跑完to这颗子树才来计算now[to],当然,这个连样例都没过,说明自己对tarjan的理解还不深。

然后因为图可能是不连通的,所以你需要对每一个联通分支,但是如果以前搜索过了就不用了。每次搜索一个联通分支之前你需要memset一下距离,不然会错,因为图不是联通的,你前面搜索的距离是没有用的。

/**************************
*Create time: Thu Aug 04 20:23:13 2016

*Author: Mymilkbottles

*File name:
**************************/
#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<time.h>

#define pi(x,y) printf("%d%c",(x),(y));
#define pin(x) printf("%d\n",(x));
#define si(x) scanf("%d",&(x))
#define sii(x,y) scanf("%d%d",&(x),&(y))
#define s3(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))
#define rep(x,y,z) for(int (x)=(y);(x)<(z);++(x))
#define dep(x,y,z) for(int (x)=(y)-1;(x)>=(z);--(x))
#define read int TcaseN;scanf("%d",&TcaseN);for(int Tcase=1;Tcase<=TcaseN;++Tcase)
#define cls(x,y) memset((x),(y),sizeof((x)));
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define max3(value_a,value_b,value_c) max(max(value_a,value_b),value_c)
#define min3(value_a,value_b,value_c) min(min(value_a,value_b),value_c)
#define GT(x) (x)=clock();
#define fin(x) freopen(x,"r",stdin);
#define fout(x) freopen(x,"w",stdout);

///In This You Can Define Long Integer Type
#define LONGTYPE long long
typedef LONGTYPE LL;
typedef unsigned LONGTYPE ULL;
const int maxint=((~((unsigned)(0)))>>1);
const LL maxll=((~((unsigned LONGTYPE)(0)))>>1);

const int inf=0x3f3f3f3f;
const double PI=acos(-1.0);
const int maxn=1e4+5;

int top1,top2;

LL now[maxn];

struct note {
int to,next;
LL dis;
} a[maxn<<1];

struct notes{
int to,next;
}q[maxn*200];

LL res[maxn*100];

int head1[maxn];
int head2[maxn];
bool used[maxn];
int fa[maxn];

void ADD(int u,int v,int w) {
a[top1].to=v;
a[top1].next=head1[u];
a[top1].dis=w;
head1[u]=top1++;
a[top1].to=u;
a[top1].next=head1[v];
a[top1].dis=w;
head1[v]=top1++;
}

void ADD1(int u,int v) {
q[top2].to=v;
q[top2].next=head2[u];

head2[u]=top2++;
q[top2].to=u;
q[top2].next=head2[v];

head2[v]=top2++;
}

int finds(int x) {
if(x^fa[x]) {
fa[x]=finds(fa[x]);
}
return fa[x];
}

void tarjan(int f) {
used[f]=true;
fa[f]=f;
for(int i=head1[f]; ~i; i=a[i].next) {
int to=a[i].to;
if(!used[to]) { if(now[f]==-1) { now[to]=-1; } else now[to]=now[f]+a[i].dis; tarjan(to); fa[to]=f; }
}

for(int i=head2[f]; ~i; i=q[i].next) {
int to=q[i].to;
if(used[to]) {
if(now[to]^-1&&(res[i>>1]==-1))
res[i>>1]=now[to]+now[f]-(now[finds(to)]<<1);
}
}
}

int main() {
#ifdef tangge
clock_t tSTART,tEND,t3;
GT(tSTART);
fin("hdu2874.txt");
#endif // tangge

/*Input:*/
int u,v,w;

int n,m,Q;
while(~s3(n,m,Q)) {
cls(head1,-1)
top1=top2=0;

rep(i,0,m) {
s3(u,v,w);
ADD(u,v,w);
}

cls(head2,-1)
rep(i,0,Q) {
sii(u,v);
ADD1(u,v);
}
cls(used,false)
cls(res,-1)
rep(i,1,n+1) {
if(!used[i]) {
cls(now,-1)
now[i]=0;
tarjan(i);
}
}
rep(i,0,Q) {
if(res[i]==-1)
puts("Not connected");
else
printf("%I64d\n",res[i]);
}

}
#ifdef tangge
GT(tEND);
printf("%.8lf\n",(tEND-tSTART)/1000.0);
#endif // tangge
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: