Connections between cities

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

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
模板题,注意该题图不连通,在tarjan算法中将d[]设为-1.
RMQ+LCA,在线算法
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAXN=;
struct Edge{
int to,cost,next;
}es[MAXN*];
int head[MAXN],tot;
void add_edge(int u,int v,int cost)
{
es[tot].to=v;
es[tot].cost=cost;
es[tot].next=head[u];
head[u]=tot++;
}
int V,E,Q;
int dp[MAXN*][];
int depth[MAXN*];
int vs[MAXN*];
int id[MAXN];
int cnt,dep;
int d[MAXN];
void dfs(int u,int fa)
{
int temp=++dep;
depth[++cnt]=temp;
vs[temp]=u;
id[u]=cnt;
for(int i=head[u];i!=-;i=es[i].next)
{
int to=es[i].to;
if(to==fa) continue;
d[to]=d[u]+es[i].cost;
dfs(to,u);
depth[++cnt]=temp;
}
} void init_rmq(int n)
{
for(int i=;i<=n;i++) dp[i][]=depth[i]; int m=floor(log(n*1.0)/log(2.0));
for(int j=;j<=m;j++)
for(int i=;i<=n-(<<j)+;i++)
dp[i][j]=min(dp[i][j-],dp[i+(<<(j-))][j-]);
}
int rmq(int a,int b)
{
int k=floor(log((b-a+)*1.0)/log(2.0));
return min(dp[a][k],dp[b-(<<k)+][k]);
}
int LCA(int u,int v)
{
if(id[u]>id[v]) swap(u,v);
int k=rmq(id[u],id[v]);
return vs[k];
} int par[MAXN],rnk[MAXN];
void init_set(int n)
{
for(int i=;i<=n;i++)
{
par[i]=i;
rnk[i]=;
}
}
int fnd(int x)
{
if(par[x]==x)
return x;
return par[x]=fnd(par[x]);
}
void unite(int x,int y)
{
int a=fnd(x);
int b=fnd(y);
if(a==b) return ;
if(rnk[a]<rnk[b])
{
par[a]=b;
}
else
{
par[b]=a;
if(rnk[a]==rnk[b]) rnk[a]++;
}
}
bool same(int x,int y)
{
return fnd(x) == fnd(y);
}
int main()
{
while(scanf("%d%d%d",&V,&E,&Q)!=EOF)
{
tot=;
memset(head,-,sizeof(head));
cnt=;
dep=;
memset(d,,sizeof(d));
memset(id,,sizeof(id));
init_set(V);
for(int i=;i<E;i++)
{
int u,v,cost;
scanf("%d%d%d",&u,&v,&cost);
add_edge(u,v,cost);
add_edge(v,u,cost);
unite(u,v);
}
for(int i=;i<=V;i++)
if(!id[i]) dfs(i,-);
init_rmq(cnt);
while(Q--)
{
int u,v;
scanf("%d%d",&u,&v);
if(same(u,v))
{
printf("%d\n",d[u]+d[v]-*d[LCA(u,v)]);
}
else
{
printf("Not connected\n");
}
}
}
return ;
}
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
const int MAXN=;
struct Edge{
int to,cost,next;
}es[*MAXN];
int V,E,Q;
struct Query{
int v,next;
}qs[*MAXN];
int head[MAXN],tot;
void add_edge(int u,int v,int cost)
{
es[tot].to=v;
es[tot].cost=cost;
es[tot].next=head[u];
head[u]=tot++;
}
int qhead[MAXN],ant;
void add_query(int u,int v)
{
qs[ant].v=v;
qs[ant].next=qhead[u];
qhead[u]=ant++;
} int d[MAXN];
int ans[*MAXN];
int par[MAXN];
bool vis[MAXN];
int fnd(int x)
{
if(x==par[x])
return x;
return par[x]=fnd(par[x]);
}
void dfs(int u,int fa)
{
par[u]=u;
vis[u]=true;
for(int i=head[u];i!=-;i=es[i].next)
{
int v=es[i].to;
if(vis[v]) continue;
d[v]=d[u]+es[i].cost;
dfs(v,u);
par[v]=u;
}
for(int i=qhead[u];i!=-;i=qs[i].next)
{
int v=qs[i].v;
if(vis[v])
{
if(d[v]!=-)
ans[i/]=d[u]+d[v]-*d[fnd(v)];
else
ans[i/]=-;
}
}
}
int main()
{
while(~scanf("%d %d %d",&V,&E,&Q))
{
tot=;
memset(head,-,sizeof(head));
ant=;
memset(qhead,-,sizeof(qhead));
memset(vis,false,sizeof(vis));
for(int i=;i<E;i++)
{
int u,v,cost;
scanf("%d%d%d",&u,&v,&cost);
add_edge(u,v,cost);
add_edge(v,u,cost);
}
for(int i=;i<Q;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add_query(u,v);
add_query(v,u);
}
for(int i=;i<=V;i++)
if(!vis[i])
{
memset(d,-,sizeof(d));
d[i]=;
dfs(i,-);
}
for(int i=;i<Q;i++)
if(ans[i]==-) printf("Not connected\n");
else printf("%d\n",ans[i]);
}
return ;
}

