codeforces 20C Dijkstra?
来源:互联网 发布:mac rar 多余文件 编辑:程序博客网 时间:2024/06/05 07:15
Description
You are given a weighted undirected graph. The vertices are enumerated from 1 ton. Your task is to find the shortest path between the vertex1 and the vertex n.
Input
The first line contains two integers n andm (2 ≤ n ≤ 105, 0 ≤ m ≤ 105), wheren is the number of vertices and m is the number of edges. Following m lines contain one edge each in formai,bi andwi (1 ≤ ai, bi ≤ n, 1 ≤ wi ≤ 106), where ai, bi are edge endpoints andwi 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
5 61 2 22 5 52 3 41 4 14 3 33 5 1
1 4 3 5
5 61 2 22 5 52 3 41 4 14 3 33 5 1
1 4 3 5
解法:dijkstra或者spfa,用pre数组记录到达每个点最短距离的前驱结点。
路径的长度范围是 1 ≤ wi ≤ 106,从1到n的最短路径长度可能超出int范围。
#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;typedef long long LL;typedef pair<LL,int> pii;const int maxn = 100010;const int maxm = 200010;int v[maxm],next[maxm],w[maxm];int first[maxn],pre[maxn],res[maxn],e;LL d[maxn];void init(){ e = 0; memset(first,-1,sizeof(first));}void add_edge(int a,int b,int c){ v[e] = b;next[e] = first[a];w[e] = c;first[a] = e++;}void dijkstra(int src){ priority_queue <pii,vector<pii>,greater<pii> > q; memset(d,-1,sizeof(d)); d[src] = 0; q.push(make_pair(0,src)); while(!q.empty()){ while(!q.empty() && q.top().first > d[q.top().second]) q.pop(); if(q.empty()) break; int u = q.top().second; q.pop(); for(int i = first[u];i != -1;i = next[i]){ if(d[v[i]] > d[u]+w[i] || d[v[i]] == -1){ d[v[i]] = d[u]+w[i]; q.push(make_pair(d[v[i]],v[i])); pre[v[i]] = u; } } }}int main(){ init(); int n,m,a,b,c; scanf("%d%d",&n,&m); for(int i = 0;i < m;i++){ scanf("%d%d%d",&a,&b,&c); add_edge(a,b,c); add_edge(b,a,c); } dijkstra(1); int now = n,cnt = 0; if(d[n] == -1){ printf("-1"); return 0; } while(now != 1){ res[cnt++] = now; now = pre[now]; } res[cnt++] = 1; for(int i = cnt-1;i >= 0;i--) printf("%d ",res[i]); return 0;}
这题不错,正好最近在看最短路的东西,就拿它练手了~
- Codeforces 20C Dijkstra?
- codeforces 20C Dijkstra?
- CodeForces 20C Dijkstra? (最短路)
- Codeforces Alpha Round #20 (Codeforces format) C. Dijkstra?(裸的dijkstra)
- Codeforces Alpha Round #20 C. Dijkstra?(优先队列优化)
- codeforces 20 c 堆优化dijkstra + 路径打印
- CF 20C Dijkstra?
- CodeForces 20C
- dijkstra算法 c语言
- Dijkstra算法函数(c++)
- C++Dijkstra算法实现
- Yaroslav and Time - CodeForces 301B Dijkstra
- Codeforces 144D. Missile Silos【dijkstra】
- codeforces 507E (dijkstra+优先队列)
- Codeforces 601A Dijkstra最短路
- CodeForces 601A (dijkstra算法)
- Dijkstra改进算法C语言
- c语言 dijkstra算法 交通图
- http://poj.org/problem?id=1035
- POJ 3468 A Simple Problem with Integers 树状数组 区间修改 区间查询
- 在mac上搭建svn服务器
- C语言continue 和break的用法
- Mysql优化之问题定位
- codeforces 20C Dijkstra?
- 数论寻找等差素数UVA 10650
- samba 服务的搭建
- c# 窗体双缓冲机制
- 暑期实训第三课(数据采集终端的制作)
- thttpd 2.25b源码分析
- 1036. Boys vs Girls (25)
- 1273 Drainage Ditches 模板题
- 常用颜色的RGB值