洛谷 P3371 【模板】单源最短路径
2017-09-29 10:42
232 查看
洛谷 P3371 【模板】单源最短路径
题目描述
如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度。输入输出格式
输入格式:
第一行包含三个整数N、M、S,分别表示点的个数、有向边的个数、出发点的编号。接下来M行每行包含三个整数Fi、Gi、Wi,分别表示第i条有向边的出发点、目标点和长度。
输出格式:
一行,包含N个用空格分隔的整数,其中第i个整数表示从点S出发到点i的最短路径长度(若S=i则最短路径长度为0,若从点S无法到达点i,则最短路径长度为2147483647)输入输出样例
输入样例:
4 6 11 2 2
2 3 2
2 4 1
1 3 5
3 4 3
1 4 4
输出样例:
0 2 4 3解题分析:
Dijkstra用链接矩阵表示图会超时,因此可采用链接表表示。另外,从一个顶点到另一个顶点可能有多条边,只要取其中较小的边即可。
#include #include #include #include #include #include #include #include using namespace std; #define N 10005 #define INF 0x7fffffff int n, m, s; int d , visited = {0}; struct node{ int to; int v; void set(int to1, int v1){ to = to1; v = v1; } }; vector g ; // 链接表表示图 int get_i(){ int ans = 0; char ch = getchar(); while(ch<'0' || ch>'9') ch = getchar(); while(ch>='0' && ch<='9'){ ans = ans * 10 + ch - '0'; ch = getchar(); } return ans; } int main(){ ios::sync_with_stdio(false); int i, j, from, to, v, k, min1, flag; node n1; n = get_i(), m = get_i(), s = get_i(); for(i=0; i d[i]){ min1 = d[i]; k = i; } } if(k == -1) break; visited[k] = 1; for(i=0; i d[k] + g[k][i].v) d[g[k][i].to] = d[k] + g[k][i].v; } } for(i=1; i<=n; i++){ cout<
相关文章推荐
- 洛谷 P3371 【模板】单源最短路径
- 洛谷 P3371 【模板】单源最短路径(Dijkstra + 堆优化)
- P3371 单源最短路径【模板】 洛谷
- 洛谷 P3371 【模板】单源最短路径
- 洛谷 P3371 【模板】单源最短路径
- [洛谷]P3371 单源最短路径模板 SPFA
- 洛谷 P3371 【模板】单源最短路径
- [洛谷]P3371 单源最短路径模板-bell
- [洛谷]P3371 单源最短路径模板 Dijkstra
- P3371 【模板】单源最短路径
- 洛谷 P3371 [模版] 单源最短路径
- P3371 【模板】单源最短路径 SPFA优化 dijkstra堆优化
- P3371 【模板】单源最短路径
- P3371 【模板】单源最短路径
- [洛谷3371]【模板】单源最短路径
- P3371 【模板】单源最短路径
- 三解<单源最短路径模板题>
- [模板] 单源最短路径
- [模板] Dijkstra单源最短路径
- [模板]-单源最短路径