UVA 11165 - Galactic Travel(BFS+twopointer+并查集)

UVA 11165 - Galactic Travel

题目链接

题意:给定一些不能走的边,要求出从s到t的最短路

思路:由于点数多,直接广搜会超时,所以加上优化,已经找过的点就不在重复找了,这点可以利用并查集进行优化,然后对于每个点的每个不能走的区间,可以先排序,然后利用twopointer的性质,每次可以从上次找到的位置往后找即可

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

const int N = 100005;

int t, n, m, parent[N], vis[N];

int find(int x) {
	return x == parent[x] ? x : parent[x] = find(parent[x]);
}

struct Ban {
	int v1, v2;
	Ban(int v1, int v2) {
		this->v1 = v1;
		this->v2 = v2;
	}
};

bool cmp(Ban a, Ban b) {
	return a.v1 < b.v1;
}

vector<Ban> b[N];

const int INF = 0x3f3f3f3f;

int bfs(int s, int t) {
	queue<int> Q;
	Q.push(s);
	vis[s] = 0;
	while (!Q.empty()) {
		int u = Q.front();
		Q.pop();
		if (u == t) return vis[u];
		int v = find(0);
		int s = 0;
		while (v < n) {
			if (v == u) v = find(v + 1);
			else {
				int flag = 1;
				for (int i = s; i < b[u].size(); i++) {
					if (b[u][i].v1 <= v && b[u][i].v2 >= v) {
						flag = 0;
						v = find(b[u][i].v2 + 1);
						s = i + 1;
						break;
					}
				}
				if (flag) {
					vis[v] = vis[u] + 1;
					Q.push(v);
					parent[v] = find(v + 1);
					v = find(v);
				}
			}
		}
	}
	return -1;
}

int main() {
	int cas = 0;
	scanf("%d", &t);
	while (t--) {
		scanf("%d%d", &n, &m);
		for (int i = 0; i <= n; i++) {
			parent[i] = i;
			b[i].clear();
		}
		int u, v1, v2;
		while (m--) {
			scanf("%d%d-%d", &u, &v1, &v2);
			if (v1 > v2) swap(v1, v2);
			b[u].push_back(Ban(v1, v2));
		}
		for (int i = 0; i < n; i++)
			sort(b[i].begin(), b[i].end(), cmp);
		int s, t;
		scanf("%d%d", &s, &t);
		printf("Case #%d: ", ++cas);
		int tmp = bfs(s, t);
		if (tmp == -1) printf("Impossible\n");
		else printf("%d\n", tmp);
	}
	return 0;
}
时间: 2024-10-11 00:31:37

UVA 11165 - Galactic Travel(BFS+twopointer+并查集)的相关文章

UVA - 11987 - Almost Union-Find (又是并查集~)

UVA - 11987 Almost Union-Find Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Problem A Almost Union-Find I hope you know the beautiful Union-Find structure. In this problem, you're to implement som

ZOJ 3811 / 2014 牡丹江赛区网络赛 C. Untrusted Patrol bfs/dfs/并查集

Untrusted Patrol Time Limit: 3 Seconds                                     Memory Limit: 65536 KB Edward is a rich man. He owns a large factory for health drink production. As a matter of course, there is a large warehouse in the factory. To ensure t

2017省夏令营Day8 【bfs,并查集】

题解:出题人丧心病狂~ 对于这道题,我们对每一个内应节点bfs,并用并查集维护,如果s和t联通,输出答案并break. PS几个小细节:①对于每个内应dis=0,为了保证不会对答案产生影响,我们在每2个节点中插入一个新的节点即可: ②因为加入新节点,数组要开大些,否则会炸. 代码如下: 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #define Max 2020304 5 using nam

UVA 3027 Corporative Network 带权并查集、

题意:一个企业要去收购一些公司把,使的每个企业之间互联,刚开始每个公司互相独立 给出n个公司,两种操作 E I:询问I到I它连接点最后一个公司的距离 I I J:将I公司指向J公司,也就是J公司是I公司的上级,距离为abs(I-J)%1000(貌似G++不支持abs,PE了两发) 思路:转化一下题意就行了,首先刚开始的时候每个公司都是独立的,I操作就是并查集中合并操作,将I这课树并到J这个树上, E操作要求的东西就是 I到I的根节点的距离,先看一个没有路径压缩直接暴力的方法把.(本以为不会过的,

Uva 12361 File Retrieval 后缀数组+并查集

题意:有F个单词,1 <= F <=60 , 长度<=10^4, 每次可以输入一个字符串,所有包含该字串的单词会形成一个集合. 问最多能形成多少个不同的集合.集合不能为空. 分析:用后缀数组处理.然后首先考虑一个单词形成一个集合的情况,若该单词是其他单词的字串,则该单词显然不会形成一个集合,那么利用后缀数组, 对于每个单词看能否与其他单词有LCP,且LCP 长度为该单词本身长度. 然后就是多个单词形成集合的情况:比较简单的处理方式就是将h数组值相同的下标集中存储,比如h[x] = h[y

UVA alive 4487 Exclusive-OR(加权并查集+异或运算的理解)

You are not given n non-negative integers X0, X1,..., Xn-1 less than 220, but they do exist, and their values never change. I'll gradually provide you some facts about them, and ask you some questions. There are two kinds of facts, plus one kind of q

UVA 1151 Buy or build(并查集之六)

题意:n个结点,以及m个组合,可以任意选择.求最小花费. 因为组合最多只有8个,可以枚举. #include<stdio.h> #include<math.h> #include<algorithm> #include<string.h> #define MAX 1007 #define INTMAX (int)1e9 using namespace std; struct list_buy { int num; int price; int node[MA

UVA - 10596 - Morning Walk (欧拉回路!并查集判断回路)

UVA - 10596 Morning Walk Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Problem H Morning Walk Time Limit 3 Seconds Kamal is a Motashota guy. He has got a new job in Chittagong . So, he has moved to Ch

Travel(HDU 5441 2015长春区域赛 带权并查集)

Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2404    Accepted Submission(s): 842 Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is tr