HDU2874(LCA应用:求两点之间距离,图不连通)的更多相关文章

  1. 求两点之间距离 C++

    求两点之间距离(20 分) 定义一个Point类,有两个数据成员:x和y, 分别代表x坐标和y坐标,并有若干成员函数. 定义一个函数Distance(), 用于求两点之间的距离.输入格式: 输入有两行 ...

  2. UESTC(LCA应用:求两点之间的距离)

    Journey Time Limit: 15000/3000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Bob has ...

  3. C#面向对象思想计算两点之间距离

    题目为计算两点之间距离. 面向过程的思维方式,两点的横坐标之差,纵坐标之差,平方求和,再开跟,得到两点之间距离. using System; using System.Collections.Gene ...

  4. 武汉科技大学ACM :1006: 零起点学算法25——求两点之间的距离

    Problem Description 输入平面坐标系中2点的坐标,输出它们之间的距离 Input 输入4个浮点数x1 y1 x2 y2,分别是点(x1,y1) (x2,y2)的坐标(多组数据) Ou ...

  5. 求两点之间最短路径-Dijkstra算法

     Dijkstra算法 1.定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.D ...

  6. JavaScript求两点之间相对于Y轴的顺时针旋转角度

    需求: 已知一个向量,初始位置在y轴方向,如图红色箭头,绕中心点(x1, y1)旋转若干角度后,到达Line(x2,y2 x1,y1)的位置,求旋转角度 分析: 坐标点(x1, y1)(x2, y2) ...

  7. C# 通过GPS坐标,计算两点之间距离

    之前在网上有很多这种计算的,但是代码都不怎么全.经过多方打听查询.找到完整代码.现将代码共享给大家. 有需要者觉得有用者欢迎使用.觉得用或简单的高手,请绕. public static double ...

  8. URAL 题目1553. Caves and Tunnels(Link Cut Tree 改动点权,求两点之间最大)

    1553. Caves and Tunnels Time limit: 3.0 second Memory limit: 64 MB After landing on Mars surface, sc ...

  9. sql 根据两点经纬度算出两点之间距离

    select (sqrt( ( ((121.544685-longitude)*PI()*12656*cos(((31.134857+latitude)/2)*PI()/180)/180) * ((1 ...

随机推荐

  1. HRBUST2030(dfs)

    成语接龙 Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %lld , %llu Java class nam ...

  2. android 5.X Toolbar+DrawerLayout实现抽屉菜单

    前言  android5.X新增的一个控件Toolbar,这个控件比ActionBar更加自由,可控,因为曾经的ActionBar的灵活性比較差,所以google逐渐使用Toolbar替代Action ...

  3. 可在 html5 游戏中使用的 js 工具库

    可在 html5 游戏中使用的 js 工具库 作者: 木頭 时间: September 21, 2014 分类: Utilities,Game 使用 cocos2d-js 3.0 开发游戏项目两三个月 ...

  4. Linux问题,磁盘分区打不开了

    Metadata kept in Windows cache, refused to mount. chkdsk /f http://www.bubuko.com/infodetail-1184937 ...

  5. php 去除html标记-strip_tags和htmlspecialchars的区别

    strip_tags 去掉 HTML 及 PHP 的标记. 语法: string strip_tags(string str); 传回值: 字串 函式种类: 资料处理 内容说明 本函式可去掉字串中包含 ...

  6. repeter中应用三元运算符

    应用情景一:根据ID显示名称例如:0代表启动,1:代表关闭例子如下 <td><%#Eval("ID").ToString() == "0" ? ...

  7. PowerDesigner逆向工程,从SQL Server数据库生成Physical Model -----数据源方式

    1.File-Reverse Engineer-Database 2.DBMS选择SQL Server 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 一路Next..... ...

  8. input光标位置

    兼容谷歌火狐-input光标位置 input框在没有添加任何效果的情况下,输入文字后光标始终在最后的位置,谷歌||火狐效果一样 但是在给input加入点击事件后 谷歌:input框插入文字后,光标会自 ...

  9. 九度OJ 1099:后缀子串排序 (排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3439 解决:1491 题目描述: 对于一个字符串,将其后缀子串进行排序,例如grain 其子串有: grain  rain  ain  i ...

  10. r testifying that your code will behave as you intend.

    https://github.com/stretchr/testify Testify - Thou Shalt Write Tests    Go code (golang) set of pack ...