输入格式
输出格式
共n-1行,第i行表示1号点到i+1号点的最短路。
样例输入
3 3
样例输出
-1
数据规模与约定
题目链接:http://lx.lanqiao.org/problem.page?gpid=T15
题目分析:再记一下SPFA的vector版的板子
#include <cstdio> #include <cstring> #include <queue> #include <vector> using namespace std; int const MAX = 200005; int const INF = 1 << 30; int n, m; struct EDGE { int u, v; int val; }e[MAX << 2]; struct NODE { int v, w; NODE(int vv, int ww) { v = vv; w = ww; } }; vector <NODE> vt[MAX]; int dis[MAX]; bool vis[MAX]; void SPFA(int v0) { memset(vis, false, sizeof(vis)); for(int i = 1; i <= n; i++) dis[i] = INF; dis[v0] = 0; queue <int> q; q.push(v0); while(!q.empty()) { int u = q.front(); q.pop(); vis[u] = false; int sz = vt[u].size(); for(int i = 0; i < sz; i++) { int v = vt[u][i].v; int w = vt[u][i].w; if(dis[v] > dis[u] + w) { dis[v] = dis[u] + w; if(!vis[v]) { q.push(v); vis[v] = true; } } } } } int main() { scanf("%d %d", &n, &m); for(int i = 0; i < m; i++) scanf("%d %d %d", &e[i].u, &e[i].v, &e[i].val); for(int i = 0; i < m; i++) vt[e[i].u].push_back(NODE(e[i].v, e[i].val)); SPFA(1); for(int i = 2; i <= n; i++) printf("%d\n", dis[i]); }
原文地址:http://blog.csdn.net/tc_to_top/article/details/44935507