首页 > 其他分享 >spfa---模板

spfa---模板

时间:2023-09-12 11:38:53浏览次数:32  
标签:int 个数 --- spfa include 模板 define


spfa模板


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
using namespace std;
#define V 1010  //点的个数
#define E 4020  //变的个数*2(双向边)
#define INF 0x3f3f3f3f

struct node
{
	int a,b,len;
}p[E];
int nex[E];
int first[V],f[V],dis[V];
int n,m,ans;

void spfa()
{
	int i;
	queue<int> q;
	for(i=0;i<=n;i++)
	{
		dis[i]=INF;
		f[i]=0;
	}
	dis[1]=0;
	f[1]=1;
	q.push(1);
	while(!q.empty())
	{
		int t=q.front();
		q.pop();
		for(i=first[t];i!=-1;i=nex[i])
		{
			if(dis[t]+p[i].len < dis[p[i].b])
			{
				dis[p[i].b]=dis[t]+p[i].len;
				if(f[p[i].b]==0)
				{
					q.push(p[i].b);
					f[p[i].b]=1;
				}
			}
		}
		f[t]=0;
	}
}
void addedge(int a,int b,int c)
{
	p[ans].a=a;
	p[ans].b=b;
	p[ans].len=c;
	nex[ans]=first[a];
	first[a]=ans;
	ans++;
}
int main()
{
	int i,j,k;
	scanf("%d%d",&n,&m);//n个点,m条边
	memset(first,-1,sizeof(first));
	ans=0;
	while(m--)
	{
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		addedge(a,b,c);
		addedge(b,a,c); //双向边
	}
	spfa();
	printf("%d\n",dis[n]);
	system("pause");
}




标签:int,个数,---,spfa,include,模板,define
From: https://blog.51cto.com/u_16244339/7443636

相关文章

  • 矩阵快速幂--模板
    http://acm.bit.edu.cn/mod/programming/view.php?id=670TheLittleArchitectII#include<stdio.h>#include<string.h>//dp方程:f[n]=3*f[n-1]+3*f[n-2]-f[n-3];//矩阵快速幂。。模板//构造矩阵//310//301//-100structnode{ longlonga[3][3];};lon......
  • 最小生成树---模板
    最基础模板#include<stdio.h>#include<string.h>#include<algorithm>usingnamespacestd;#defineV110//点的个数#defineE5100//边的个数intparent[V];introot(intp){ if(parent[p]==-1)returnp; elsereturnparent[p]=root(parent[p]);}void......
  • 欧拉函数--模板
    欧拉函数--模板//求1..n-1中与n互质的数的个数inteular(intn){ intret=1,i; for(i=2;i*i<=n;i++) if(n%i==0){ n/=i,ret*=i-1; while(n%i==0) n/=i,ret*=i; } if(n>1) ret*=n-1; returnret;}......
  • 快速幂模板
    参数数据类型,可改为来longlong,__int64.//m^n%kintquickpow(intm,intn,intk){intb=1;while(n>0){if(n&1)b=(b*m)%k;n=n>>1;m=(m*m)%k;}returnb;}......
  • Python - 接口自动化(Requests)
    1、requests简介如果想用python做接口测试,我们首先有不得不了解和学习的模块。它就是python的第三方模块:Requests。虽然Python内置有urllib模块用于访问网络资源。但是,它用起来比较麻烦,而且,缺少很多实用的高级功能。所以呢更好的方案是使用requests。它也是目前应用最广泛、最......
  • 二叉树 遍历 hdu-1710-Binary Tree Traversals
    给出二叉树的前序遍历和中序遍历,求后序遍历。。 算法:由前序遍历的第一个元素可确定左、右子树的根节点,参照中序遍历又可进一步确定子树的左、右子树元素。如此递归地参照两个遍历序列,最终构造出二叉树。 由前序和中序结果求后序遍历结果树的遍历: 给你一棵树的先......
  • RabbitMQ - Exception (504) Reason: "channel id space exhausted"
    使用go的第三方包:github.com/rabbitmq/amqp091-go出现报错:getmqchannelerror{"error":"Exception(504)Reason:channelidspaceexhausted"}ctx:=context.Background()results,err:=global.Redis.LRange(ctx,abListName,0,-1).Result()......
  • 无涯教程-JavaScript - PDURATION函数
    描述PDURATION函数返回投资达到指定值所需的周期数。PDURATION使用以下公式-$$PDURATION=\frac{log\left(指定值\right)-log\left(currentValue\right)}{log\left(1+rate\right)}$$WherespecifiedValue等于fvcurrentValue等于pv语法PDURATION(rate,pv,fv)......
  • Ubuntu Server 22.04 双网卡绑定 配置文件 Bond mode 1 : active-backup 主备模式
    UbuntuServer22.041.拓扑视图实例 2.备份配置文件修改前备份root@ax:~#cpetc/netplan/00-installer-config.yamletc/netplan/00-installer-config.yaml.orig修改配置文件,Ubuntu严格区分格式,空格缩进。简要说明:eno1-eno4,关闭dhcp;bond0只绑定eno1、eno2,实际可根据情况,绑定更多......
  • xilinx赛灵思下载器jtag-hs3兼容alinx仿真fpga烧录digilent高速常见问题解答
    1.概述  XJTAG-HS3是XILINX的USB转JTAG的高速仿真器,可以下载、烧录和仿真Xilinx FPGA和CPLD芯片,以及配置PROM、FLASH. XJTAG-HS3比PlatformCableUSBII下载器快10倍速度。 可以在30Mbit/秒下驱动JTAG/SPI总线,并且能实现对XilinxZYNQ平台处理器核的重置。可以支持ZYN......