Two Paths Posted on 2019-07-12 | In acm , 做题记录 , 2017杭电多校赛 题目链接Code12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364#include <bits/stdc++.h>#define ka1 getchar()#define iis std::ios::sync_with_stdio(false)using namespace std;typedef long long LL;const int N = 100005;const int INF = 0x3f3f3f3f;const LL mod = 10000;const double eps = 1e-8;struct lp{ int to; LL w; lp(int a,LL b){to=a;w=b;} bool operator <(const lp &a)const { if(w!=a.w) return w>a.w; return to<a.to; }};vector<lp>mp[N];LL dis1[N],dis2[N];int n,m;void init(){ for(int i=0;i<=n;++i)mp[i].clear();}void dij(int s){ memset(dis1,0x3f,sizeof(dis1)); memset(dis2,0x3f,sizeof(dis2)); dis1[s]=0; priority_queue<lp>Q; Q.push(lp(s,0)); while(!Q.empty() ){ lp x=Q.top();Q.pop(); int u=x.to; if(dis2[u]<x.w)continue; for(int i=0;i<mp[u].size();++i){ lp y=mp[u][i]; if(dis2[y.to]>(x.w+y.w)){ dis2[y.to]=x.w+y.w; Q.push(lp(y.to,dis2[y.to])); } if(dis2[y.to]<dis1[y.to]){ swap(dis2[y.to],dis1[y.to]); } } } printf("%lld\n",dis2[n]);}int main(){ int t; scanf("%d",&t); while(t--){ int a,b,c; scanf("%d%d",&n,&m); init(); for(int i=0;i<m;++i){ scanf("%d%d%d",&a,&b,&c); mp[a].push_back(lp(b,c)); mp[b].push_back(lp(a,c)); } dij(1); } return 0;}