Connections between cities

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 896 Accepted Submission(s): 236
 
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.

 
 
Source
2009 Multi-University Training Contest 8 - Host by BJNU
 
Recommend
gaojie
 
/*
用vector炸了,还是用邻接表卡过的
*/
#include<bits/stdc++.h>
using namespace std;
#define M 10007
#define N 2222212
int bin[M],dis[M],vis[M],cur[N],root[M];
int s1[N],s2[N],t[N],d[N],p[N];
int n,m,ne,cnt; int find(int x)
{
while(x!=bin[x])
x=bin[x];
return x;
} void add(int u,int v,int w,int h[])
{
t[ne]=v,d[ne]=w,p[ne]=h[u],h[u]=ne++;
t[ne]=u,d[ne]=w,p[ne]=h[v],h[v]=ne++;
}
/*
LCA算法以某一个节点作为根节点,开始遍历,遍历到一个结点先判断与它相关的结点是不是有已经被访问过的,
如果有的话,判断两个结点是不是在同一棵树上,如果是的话就保留最近公共祖先的距离,如果不是的话,就是不能到达,距离就赋值为-1 */
void LCA(int u)
{
root[u]=cnt;
bin[u]=u;
vis[u]=;
//遍历查询树
for(int i=s2[u];i;i=p[i])
{
int v=t[i];
if(vis[v])
{
if(root[u]==root[v])//在同一棵树下
{
int rt=find(v);//最近公共祖先
cur[d[i]]=dis[u]+dis[v]-*dis[rt];
}
else
cur[d[i]]=-;
} }
//遍历城市图
for(int i=s1[u];i;i=p[i])
{
int v=t[i];
if(!vis[v])
{
dis[v]=dis[u]+d[i];
LCA(v);
bin[v]=u;//路径压缩
}
}
} int main()
{
//freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
int n,m,q,i,j;
while(scanf("%d%d%d",&n,&m,&q)!=EOF)
{
for(i=,ne=;i<=n;i++)
{
s1[i]=s2[i]=vis[i]=cur[i]=root[i]=bin[i]=;
}
int u,v,w;
for(i=;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w,s1);
}
for(i=;i<=q;i++)
{
scanf("%d%d",&u,&v);
add(u,v,i,s2);
}
for(i=,cnt=;i<=n;i++,cnt++)
if(!vis[i])
{
dis[i]=;
LCA(i);
} for(i=;i<=q;i++)
if(cur[i]>=)
printf("%d\n",cur[i]);
else
printf("Not connected\n");
}
return ;
}
#include<bits/stdc++.h>
#define N 10005
using namespace std;
int n,m,c;
int bin[N];
int root[N];//表示点i的根节点
int roo;//表示当前是以哪个结点为根节点遍历的
int dis[N];//标记结点i到根结点的距离
int vis[N];//标记i点是否被访问过
int cur[N];//表示第几组解
int op[N][N];//表示解是第几组
struct node
{
int v,val;
node(){}
node(int a,int b)
{
v=a;
val=b;
}
};
vector<node>edge[N];
vector<node>edg[N];
int findx(int x)
{
while(x!=bin[x])
x=bin[x];
return x;
}
/*
Tarjan算法以某一个节点作为根节点,开始遍历,遍历到一个结点先判断与它相关的结点是不是有已经被访问过的,
如果有的话,判断两个结点是不是在同一棵树上,如果是的话就保留最近公共祖先的距离,如果不是的话,就是不能到达,距离就赋值为-1 */
int LCA(int u)
{
//首先遍历查询树
root[u]=roo;
vis[u]=;
bin[u]=u;
for(int i=;i<edg[u].size();i++)
{
int nex=edg[u][i].v;
if(vis[nex]==)//这个点遍历过了
{
if(root[u]==root[nex])
{
int rt=findx(nex);
cur[op[u][nex]]=dis[u]+dis[nex]-*dis[rt];//
}
else
cur[op[u][nex]]=-;
}
}
//然后就是遍历程城市树
for(int i=;i<edge[u].size();i++)
{
int nex=edge[u][i].v;
if(vis[nex]) continue;
dis[nex]=dis[u]+edge[u][i].val;//父节点到根节点的距离,加上到父节点的距离
LCA(nex);
bin[nex]=u;//路径压缩
}
}
int x,y,val;
void inti()
{
for(int i=;i<=n;i++)
{
edge[i].clear();
edg[i].clear();
bin[i]=i;
cur[i]=-;
dis[i]=;
vis[i]=;
}
}
int main()
{
//freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
while(scanf("%d%d%d",&n,&m,&c)!=EOF)
{
inti();
//构建城市的图
for(int i=;i<m;i++)
{
scanf("%d%d%d",&x,&y,&val);
edge[x].push_back(node(y,val));
edge[y].push_back(node(x,val));
}
//构建查询树
for(int i=;i<c;i++)
{
scanf("%d%d",&x,&y);
op[x][y]=op[y][x]=i;
edg[x].push_back(node(y,));
edg[y].push_back(node(x,));
}
for(int i=;i<=n;i++)
{
if(!vis[i])
{
roo=i;
dis[i]=;
LCA(i);
}
}
for(int i=;i<c;i++)
{
if(cur[i]==-)
puts("Not connected");
else
printf("%d\n",cur[i]);
}
}
return ;
}

