Dijkstra堆优化 codeforces/problem/20/C

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 
#include 
#include 

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



vector >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 > 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 
#include 
#include 

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;
}  
vectorG[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 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;
}

你可能感兴趣的:(算法题解,最短路,dijkstra堆优化,算法)