东大oj-1591 Circle of friends

题目描述

Nowadays, "Circle of Friends" is a very popular social networking platform in WeChat. We can share our life to friends through it or get other's situation.
Similarly, in real life, there is also a circle of friends, friends would often get together communicating and playing to maintain friendship. And when you have difficulties, friends will generally come to help and ask nothing for return.
However, the friendship above is true friend relationship while sometimes you may regard someone as your friend but he doesn't agree.In this way when you ask him for help, he often asks you for a meal, and then he will help you.
If two people think they are friends mutually,they will become true friend,then once one of them has a problem or makes a query, the other one will offer help for free.What's more,if one relationship is similar to “A regards B as friend, B regards C as friend and C regards A as friend”,they will make a friends circle and become true friends too with each other. Besides, people will not ask those who they don’t regard as friends for help. If one person received a question and he can not solve it, he will ask his friends for help. 
Now, Nias encounters a big problem, and he wants to look for Selina's help. Given the network of friends, please return the minimum number of meals Nias must offer. Of course Nias is lavish enough, so he will pay for all the meals in the network of friends.

输入

The first line of input contains an integer T, indicating the number of test cases (T<=30).
For each test case, the first line contains two integers, N and M represent the number of friends in the Nias’s network and the number of relationships in that network. N and M are less than 100000 and you can assume that 0 is Nias and n-1 is Selina.
Next M lines each contains two integers A and B, represent a relationship that A regards B as his friend, A and B are between 0 and n-1.

输出

For each test case, please output the minimum number of meals Nias need to offer; if Nias can’t get Selina’s help, please output -1.

样例输入

3
4 4
0 1
1 2
2 1
2 3

3 3
0 1
1 2
2 1

3 1
0 1

样例输出

2
1
-1

题解

首先,深度优先求有向图强连通分量(Tarjan)算法

其次,构图,所构造出来的新图是一个拓扑图

最后,spfa求最短路径.

知识点:

spfa适用于稀疏图最短路径,dijstra适合稠密图最短路.

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn = 1e5 + 7;
int N, M;
struct Edge{
	int to, next;
}e[maxn*2];
int ei,g[maxn];
void push_back(int f, int t){ 
	e[++ei].to = t;
	e[ei].next = g[f];
	g[f] = ei;
}
int ti, pre[maxn], low[maxn],num[maxn],sccNo;
int a[maxn];
struct Stack{
	int a[maxn];
	int i;
	void init(){ i = 0; }
	void push(int x){a[i++] = x;}
	int pop(){return a[--i];}
}sta;
void dfs(int now){
	pre[now] = low[now] = ++ti;
	sta.push(now);
	for (int i = g[now]; i; i = e[i].next){
		int t = e[i].to;
		if (pre[t] == 0)dfs(t), low[now] = min(low[now], low[t]);
		else if (num[t] == 0)low[now] = min(low[now], pre[t]);
	}
	if (pre[now] == low[now]){
		++sccNo; 
		while (true){
			int x = sta.pop();
			num[x] = sccNo;
			if (x == now)break;
		}
	}
}
void newGraph(){
	memset(a, 0, sizeof(int)*(1+sccNo));
	for (int i = 0; i < N; i++){
		for (int j = g[i]; j; j = e[j].next){
			int t = e[j].to;
			if (num[i] ^ num[t]){
				e[++ei].next = a[num[i]];
				e[ei].to = num[t];
				a[num[i]] = ei;
			}
		}
	}
}
struct Q{
	int a[maxn], head, rear;
	bool has[maxn];
	void init(){ head = rear = 0; memset(has, 0, sizeof(int)*(sccNo+1)); }
	void enq(int x){ a[rear] = x; rear = (rear + 1) % maxn; has[x] = 1; }
	int deq(){ int ans = a[head]; head = (head + 1) % maxn; has[ans] = 0; return ans; }
}q;
int dis[maxn]; 
void spfa(){
	q.init();
	q.enq(num[0]);
	memset(dis, 0x34, sizeof(int)*(1 + sccNo));
	dis[num[0]] = 0;
	while (q.head^q.rear){
		int now = q.deq();
		for (int i = a[now]; i; i = e[i].next){
			int t = e[i].to;
			if (dis[t] > dis[now] + 1){
				dis[t] = dis[now] + 1;
				if (q.has[t]== false){
					q.enq(t);
				}
			}
		}
	}
	if (dis[num[N - 1]] == 0x34343434)printf("-1\n");
	else printf("%d\n", dis[num[N - 1]]);
}
int main(){
	freopen("in.txt", "r", stdin);
	int T; scanf("%d", &T);
	while (T--){
		scanf("%d%d", &N, &M);
		ei = 0, memset(g, 0, sizeof(int)*N);
		while (M--){
			int x, y; scanf("%d%d", &x, &y);
			push_back(x, y);
		}
		ti = 0, memset(pre, 0, sizeof(int)*N), memset(num, 0, sizeof(int)*N);
		sta.init(), sccNo = 0;
		for (int i = 0; i < N; i++)if (pre[i] == 0)dfs(i);
		newGraph(); 
		spfa();
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SDUT-OJ(Software Development University of Tsinghua Online Judge)是一个在线编程平台,提供给清华大学软件学院的学生和爱好者练习和解决算法问题的环境,其中包括各种计算机科学题目,包括数据结构、算法、图形等。对于"最小生成树"(Minimum Spanning Tree, MST)问题,它是图论中的经典问题,目标是从一个加权无向图中找到一棵包含所有顶点的树,使得树的所有边的权重之和最小。 在C语言中,最常见的是使用Prim算法或Kruskal算法来求解最小生成树。Prim算法从一个顶点开始,逐步添加与当前生成树相连且权重最小的边,直到所有顶点都被包含;而Kruskal算法则是从小到大对所有边排序,每次选取没有形成环的新边加入到树中。 如果你想了解如何用C语言实现这些算法,这里简单概括一下: - 通常使用优先队列(堆)来存储边和它们的权重,以便快速查找最小值。 - 从任意一个顶点开始,遍历与其相邻的边,若新边不形成环,就更新树,并将新边加入优先队列。 - Kruskal算法: - 先将所有的边按照权重升序排序。 - 创建一个空的最小生成树,然后依次取出排序后的边,如果这条边连接的两个顶点不在同一个连通分量,则将其添加到树中。 如果你需要更详细的代码示例,或者有具体的问题想了解(比如如何处理环、如何实现优先队列等),请告诉我,我会为你提供相应的帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值