首页 > 代码库 > 七夕专场-D题
七夕专场-D题
一个map的超水题。
#include <cstdio>#include <iostream>#include <cstring>#include <queue>#include <algorithm>#include <algorithm>#define LL long longusing 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;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。