CF 20C Dijkstra?

这是一个关于图论的问题,要求在给定的加权无向图中找到从顶点1到顶点n的最短路径。输入包含图的节点数n和边数m,以及每条边的起点、终点和权重。输出是-1表示没有路径,否则输出最短路径。题目描述中提到可以使用堆优化的Dijkstra算法,并提供了第一次使用C++链表存图的经历,这可能成为之后的模板。

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

Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u

[]  [Go Back]  [Status]  

Description

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 ai, bi 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.

Sample Input

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 
解析

最短路,堆优Dijkstra。这个题本身没什么,就是每次更新最短路的时候维护一个fa(fa[i]表示i的父亲)。第一次写堆优Dijkstra,第一次用C++链表存图。以后就是我的模板了

#include<stack>
#include<cstdio>
#include<cstring>
#include<utility>
#include<queue>

using namespace std;

#define INF 0x3f3f3f3f3f3f3f3fLL
typedef long long LL;

struct tnode
{
	tnode *next;
	int from,to;
	LL len;
};
typedef tnode* pnode; pnode Chain[100100];

typedef pair<LL,int> pii;
priority_queue<pii,vector<pii>,greater<pii> > heap;
bool vis[100100];

int N,M,pre[100100];
LL dist[100100];
void insert(int from,int to,LL len)
{
	pnode p=new tnode;
	p->next=Chain[from];
	p->from=from;
	p->to=to;
	p->len=len;
	Chain[from]=p;
}

void readdata()
{
	scanf("%d%d",&N,&M);
	for(int i=1;i<=N;i++) Chain[i]=NULL;
	for(int i=1;i<=M;i++)
	{
		int a,b;
		LL c;
		scanf("%d%d%I64d",&a,&b,&c);
		insert(a,b,c); insert(b,a,c);
	}
}

void dijkstra()
{
	for(int i=2;i<=N;i++) dist[i]=INF; dist[1]=0;
	memset(vis,0,sizeof(vis));

	heap.push(make_pair(dist[1],1));
	while(!heap.empty())
	{
		int v=heap.top().second; heap.pop();
		if(vis[v]) continue; vis[v]=1;
		for(pnode p=Chain[v];p;p=p->next)
		{
			int u=p->to,len=p->len;
			if(dist[u]>dist[v]+len)
			{
				pre[u]=v;
				dist[u]=dist[v]+len;
				heap.push(make_pair(dist[u],u));
			}
		}
	}

	//for(int i=1;i<=N;i++) printf("%I64d ",dist[i]); printf("%I64d\n",~0LL>>2);
	stack<int> Stack;
	if(dist[N]==INF) printf("-1");
	else
	{
		Stack.push(N);
		while(Stack.top()!=1) Stack.push(pre[Stack.top()]);
		while(!Stack.empty()) {printf("%d ",Stack.top());Stack.pop();}
	}
}

int main()
{
	freopen("CF20C.in","r",stdin);

	readdata();
	dijkstra();

	while(1);
	return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值