标签:style blog color os io for div size
这个题目意思我给弄错了。真桑心没好好听TK的指导,只要在松弛操作里头记录前驱节点就可以了,还要注意long long,其实我有很多时候测试错了可以猜到要用long long才可以,但是不知道缘故,就单单怎么算,不是很了解。
#include <cstdio> #include <iostream> #include <cstring> #include <queue> #include <algorithm> #include <algorithm> #define LL long long using namespace std; const int maxn = 200010; const int maxm = 200100; typedef pair<LL,int> pii; int v[maxm],next[maxm],w[maxm]; int first[maxn]; LL d[maxn]; int e; int temp[maxn]; int re[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 dij(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()){ int u = q.top().second; q.pop(); for(int i = first[u];i != -1;i = next[i]){ if(d[v[i]] == -1 || d[v[i]] > d[u]+w[i]){ temp[v[i]] = u; d[v[i]] = d[u]+w[i]; q.push(make_pair(d[v[i]],v[i])); } } } } int main(){ int m,n;cin >> m >> n; int a ,b,c; init(); for(int i = 0;i < n;i++){ cin >> a >> b >> c; add_edge(a,b,c); add_edge(b,a,c); } dij(1); int p = 0,now = m; if(d[m] == -1) { cout << "-1" << endl; return 0; } re[p++] = m; while(now != 1) { now = temp[now]; re[p++] = now; } for(int i = p-1;i >0;i--) { cout << re[i] << " "; } cout << re[0] << endl; return 0; }
标签:style blog color os io for div size
原文地址:http://www.cnblogs.com/hhhhhhhhhhhhhhhhhhhhhhhhhhh/p/3888301.html