BestCoder Round #74 Shortest Path

本文介绍了一道最短路径问题的竞赛题,通过优化算法避免了直接使用Floyd或SPFA计算所有点间最短路径带来的性能问题。仅计算三条额外边的六个端点到其他点的距离,从而大幅减少计算量。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5636

题意:给我们一条n个点组成的链,然后在上面加3条边,输出某两个点

题目一看就是最短路问题,但是一看点数:10^5,比赛的时候还真的没敢去做,觉得这题要算出所有点到其他点的最短距离,无论用Floyd还是SPFA都应该会炸时间,事后才发现并不需要求出所有点之间的距离,只需要求出加三条边的那6个点到其他点的距离,因为u,v两点的距离如果没有经过这加的三条边就是v-u,如果经过了就是这三条边的6个端点到u和v的距离和。

#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 100000+5;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
int a[6],dis[6][maxn],res[maxn];
bool vis[maxn];
vector<int> G[maxn];
int n,m;
void addEdge(int u,int v)
{
	G[u].push_back(v);
	G[v].push_back(u);
}
void SPFA(int i,int s)
{
	memset(dis[i],INF,sizeof(dis[i]));
	memset(vis,false,sizeof(vis));
	dis[i][s] = 0;
	vis[s] = true;
	queue<int> q;
	q.push(s);
	while(!q.empty())
	{
		int u = q.front();
		q.pop();
		for(int j=0; j<G[u].size(); j++)
		{
			int v = G[u][j];
			if(vis[v]) continue;
			dis[i][v] = dis[i][u]+1;
			vis[v] = true;
			q.push(v);
		}
	}
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&m);
		for(int i=1; i<=n; i++)
			G[i].clear();
		for(int i=1; i<n; i++)
			addEdge(i,i+1);
		for(int i=0; i<3; i++)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			addEdge(u,v);
			a[i*2] = u;
			a[i*2+1] = v;
		}
		for(int i=0; i<6; i++)
			SPFA(i,a[i]);
		for(int i=0; i<m; i++)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			if(u > v) swap(u,v);
			res[i] = v-u;
			for(int j=0; j<6; j++)
				res[i] = min(res[i],dis[j][u]+dis[j][v]);
		}
		int ans = 0;
		for(int i=0; i<m; i++)
			ans = (ans + 1ll*(i+1)*res[i]) % MOD;
		printf("%d\n",ans);
	}
	return 0;
}


### MATLAB `shortestpath` 函数的使用方法及示例 在 MATLAB 中,`shortestpath` 函数用于计算图中两个节点之间的最短路径。该函数可以应用于有向图或无向图,并支持加权或非加权边。以下是对该函数的详细说明以及代码示例[^3]。 #### 函数语法 `shortestpath(G,s,t)` - `G` 是一个图对象(可以通过 `graph` 或 `digraph` 创建)。 - `s` 和 `t` 分别表示起点和终点节点。 `[P,d] = shortestpath(G,s,t)` - 返回最短路径 `P` 和路径长度 `d`。 #### 示例代码 以下是一个简单的 MATLAB 代码示例,展示如何使用 `shortestpath` 函数计算图中两个节点之间的最短路径。 ```matlab % 创建一个无向图 s = [1 1 2 2 3 4 4 5]; % 起点节点 t = [2 3 3 4 5 5 6 6]; % 终点节点 weights = [10 10 1 2 1 10 10 1]; % 边权重 G = graph(s, t, weights); % 创建图对象 % 可视化图 plot(G, &#39;EdgeLabel&#39;, G.Edges.Weight); % 计算最短路径 [sPath, pathLength] = shortestpath(G, 1, 6); disp(&#39;最短路径:&#39;); disp(sPath); disp(&#39;路径长度:&#39;); disp(pathLength); ``` #### 注意事项 - 如果图中有负权重边,则需要使用 Bellman-Ford 算法,可以通过指定 `&#39;Method&#39;,&#39;BellmanFord&#39;` 参数实现[^3]。 - 对于大规模图,可以选择不同的算法以优化性能,例如 Dijkstra 或 BFS。 ```matlab % 使用 Bellman-Ford 算法 [sPath, pathLength] = shortestpath(G, 1, 6, &#39;Method&#39;, &#39;BellmanFord&#39;); ``` #### 关于 Watts-Strogatz 小世界网络模型 如果用户对社会网络分析感兴趣,MATLAB 提供了生成 Watts-Strogatz 小世界网络的功能。结合 `shortestpath` 函数,可以进一步研究小世界网络的平均路径长度等特性[^2]。 ```matlab % 生成 Watts-Strogatz 小世界网络 n = 10; % 节点数 k = 4; % 每个节点的初始邻居数 p = 0.3; % 重连概率 G = wattsstrogatz(n, k, p); % 计算所有节点对之间的最短路径 D = distances(G); avgPathLength = mean(D(:)); % 平均路径长度 disp(&#39;平均路径长度:&#39;); disp(avgPathLength); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值