SDUT 3262 Circle of Friends【强连通Tarjan+缩点染色+最短路SPFA】
来源:互联网 发布:为知笔记活动 编辑:程序博客网 时间:2024/05/20 20:03
Circle of Friends
Problem Description
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.
Input
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.
Output
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.
Example Input
3 4 4 0 11 2 2 1 2 3 3 3 0 1 1 2 2 1 3 1 0 1
Example Output
2 1 -1
Hint
Author
题目大意:给你N个点,M个关系,关系A B,表示A认识B.
如果A认识B而且B认识A.那么如果现在我是A我想找到B就不需要别人帮我介绍我自然认识.
如果A认识B但是B不认识A.那么我们现在就需要花费1单位来认识B.
现在让你求0想要认识n-1的最小花费。
思路:
很显然的最短路问题,问题关键在于对于有向环的处理。
如果我们现在是一个DAG图,那么很显然就不需要考虑环的问题了。
那么我们预处理图将其变成一个DAG图即可,这里Tarjan强连通缩点染色很方便就处理出来了。
问题很裸,不需要太多思考过程,主要是代码实现熟练程度问题。
Ac代码:
#include<stdio.h>#include<string.h>#include<queue>#include<vector>using namespace std;vector<int >mp2[150000];vector<int >mp[150000];int stack[100800];int vis[100800];int low[100800];int color[100800];int dfn[100800];int dist[100800];int n,m,cnt,sig,tt;void Tarjan(int u){ vis[u]=1; low[u]=dfn[u]=cnt++; stack[++tt]=u; for(int i=0;i<mp[u].size();i++) { int v=mp[u][i]; if(vis[v]==0) { Tarjan(v); } if(vis[v]==1)low[u]=min(low[u],low[v]); } if(dfn[u]==low[u]) { sig++; do { color[stack[tt]]=sig; low[stack[tt]]=sig; vis[stack[tt]]=2; }while(stack[tt--]!=u); }}void SPFA(int ss){ memset(vis,0,sizeof(vis)); for(int i=0;i<=n;i++)dist[i]=0x3f3f3f3f; queue<int >s; s.push(ss); dist[ss]=0; while(!s.empty()) { int u=s.front(); s.pop();vis[u]=0; for(int i=0;i<mp2[u].size();i++) { int v=mp2[u][i]; if(dist[v]>dist[u]+1) { dist[v]=dist[u]+1; if(vis[v]==0) { vis[v]=1; s.push(v); } } } } int ennd=color[n-1]; if(dist[ennd]==0x3f3f3f3f)printf("-1\n"); else printf("%d\n",dist[ennd]);}void Slove(){ cnt=1; sig=0,tt=-1; for(int i=0;i<=n;i++)mp2[i].clear(); memset(vis,0,sizeof(vis)); for(int i=0;i<n;i++) { if(vis[i]==0) { Tarjan(i); } } for(int i=0;i<n;i++) { for(int j=0;j<mp[i].size();j++) { int u=color[i]; int v=color[mp[i][j]]; if(u!=v) { // printf("--%d %d\n",u,v); mp2[u].push_back(v); } } } int root=color[0]; SPFA(root);}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=0;i<=n;i++)mp[i].clear(); for(int i=0;i<m;i++) { int x,y; scanf("%d%d",&x,&y); mp[x].push_back(y); } Slove(); }}
- SDUT 3262 Circle of Friends【强连通Tarjan+缩点染色+最短路SPFA】
- sdut 3262 Circle of Friends (SCC缩点) 有向图 强连通分量模板
- SDUT-3262-Circle of Friends(强连通分量)
- sdut 3262 Circle of Friends tarjan
- sdut 3262 Circle of Friends (SCC缩点)
- Poj 3592 Instantaneous Transference【强连通Tarjan+染色缩点+SPFA】
- hrbust 1797 Fly【Tarjan强连通+缩点染色+最长路SPFA】好题~
- Codeforces 427C Checkposts【Tarjan强连通+缩点染色】
- poj 3114 Countries in War(强连通分量缩点+spfa求最短路)
- POJ 3114 Countries in War 强连通tarjan缩点后 跑最短路spfa
- 山东第六届省赛Circle of Friends(强联通缩点最短路)
- hdu 5934 Bomb【最小点基----Tarjan强连通+缩点染色】
- bzoj 1179 ATM tarjan缩点+spfa最短路
- 图论总结 Dijkstra Tarjan 最小生成树 二分图 最短路 强连通分量 双连通分量 Bellman-Ford SPFA 二分图染色 Kruskal Prim 网络流 二分图匹配 Dinic
- hdu 3062 Party【2-SAT入门---强连通Tarjan+染色缩点】
- Poj 3648 Wedding【2-Sat--------Tarjan强连通+缩点染色+拓扑排序】
- BZOJ 1179: [Apio2009]Atm Tarjan强连通分量缩点,SPFA,DP
- POJ2186 Tarjan强连通分量+缩点
- HDU5327(数位DP)
- bzoj3160
- spring aop基础知识
- 重载(Overlode) VS. 重写(Overriding)
- 二叉树16:从上往下打印二叉树
- SDUT 3262 Circle of Friends【强连通Tarjan+缩点染色+最短路SPFA】
- Java中 hashcode 和 equals、equals和==
- HDU 5236 Article
- FTP服务
- 二叉树17:按之字形顺序打印二叉树
- 友元函数和友元类~
- 快排的实现(Java)
- GitHub入门
- 架构之路知多少,开篇只为你知晓