山东省第六届ACM程序设计竞赛

本文介绍了一种基于社交网络的朋友圈求助算法。通过Tarjan算法寻找强连通分量,并结合BFS或SPFA算法来确定最少的求助成本。适用于解决特定类型的最短路径问题。

Circle of Friends

  2000 ms         65536 KiB
Submit Status My Status  Origin

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.

Sample

Input

Copy3 
4 4 
0 1
1 2 
2 1 
2 3  

3 3 
0 1 
1 2 
2 1 
 
3 1 
0 1

Output

Copy2 
1 
-1
Submit Status My Status  Origin

看懂题目之后就觉得是一个板子题目,但是对于板子不熟悉,导致了一个点问题,那个low和dfn数组玄学存的啥

判断是否在一个强联通分块还是用Scc数组

题意:给你n个人和m个关系,问你最后0可以到n-1不

但是有个前提,假如某几个点成环,那么他们之间是不需要花费什么就可以问问题,如果不是成环的哪种真朋友关系就需要花费1来问问题问你最少花费是多少,如果不能到输出-1.  tarjan缩点,跑一下bfs就行,也可以缩点之后,同一个强联通分块距离改成0 其余都是1 然后跑一下spfa也行。

#include<bits/stdc++.h> 
using namespace std;
const int MAX=100005; 
struct node{  
    int s,t,next;  
}e[MAX]; 
struct Node{
	int v;
	int step;
}; 
int head[MAX],cnt;  
void add(int u,int v)  
{  
    e[cnt]=node{u,v,head[u]};  
    head[u]=cnt++;  
}  
int dfn[MAX];  //每个节点的访问时间编号  
int low[MAX];  //每个点能到达的最小编号  
int sta[MAX],top;  
int Scc[MAX];  //每个点所属的分量 序号  
int vis[MAX];
void tardfs(int u,int &lay,int &sig)  
{  
    low[u]=dfn[u]=lay++;  //到达此点的时间  
    sta[top++]=u;  //压入栈  
    for(int i=head[u];~i;i=e[i].next)  
    {  
        int t=e[i].t;  
        if(dfn[t]==0)  
        {  
            tardfs(t,lay,sig);  
            low[u]=min(low[u],low[t]);  
        }  
        else if(!Scc[t])//访问过了  
            low[u]=min(low[u],dfn[t]);//强连通求法可以用low  
    }  
    if(low[u]==dfn[u])//u不能到达任何之前走过的点  
    {  
        sig++;  
        while(1)  
        {  
            int j=sta[--top];//出栈  
            Scc[j]=sig;  
            if(j==u)
				break;  
        }//包含u的连通分量出栈  
    }  
}  
int tarjan(int n)  
{  
    int sig=0;//强连通数  
    int lay=1;//时间戳  
    top=0;  
    memset(Scc,0,sizeof(Scc));  
    memset(dfn,0,sizeof(dfn));//时间戳归0  
    for(int i=0;i<n;i++)  
    {  
        if(!dfn[i])
			tardfs(i,lay,sig);  
    }  
    return sig;//返回连通数  
}  
int main()  
{  
    int n,m,u,v,T;  
    cin>>T;  
    while(T--)  
    {  
        cin>>n>>m;  
        memset(head,-1,sizeof(head));  
        memset(vis,0,sizeof(vis));
        memset(low,0,sizeof(low));
        memset(sta,0,sizeof(low));
        memset(e,0,sizeof(e));
        cnt=0;  
        int flag=0;
        while(m--)  
        {  
            scanf("%d%d",&u,&v);  
            add(u,v); 
			if(u==0 && v==n-1)
				flag=1; 
        }  
        int sig=tarjan(n); 
           queue<Node>q;
           vis[0]=1;
		   int ans=1e9;
		   Node t;
		   t.v=0;
		   t.step=0;
		   q.push(t);
		   while(!q.empty())
		   {
		   		Node temp=q.front();
		   		q.pop();
		   		for(int i=head[temp.v];i!=-1;i=e[i].next)
		   		{
		   			int v=e[i].t;
		   			if(v==(n-1))
		   			{
		   				if(Scc[temp.v]==Scc[v])
		   					ans=min(ans,temp.step);
		   				else
		   					ans=min(ans,temp.step+1);
						continue;
					}
		   			if(vis[v]==0)
		   			{
					   	vis[v]=1;
			   			if(Scc[temp.v]==Scc[v])
			   			{
			   				Node la;
			   				la.step=temp.step;
			   				la.v=v;
			   				q.push(la);
						}
						else
						{
							Node la;
							la.step=temp.step+1;
							la.v=v;
							q.push(la); 
						}
					}
				}
		   }
		    if(ans==1e9)
		   		printf("-1\n");
		   	else
		   		printf("%d\n",ans);
    } 
	return 0; 
} 
#include<bits/stdc++.h> //spfa
using namespace std;
const int MAX=100005; 
const int INF=0x3f3f3f3f;
struct node{  
    int s,t,next;  
    int dist;
}e[MAX]; 
struct Node{
	int v;
	int step;
}; 
int head[MAX],cnt,n;  
void add(int u,int v)  
{  
    e[cnt]=node{u,v,head[u],1};  
    head[u]=cnt++;  
}  
int dfn[MAX];  //每个节点的访问时间编号  
int low[MAX];  //每个点能到达的最小编号  
int sta[MAX],top;  
int Scc[MAX];  //每个点所属的分量 序号  
int vis[MAX];
int dis[MAX];
void tardfs(int u,int &lay,int &sig)  
{  
    low[u]=dfn[u]=lay++;  //到达此点的时间  
    sta[top++]=u;  //压入栈  
    for(int i=head[u];~i;i=e[i].next)  
    {  
        int t=e[i].t;  
        if(dfn[t]==0)  
        {  
            tardfs(t,lay,sig);  
            low[u]=min(low[u],low[t]);  
        }  
        else if(!Scc[t])//访问过了  
            low[u]=min(low[u],dfn[t]);//强连通求法可以用low  
    }  
    if(low[u]==dfn[u])//u不能到达任何之前走过的点  
    {  
        sig++;  
        while(1)  
        {  
            int j=sta[--top];//出栈  
            Scc[j]=sig;  
            if(j==u)
				break;  
        }//包含u的连通分量出栈  
    }  
}  
int tarjan(int n)  
{  
    int sig=0;//强连通数  
    int lay=1;//时间戳  
    top=0;  
    memset(Scc,0,sizeof(Scc));  
    memset(dfn,0,sizeof(dfn));//时间戳归0  
    for(int i=0;i<n;i++)  
    {  
        if(!dfn[i])
			tardfs(i,lay,sig);  
    }  
    return sig;//返回连通数  
}  
void spfa()
{  
    memset(dis,INF,sizeof(dis)); 
	memset(vis,0,sizeof(vis)); 
    queue<int> Q;  
    Q.push(0);  
    vis[0]=1;  
    dis[0]=0;  
    while (!Q.empty())
	{  
        int u=Q.front();  
        Q.pop();  
        vis[u]=0;  
        for(int i=head[u];i!=-1;i=e[i].next)
		{  
            int v=e[i].t;  
            if(dis[v]>dis[u]+e[i].dist)
			{  
                dis[v]=dis[u]+e[i].dist;  
                if (!vis[v])
				{  
                    vis[v]=1;  
                    Q.push(v);  
                }  
            }  
        }  
    }  
    if (dis[n-1]==INF) printf ("-1\n");  
    else printf ("%d\n",dis[n-1]);  
}  
int main()  
{  
    int m,u,v,T;  
    cin>>T;  
    while(T--)  
    {  
        cin>>n>>m;  
        memset(head,-1,sizeof(head));  
        memset(vis,0,sizeof(vis));
        memset(low,0,sizeof(low));
        memset(sta,0,sizeof(low));
        memset(e,0,sizeof(e));
        cnt=0;  
        int flag=0;
        while(m--)  
        {  
            scanf("%d%d",&u,&v);  
            add(u,v); 
			if(u==0 && v==n-1)
				flag=1; 
        }  
        int sig=tarjan(n); 
        for(int i=0;i<cnt;i++)
        {
        	int u=e[i].s;
        	int v=e[i].t;
        	if(Scc[u]==Scc[v])
        		e[i].dist=0;
		}
		spfa();
    } 
	return 0; 
}  


