stl真是好,,偷懒少写邻接表,,
两个STL应用使代码简短了很多。然而还是那句话,天上不会掉馅饼,程序的效率还是有所下降的。然而,效率不是全部,人们宁可牺牲三倍效率用Java而不用C语言就是最好的例子(from_Charles E Leiserson_),具体取舍要看情况。 ——摘自luogu dalao
1 #include <cstdio> 2 #include <algorithm> 3 #include <iostream> 4 #include <vector> 5 #include <queue> 6 using namespace std; 7 const int inf = 0x7fffffff; 8 const int maxn = 500000 + 1; 9 struct EDGE{ 10 int v, w; 11 }pos; 12 vector<EDGE> e[maxn]; 13 queue<int> q; 14 int n, m, dis[maxn], s; 15 bool vis[maxn]; 16 int SPFA() 17 { 18 while(!q.empty()) 19 { 20 int now = q.front(); 21 q.pop(); 22 vis[now] = 0; 23 for(int i = 0; i < e[now].size(); i++) 24 { 25 if(dis[e[now][i].v] > dis[now] + e[now][i].w) 26 { 27 dis[e[now][i].v] = dis[now] + e[now][i].w; 28 if(vis[e[now][i].v] == 0) 29 { 30 vis[e[now][i].v] = 1; 31 q.push(e[now][i].v); 32 } 33 } 34 } 35 } 36 } 37 int main() 38 { 39 scanf("%d%d%d",&n,&m,&s); 40 for(int i = 1; i <= n; i++) 41 dis[i] = inf; 42 for(int i = 1; i <= m; i++) 43 { 44 int u; 45 scanf("%d%d%d",&u,&pos.v,&pos.w); 46 e[u].push_back(pos); 47 } 48 q.push(s); 49 dis[s] = 0; 50 vis[s] = 1; 51 SPFA(); 52 for(int i = 1; i <= n; i++) 53 printf("%d ",dis[i]); 54 return 0; 55 }