ACM刷题之codeforce————Dijkstra?

本文介绍了一道关于寻找加权无向图中最短路径的编程题,使用SPFA算法时遇到的时间限制问题及解决方案。通过对算法进行剪枝优化,有效避免了不必要的计算,显著提升了运行效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Dijkstra?
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output

You are given a weighted undirected graph. The vertices are enumerated from 1 to n. Your task is to find the shortest path between the vertex 1 and the vertex n.

Input

The first line contains two integers n and m (2 ≤ n ≤ 105, 0 ≤ m ≤ 105), where n is the number of vertices and m is the number of edges. Following m lines contain one edge each in form aibi and wi (1 ≤ ai, bi ≤ n, 1 ≤ wi ≤ 106), where ai, bi are edge endpoints and wi is the length of the edge.

It is possible that the graph has loops and multiple edges between pair of vertices.

Output

Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them.

Examples
input
5 6
1 2 2
2 5 5
2 3 4
1 4 1
4 3 3
3 5 1
output
1 4 3 5 
input
5 6
1 2 2
2 5 5
2 3 4
1 4 1
4 3 3
3 5 1
output
1 4 3 5 

一道最短路的题目,一开始我用spfa的算法来写,但是写完后提交,发现超时了。
以为是初始化的问题,所以又稍微改了下去提交,发现还是超时的,当时还以为这题是不是卡spfa的,于是网上查了下相关的资料,发现这题原来要剪枝。

剪枝想法:因为我之前的spfa是针对于所有点的最短路,而这题让我们求的是针对于终点的最短路,所以很边界的点其实可以省略。
if(dis[now]>dis[m]) continue;  这一行就可以把我从超时变成78ms..
m是终点。如果当前距离已经比终点要大,那么没必要走下去了,所以直接剪掉。

通过这题,我对于最短路的剪枝有了更多了解。
下面是ac代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<iostream>
using namespace std;
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int N=2e5+7;
const int maxn=100002;

int n,m;

long long dis[maxn];
int inq[maxn];
int vi[maxn];
int la[maxn];

void init()
{
	CLR(inq,0);
	CLR(vi,-1);
	CLR(dis, INF);
}



int main()
{
	//freopen("f:/input.txt", "r", stdin);
	int x,y;
	long long z;
	while(scanf("%d%d",&m,&n)!=EOF)
	{
		init();
		vector<pair<int,long long> >E[maxn];
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d%lld",&x,&y,&z);
			E[x].push_back(make_pair(y,z));
			E[y].push_back(make_pair(x,z));
		}
		
		queue<int> Q;
		Q.push(1);
		dis[1]=0;
		inq[1]=1;
		while(!Q.empty())
		{
			int now = Q.front();
			Q.pop();
			inq[now]=0;
			if(dis[now]>dis[m]) continue; 
			for(int i=0;i<E[now].size();i++)
			{
				int v = E[now][i].first;
				if(dis[v]>dis[now]+E[now][i].second){
					dis[v]=dis[now]+E[now][i].second;
					vi[v] = now;
					if(inq[v]==1) continue;
					inq[v]=1;
					Q.push(v);
					
				}
			}
		}
		if(dis[m]==INF || vi[m] == -1) {
			printf("-1\n");
		} else {
			la[0]=m;
			int i =m,cnt=1;
			while(vi[i]!=-1) {
				la[cnt]=vi[i];
				i=vi[i];
				++cnt;
			}
			for(i=cnt-1;i>0;i--) printf("%d ",la[i]);
			printf("%d\n",m);
			
		}
		
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值