【电力系统】单机无穷大电力系统短路故障暂态稳定Simulink仿真(带说明文档)内容概要:本文档围绕“单机无穷大电力系统短路故障暂态稳定Simulink仿真”展开,提供了完整的仿真模型与说明文档,重点研究电力系统在发生短路故障后的暂态稳定性问题。通过Simulink搭建单机无穷大系统模型,模拟不同类型的短路故障(如三相短路),分析系统在故障期间及切除后的动态响应,包括发电机转子角度、转速、电压和功率等关键参数的变化,进而评估系统的暂态稳定能力。该仿真有助于理解电力系统稳定性机理,掌握暂态过程分析方法。; 适合人群:电气工程及相关专业的本科生、研究生,以及从事电力系统分析、运行与控制工作的科研人员和工程师。; 使用场景及目标:①学习电力系统暂态稳定的基本概念与分析方法;②掌握利用Simulink进行电力系统建模与仿真的技能;③研究短路故障对系统稳定性的影响及提高稳定性的措施(如故障清除时间优化);④辅助课程设计、毕业设计或科研项目中的系统仿真验证。; 阅读建议:建议结合电力系统稳定性理论知识进行学习,先理解仿真模型各模块的功能与参数设置,再运行仿真并仔细分析输出结果,尝试改变故障类型或系统参数以观察其对稳定性的影响,从而深化对暂态稳定问题的理解。
本研究聚焦于运用MATLAB平台,将支持向量机(SVM)应用于数据预测任务,并引入粒子群优化(PSO)算法对模型的关键参数进行自动调优。该研究属于机器学习领域的典型实践,其核心在于利用SVM构建分类模型,同时借助PSO的全局搜索能力,高效确定SVM的最优超参数配置,从而显著增强模型的整体预测效能。 支持向量机作为一种经典的监督学习方法,其基本原理是通过在高维特征空间中构造一个具有最大间隔的决策边界,以实现对样本数据的分类或回归分析。该算法擅长处理小规模样本集、非线性关系以及高维度特征识别问题,其有效性源于通过核函数将原始数据映射至更高维的空间,使得原本复杂的分类问题变得线性可分。 粒子群优化算法是一种模拟鸟群社会行为的群体智能优化技术。在该算法框架下,每个潜在解被视作一个“粒子”,粒子群在解空间中协同搜索,通过不断迭代更新自身速度与位置,并参考个体历史最优解和群体全局最优解的信息,逐步逼近问题的最优解。在本应用中,PSO被专门用于搜寻SVM中影响模型性能的两个关键参数——正则化参数C与核函数参数γ的最优组合。 项目所提供的实现代码涵盖了从数据加载、预处理(如标准化处理)、基础SVM模型构建到PSO优化流程的完整步骤。优化过程会针对不同的核函数(例如线性核、多项式核及径向基函数核等)进行参数寻优,并系统评估优化前后模型性能的差异。性能对比通常基于准确率、精确率、召回率及F1分数等多项分类指标展开,从而定量验证PSO算法在提升SVM模型分类能力方面的实际效果。 本研究通过一个具体的MATLAB实现案例,旨在演示如何将全局优化算法与机器学习模型相结合,以解决模型参数选择这一关键问题。通过此实践,研究者不仅能够深入理解SVM的工作原理,还能掌握利用智能优化技术提升模型泛化性能的有效方法,这对于机器学习在实际问题中的应用具有重要的参考价值。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值