Connections between cities

Connections between cities

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 566    Accepted Submission(s): 178

 
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.
 
刚开始以为用Floyd做,结果内存不够。于是搜了LCA算法。
 
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;

struct Node{
	int len;  // 权值 
	int num;  // 顶点编号 
};
const int MAXN = 10000 + 10;
const int MAXC = 1000000 + 10;
int p[MAXN], ans[MAXC];
int n, m, c;
vector<Node> nodes1[MAXN];
vector<Node> nodes2[MAXN];
int dist[MAXN], vis[MAXN];  //dist距根节点的距离, vis是否访问过,记录成其根节点否则为-1 

void init(){
	memset(dist, 0, sizeof(dist));
	memset(ans, 0, sizeof(ans));
	memset(vis, 0, sizeof(vis));
	
	for(int i = 1; i <= n; i++){
		p[i] = i;
		nodes1[i].clear();
		nodes2[i].clear();
	}
}
int find_set(int x){
	if(x != p[x]){
		p[x] = find_set(p[x]);
	}
	return p[x];
}
void union_set(int x, int y){
	int i = find_set(x);
	int j = find_set(y);
	p[j] = i;
}
void dfs(int u, int len, int root){
	vis[u] = root;
	dist[u] = len;
	for(int i = 0; i < nodes2[u].size(); i++){
		int v = nodes2[u][i].num;
		if(vis[v] == root){  //是同一个根
			 ans[nodes2[u][i].len] = dist[u] + dist[v] - 2 * dist[find_set(v)];
		}
		else if(vis[v]){  //访问过,但不是同一个根 
			ans[nodes2[u][i].len] = -1;
		} 
		
	}
	for(int i = 0; i < nodes1[u].size(); i++){
		int v = nodes1[u][i].num;
		int w = nodes1[u][i].len;
		if(!vis[v]){
			dfs(v, len + w, root);
			union_set(u, v);
		}
	}
}
int main(){
	int i, j, k;
	Node cur;
	while(~scanf("%d%d%d", &n, &m, &c)){
		init();
		for(int s = 0; s < m; s++){
			scanf("%d%d%d", &i, &j, &k);
			cur.len = k;
			cur.num = j;	
			nodes1[i].push_back(cur);  //i - > j len
			cur.num = i;
			nodes1[j].push_back(cur);  //j - > i len
		}
		for(int s = 0; s < c; s++){
			scanf("%d%d", &i, &j);
			cur.len = s;
			cur.num = j;
			nodes2[i].push_back(cur);  // i -> j len (len代表编号) 
			cur.len = s;
			cur.num = i;
			nodes2[j].push_back(cur);	//j -> i len
		}
		for(int s = 1; s <= n; s++){
			if(!vis[s]){
				dfs(s, 0, s);  //以i为根 
			} 
		}
		for(int s = 0; s < c; s++){
			if(ans[s] == -1) printf("Not connected\n");
			else printf("%d\n", ans[s]);
		}
	}
	return 1;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值