Connections between cities (hdu 2874 LCA)
2015-09-18 16:22
211 查看
Connections between cities
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6908 Accepted Submission(s): 1798
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 | We have carefully selected several similar problems for you: 2873 2876 2872 2875 2877
题意:n个点m条边c次询问,每次询问(u,v)之间的最短距离,若不联通,输出“Not connected”,否则输出最短距离,没有环。
思路:LCA,但是给的图是一个森林,每次找到子树Tarjan,还有一种方法就是添加根root,把所有树连成一棵树再一次LCA。数组开小了它却返回TLE,郁闷我半天。。。
代码:
#include <iostream> #include <functional> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #pragma comment (linker,"/STACK:102400000,102400000") #define pi acos(-1.0) #define eps 1e-6 #define lson rt<<1,l,mid #define rson rt<<1|1,mid+1,r #define FRE(i,a,b) for(i = a; i <= b; i++) #define FREE(i,a,b) for(i = a; i >= b; i--) #define FRL(i,a,b) for(i = a; i < b; i++) #define FRLL(i,a,b) for(i = a; i > b; i--) #define mem(t, v) memset ((t) , v, sizeof(t)) #define sf(n) scanf("%d", &n) #define sff(a,b) scanf("%d %d", &a, &b) #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c) #define pf printf #define DBG pf("Hi\n") typedef long long ll; using namespace std; #define INF 0x3f3f3f3f #define mod 1000000009 const int maxn = 10005; const int MAXN = 200005; const int MAXM = 200010; const int N = 1005; struct Edge { int u,v,w,next; }edge[MAXN]; struct Query { int q,id,next; }query[2000000+10]; int n,m,c; int head[maxn],hed[maxn]; int father[maxn],dis[maxn]; int tot1,tot2; bool vis[maxn],vis2[maxn]; int ans[1000000+10]; void init() { tot1=tot2=0; memset(head,-1,sizeof(head)); memset(hed,-1,sizeof(hed)); memset(vis2,false,sizeof(vis2)); memset(dis,0,sizeof(dis)); memset(ans,-1,sizeof(ans)); for (int i=0;i<=n;i++) father[i]=i; } void addedge(int u,int v,int w) { edge[tot1].u=u; edge[tot1].v=v; edge[tot1].w=w; edge[tot1].next=head[u]; head[u]=tot1++; } void addQuery(int u,int v,int id) { query[tot2].q=v; query[tot2].id=id; query[tot2].next=hed[u]; hed[u]=tot2++; } int find_father(int x) { if (x!=father[x]) father[x]=find_father(father[x]); return father[x]; } void Union(int u,int v) { father[find_father(v)]=find_father(u); } void Tarjan(int u,int d) { vis[u]=true; vis2[u]=true; dis[u]=d; for (int i=head[u];~i;i=edge[i].next) { int v=edge[i].v; if (vis[v]) continue; Tarjan(v,d+edge[i].w); Union(u,v); } for (int i=hed[u];~i;i=query[i].next) { int v=query[i].q; if (!vis[v]) continue; ans[query[i].id]=dis[u]+dis[v]-2*dis[find_father(v)]; } } int main() { #ifndef ONLINE_JUDGE freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin); #endif int i,j,u,v,w; while (~scanf("%d%d%d",&n,&m,&c)) { init(); for (i=0;i<m;i++) { scanf("%d%d%d",&u,&v,&w); vis2[v]=true; addedge(u,v,w); addedge(v,u,w); } for (i=0;i<c;i++) { scanf("%d%d",&u,&v); addQuery(u,v,i); addQuery(v,u,i); } for (i=1;i<=n;i++) { if (!vis2[i]) { memset(vis,false,sizeof(vis)); Tarjan(i,0); } } for (i=0;i<c;i++) { if (ans[i]==-1) printf("Not connected\n"); else printf("%d\n",ans[i]); } } return 0; }
相关文章推荐
- 从内存和内存的分区的角度来理解数据类型 、变量、指针。
- 2 Add Two Numbers
- 黑马程序员——ios开发基础之OC中的面向对象以及对象特性
- 2015.9.18笔记
- Hadoop单节点环境搭建
- Cocos2d-JS 创建node节点示例
- The resource could not be loaded because the App Transport Security policy requires the use of a sec
- CodeForces 13A - Numbers
- socket select函数的详细讲解
- HDOJ 4857 逃生(拓扑排序(邻接表+反向建图,逆序输出+优先队列))
- 倒排索引
- VB.NET与C# 语法show差异
- SSH深度历险记(八) 剖析SSH核心原则+Spring依赖注入的三种方式
- Head First Python学习笔记1
- testng dataprovider 的几种用法以及Java中的二维数组
- 小结(jquery,ui,flexgrid,数据库资源)
- socket 的select函数例子
- python多线程编程:生成者和消费者
- android系统平台显示驱动开发简要:LCD驱动调试篇『四』
- Cocos2d-js v3.1 事件分发机制