Dijkstra堆优化 codeforces/problem/20/C

博客围绕加权无向图求最短路问题展开。给定N个点、M条边的无向图,需找出从顶点1到顶点N的最短路。采用堆优化Dijkstra算法求解,过程中遇到结构体写法超时问题,通过用变量记录避免每次计算数组大小解决,作者表示部分内容未完全弄懂,后续待补。

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

http://codeforces.com/contest/20/problem/C

C. Dijkstra?
time limit per test1 second
memory limit per test64 megabytes
inputstandard input
outputstandard 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 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.

Examples
inputCopy
5 6
1 2 2
2 5 5
2 3 4
1 4 1
4 3 3
3 5 1
outputCopy
1 4 3 5
inputCopy
5 6
1 2 2
2 5 5
2 3 4
1 4 1
4 3 3
3 5 1
outputCopy
1 4 3 5

题意

N个点,M条边。无向图
从A到B的距离是Wi
输出从1到N的最短路

思路

数据范围
(2 ≤ n ≤ 1e5, 0 ≤ m ≤ 1e5)
(1 ≤ ai, bi ≤ n, 1 ≤ wi ≤ 1e6)
用堆优化 Dijkstra,在用结构体写的时候T了,常数没有处理好,for循环如果每次都算一次G[now_node].size()就会超时,用变量记一下答案就过了。刚开始还以为只有pair才能过
int sz = G[now_node].size();
for(int i = 0 ; i < sz ; i++)

wl给我讲了一遍,但还是没有完全弄懂,以后这类题还要补。

两个板子

#include <cstdio>
#include <queue>
#include <climits>

using namespace std;
 
const int MaxN = 1e5 + 5;



vector<pair<int,long long> >G[MaxN];



long long dis[MaxN];
int n , m , x;
int pos[MaxN];
void dfs(int x ){
if(x == 1){
	printf("1 ");
	return ;
}
dfs(pos[x]);
printf("%d ",x);
}
void dij(){
priority_queue< pair<int,int> > que;
for(int i = 1; i <= n ; i++)dis[i] = LLONG_MAX;
dis[1] = 0LL;
que.push(make_pair(0LL,1));
while(!que.empty()){
	int now = que.top().second;
//		printf("now = %d\n",now);
	que.pop();
	if(now == n)break;
	int sz = G[now].size();
	for(int i = 0 ; i < sz ; i++){
		long long w = G[now][i].second;
		int to = G[now][i].first;
		if(dis[to] > dis[now] + w){
			dis[to] = dis[now] + w;
			pos[to] = now;
			que.push(make_pair(-dis[to],to));
		}
	}
}
}
int main(){
scanf("%d %d",&n,&m);
while(m--){
	int u , v ;
	long long w;
	scanf("%d %d %I64d",&u,&v,&w);
	G[u].push_back(make_pair(v,w));
	G[v].push_back(make_pair(u,w));
}
dij();
if(dis[n] == LLONG_MAX){
	puts("-1");
}
else{
	dfs(n);
}
return 0;
}



#include <cstdio>
#include <queue>
#include <climits>

using namespace std;

const int MaxN = 1e5 + 5;
const int inf = 0x3f3f3f3f;

struct edge{
int to , w;

};
bool operator < (edge a ,edge b) {
return a.w > b.w;
}  
vector<edge>G[MaxN];



long long dis[MaxN];

int n , m , x;
int pos[MaxN];
void dfs(int x ){
if(x == 1){
	printf("1 ");
	return ;
}
dfs(pos[x]);
printf("%d ",x);
}
void dij(){
for(int i = 1; i <= n ; i++)dis[i] = LLONG_MAX;
dis[1] = 0LL;
priority_queue<edge> que;
que.push((edge){1,0LL});
while(!que.empty()){
	edge now_edge = que.top();
	que.pop();
	int now_node = now_edge.to;
	if(now_node == n)break;
	int sz = G[now_node].size();
	for(int i = 0 ; i < sz ; i++){
		int to = G[now_node][i].to;
		long long w = G[now_node][i].w;
		if(dis[to] > dis[now_node] + w){
			dis[to] = dis[now_node] + w;
			pos[to] = now_node;
			que.push((edge){to,dis[to]});
		}
	}
}
}
int main(){
scanf("%d %d",&n,&m);
while(m--){
	int u , v;
	long long w;
	scanf("%d %d %I64d",&u,&v,&w);
	G[u].push_back((edge){v,w});
	G[v].push_back((edge){u,w});
}
dij();
if(dis[n] == LLONG_MAX){
	puts("-1");
}
else{
	dfs(n);
}
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值