SPFA首题!hdu-2544

本文深入探讨了SPFA算法的原理与应用,通过简洁的代码实现,展示其在求解最短路径问题上的高效性。同时,对比Bellman-Ford算法,SPFA在特定条件下展现出更优性能。
Llama Factory

Llama Factory

模型微调
LLama-Factory

LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调

刚才看了一下SPFA,很简单。时间有限,见BAIDU。说是BELLMAN的优化,不过我觉得更像DIJKSTRA

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 200
#define QSIZE 200
#define INF 9999999
int f[SIZE][SIZE];
int d[SIZE];
int inq[SIZE];
int que[QSIZE],head,tail,len;
int N,M;

int push(int x)
{
	que[tail++]=x;
	len++;
	if(tail==QSIZE)
		tail=0;
	return 0;
}

int pop(int *x)
{
	*x=que[head++];
	len--;
	if(head==QSIZE)
		head=0;
	return 0;
}

int main(void)
{
#ifndef ONLINE_JUDGE
	freopen("in","r",stdin);
#endif
	int i,x,t1,t2,t3;
	while(scanf("%d%d",&N,&M),M||N)
	{
		memset(f,0,sizeof(f));
		for(i=0;i<M;i++)
		{
			scanf("%d%d%d",&t1,&t2,&t3);
			f[t1][t2]=f[t2][t1]=t3;
		}
		for(i=1;i<=N;i++)
			inq[i]=0, d[i]=INF;
		head=tail=len=0;
		push(1);
		inq[1]=1;
		d[1]=0;
		while(len>0)
		{
			pop(&x);
			inq[x]=0;
			for(i=1;i<=N;i++)
				if(f[x][i] && d[x]+f[x][i]<d[i])
				{
					d[i]=d[x]+f[x][i];
					if(!inq[i])
						push(i),inq[i]=1;
				}
		}
		printf("%d\n",d[N]);
	}
	return 0;
}

			


 

您可能感兴趣的与本文相关的镜像

Llama Factory

Llama Factory

模型微调
LLama-Factory

LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调

源码地址: https://pan.quark.cn/s/3916362e5d0a 在C#编程平台下,构建一个曲线编辑器是一项融合了图形用户界面(GUI)构建、数据管理及数学运算的应用开发任务。 接下来将系统性地介绍这个曲线编辑器开发过程中的核心知识点:1. **定制曲线面板展示数据曲线**: - 控件选用:在C#的Windows Forms或WPF框架中,有多种控件可用于曲线呈现,例如PictureBox或用户自定义的UserControl。 通过处理重绘事件,借助Graphics对象执行绘图动作,如运用DrawCurve方法。 - 数据图形化:通过线性或贝塞尔曲线连接数据点,以呈现数据演变态势。 这要求掌握直线与曲线的数学描述,例如两点间的直线公式、三次贝塞尔曲线等。 - 坐标系统与缩放比例:构建X轴和Y轴,设定坐标标记,并开发缩放功能,使用户可察看不同区间内的数据。 2. **在时间轴上配置多个关键帧数据**: - 时间轴构建:开发一个时间轴组件,显示时间单位刻度,并允许用户在特定时间点设置关键帧。 时间可表现为连续形式或离散形式,关键帧对应于时间轴上的标识。 - 关键帧维护:利用数据结构(例如List或Dictionary)保存关键帧,涵盖时间戳和关联值。 需考虑关键帧的添加、移除及调整位置功能。 3. **调整关键帧数据,通过插值方法获得曲线**: - 插值方法:依据关键帧信息,选用插值方法(如线性插值、样条插值,特别是Catmull-Rom样条)生成平滑曲线。 这涉及数学运算,确保曲线在关键帧之间无缝衔接。 - 即时反馈:在编辑关键帧时,即时刷新曲线显示,优化用户体验。 4. **曲线数据的输出**: - 文件类型:挑选适宜的文件格式存储数据,例如XML、JSON或...
以下是hdu4310的Java解法: ```java import java.util.*; import java.io.*; public class Main { static int MAXN = 100010; static int MAXM = 200010; static int INF = 0x3f3f3f3f; static int n, m, s, t, cnt; static int[] head = new int[MAXN]; static int[] dis = new int[MAXN]; static boolean[] vis = new boolean[MAXN]; static int[] pre = new int[MAXN]; static int[] cur = new int[MAXN]; static class Edge { int to, next, cap, flow, cost; public Edge(int to, int next, int cap, int flow, int cost) { this.to = to; this.next = next; this.cap = cap; this.flow = flow; this.cost = cost; } } static Edge[] edge = new Edge[MAXM]; static void addEdge(int u, int v, int cap, int flow, int cost) { edge[cnt] = new Edge(v, head[u], cap, flow, cost); head[u] = cnt++; edge[cnt] = new Edge(u, head[v], 0, 0, -cost); head[v] = cnt++; } static boolean spfa() { Arrays.fill(dis, INF); Arrays.fill(vis, false); Queue<Integer> q = new LinkedList<>(); q.offer(s); dis[s] = 0; vis[s] = true; while (!q.isEmpty()) { int u = q.poll(); vis[u] = false; for (int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if (edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost) { dis[v] = dis[u] + edge[i].cost; pre[v] = u; cur[v] = i; if (!vis[v]) { vis[v] = true; q.offer(v); } } } } return dis[t] != INF; } static int[] MCMF(int s, int t) { int flow = 0, cost = 0; while (spfa()) { int f = INF; for (int u = t; u != s; u = pre[u]) { f = Math.min(f, edge[cur[u]].cap - edge[cur[u]].flow); } for (int u = t; u != s; u = pre[u]) { edge[cur[u]].flow += f; edge[cur[u] ^ 1].flow -= f; cost += edge[cur[u]].cost * f; } flow += f; } return new int[]{flow, cost}; } public static void main(String[] args) { Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))); int T = in.nextInt(); for (int cas = 1; cas <= T; cas++) { n = in.nextInt(); m = in.nextInt(); s = 0; t = n + m + 1; cnt = 0; Arrays.fill(head, -1); for (int i = 1; i <= n; i++) { int c = in.nextInt(); addEdge(s, i, c, 0, 0); } for (int i = 1; i <= m; i++) { int c = in.nextInt(); addEdge(i + n, t, c, 0, 0); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { int c = in.nextInt(); addEdge(i, j + n, INF, 0, c); } } int[] ans = MCMF(s, t); System.out.printf("Case #%d: %d\n", cas, ans[1]); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值