Advertisement
Guest User

code

a guest
Mar 2nd, 2021
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. //const int mod = 10000007;
  4. const long long inf = 1e15;
  5. //const double Pi = acos(-1.0);
  6. typedef long long ll;
  7. typedef vector<int> vi;
  8. typedef vector<long long> vll;
  9. #define pb push_back
  10. #define mp make_pair
  11. #define F first
  12. #define S second
  13. #define all(x) x.begin(), x.end()
  14. #define sortall(x) sort(all(x))
  15. #define rep(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
  16. #define lc cout<<"\n"
  17. #define print2d(dp,n,m) for(int i=0;i<n;i++){for(int j=0;j<m;j++)cerr<<dp[i][j]<<" ";cerr<<"\n";}
  18.  
  19. vector<vector<pair<ll,ll>>> adj1,adj2;
  20.  
  21. struct edge{
  22. ll a,b,cost;
  23. };
  24.  
  25. int main(){
  26. ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  27. //freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
  28. int tt=1;
  29. //cin>>tt;
  30. while(tt--){
  31.  
  32. int n,m;
  33. cin>>n>>m;
  34. adj1.resize(n+1);adj2.resize(n+1);
  35. vector<edge> ed(m);
  36.  
  37. rep(i,0,m){
  38. ll x,y,w;
  39. cin>>x>>y>>w;
  40. adj1[x].pb(mp(y,w));
  41. adj2[y].pb(mp(x,w));
  42. ed[i].a=x;ed[i].b=y;ed[i].cost = w;
  43. }
  44.  
  45. vector<ll> d1(n+1,inf),d2(n+1,inf);
  46. d1[1]=0;d2[n]=0;
  47.  
  48. using pll = pair<ll,ll>;
  49.  
  50. priority_queue<pll,vector<pll>,greater<pll>> q;
  51. q.push(mp(0,1));
  52. while(!q.empty()){
  53. int v = q.top().S;
  54. int d_v = q.top().F;
  55. q.pop();
  56. if(d_v != d1[v]) continue;
  57. for (auto e : adj1[v]) {
  58. int to = e.first;
  59. int len = e.second;
  60.  
  61. if (d1[v] + len < d1[to]) {
  62. d1[to] = d1[v] + len;
  63. q.push({d1[to], to});
  64. }
  65. }
  66. }
  67.  
  68. priority_queue<pll,vector<pll>,greater<pll>> qn;
  69. qn.push(mp(0,n));
  70. while(!qn.empty()){
  71. int v = qn.top().S;
  72. int d_v = qn.top().F;
  73. qn.pop();
  74. if(d_v != d2[v]) continue;
  75. for (auto e : adj2[v]) {
  76. int to = e.first;
  77. int len = e.second;
  78.  
  79. if (d2[v] + len < d2[to]) {
  80. d2[to] = d2[v] + len;
  81. qn.push({d2[to], to});
  82. }
  83. }
  84. }
  85.  
  86.  
  87. ll ans = inf;
  88. for(auto e : ed){
  89. ans = min(ans,d1[e.a]+d2[e.b]+e.cost/2);
  90. }
  91. cout<<ans;
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement