ACM刷题之codeforce————Dijkstra?

版权声明:本文为小时的枫原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaofeng187/article/details/79201097
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);
			
		}
		
	}
}



猜你喜欢

转载自blog.csdn.net/xiaofeng187/article/details/79201097