Connections between cities的更多相关文章

  1. hdu 2874 Connections between cities [LCA] (lca->rmq)

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  2. HDU 2874 Connections between cities(LCA Tarjan)

    Connections between cities [题目链接]Connections between cities [题目类型]LCA Tarjan &题意: 输入一个森林,总节点不超过N ...

  3. hdu 2874 Connections between cities 带权lca判是否联通

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  4. hdu 2874 Connections between cities(st&rmq LCA)

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  5. hdu 2874 Connections between cities (并查集+LCA)

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  6. hdu-2874 Connections between cities(lca+tarjan+并查集)

    题目链接: Connections between cities Time Limit: 10000/5000 MS (Java/Others)     Memory Limit: 32768/327 ...

  7. HDU 2874 Connections between cities(LCA)

    题目链接 Connections between cities LCA的模板题啦. #include <bits/stdc++.h> using namespace std; #defin ...

  8. HDU——2874 Connections between cities

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  9. Connections between cities LCA

    Problem Description After World War X, a lot of cities have been seriously damaged, and we need to r ...

随机推荐

  1. JVM 菜鸟进阶高手之路九(解惑)

    转载请注明原创出处,谢谢! 在第八系列最后有些疑惑的地方,后来还是在我坚持不懈不断打扰笨神,阿飞,ak大神等,终于解决了该问题.第八系列地址:http://www.cnblogs.com/lirenz ...

  2. A glimpse of Support Vector Machine

    支持向量机(support vector machine, 以下简称svm)是机器学习里的重要方法,特别适用于中小型样本.非线性.高维的分类和回归问题.本篇希望在正篇提供一个svm的简明阐述,附录则提 ...

  3. 【个人笔记】《知了堂》node.js简介及创建应用

    Node.js是什么? Node.js是建立在谷歌Chrome的JavaScript引擎(V8引擎)的Web应用程序框架.Node.js自带运行时环境可在Javascript脚本的基础上可以解释和执行 ...

  4. 关于MySQL 事务,视图,索引,数据库备份,恢复

      /*创建数据库*/ CREATE DATABASE `mybank`;/*创建表*/USE mybank;CREATE TABLE `bank`(    `customerName` CHAR(1 ...

  5. 如何解决Python.h:No such file or directory

    安装python2.7对应的dev sudo apt-get install python-dev 安装python3.6对应的dev sudo apt-get install python3-dev

  6. Long Long Message (poj2774 后缀数组求最长公共子串)

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 19206   Accepted: 79 ...

  7. vue环境搭建

    1.Window 上安装Node.js 1.Windows 安装包(.msi) 32 位安装包下载地址 : https://nodejs.org/dist/v4.4.3/node-v4.4.3-x86 ...

  8. Thirft框架快速入门

    Thrift介绍1.什么是thrift?thrift早期由facebook内部团队开发,主要用于实现跨语言间的方法调用,属于远程方法调用的一种,后开源纳入apache中,成为了apache thrif ...

  9. Android ListView getViewTypeCount 的返回值问题解决

    最近在学慕课网上的一个实战课程,期间有一个智能聊天机器人模块. 聊天界面通过 ListView 显示,用 Adapter 加载.一般来说,单对单的聊天,两者发出的话分别列在聊天页面的左右两边.所以,在 ...

  10. 关于Elixir游戏服设计系列

    写着写着就废球了,感觉空对空,实在没什么意思. 另外很快就要搞新项目,决定新项目就直接上elixir了.目前该做的准备工作已经探索了一些了. 以下的东西是写给同事参考的,感兴趣的可以看看,提建议更好. ...