fork download
  1. //Lib
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cstdlib>
  5. #include<cmath>
  6. #include<ctime>
  7.  
  8. #include<iostream>
  9. #include<algorithm>
  10. #include<vector>
  11. #include<string>
  12. #include<queue>
  13. #include<set>
  14. #include<map>
  15. using namespace std;
  16. //Macro
  17. #define rep(i,a,b) for(int i=a,tt=b;i<=tt;++i)
  18. #define drep(i,a,b) for(int i=a,tt=b;i>=tt;--i)
  19. #define erep(i,e,x) for(int i=x;i;i=e[i].next)
  20. #define irep(i,x) for(__typeof(x.begin()) i=x.begin();i!=x.end();i++)
  21. #define read() (strtol(ipos,&ipos,10))
  22. #define sqr(x) ((x)*(x))
  23. #define pb push_back
  24. #define PS system("pause");
  25. typedef long long ll;
  26. typedef pair<int,int> pii;
  27. const int oo=~0U>>1;
  28. const double inf=1e100;
  29. const double eps=1e-6;
  30. string name="exp", in=".in", out=".out";
  31. //Var
  32. struct E
  33. {
  34. int next,node,v;
  35. }e[400008];
  36. queue<int> q;
  37. pii p[10008][2],lnk[200008];
  38. int n,m,tot,ans=oo,h[10008];
  39. bool vis[10008];
  40. void add(int a,int b,int c){e[++tot].next=h[a];e[tot].node=b;e[tot].v=c;h[a]=tot;}
  41. void Init()
  42. {
  43. scanf("%d%d",&n,&m);
  44. rep(i,1,n)p[i][1].second=p[i][0].second=oo>>1;
  45. int s,t,w1,w2;
  46. rep(i,1,m)
  47. {
  48. scanf("%d%d%d%d",&s,&t,&w1,&w2);
  49. if(t==1)swap(s,t),swap(w1,w2);
  50. add(s,t,w1);
  51. if(s==1)
  52. {
  53. p[t][0].first=i;
  54. p[t][0].second=w1;
  55. lnk[i].first=t;lnk[i].second=w2;
  56. q.push(t);vis[t]=true;
  57. }
  58. else add(t,s,w2);
  59. }
  60. }
  61. void Push(int v){if(!vis[v])vis[v]=true,q.push(v);}
  62. void Update(int i,int d,int v)
  63. {
  64. if(p[v][0].second>d)
  65. {
  66. if(p[v][0].first!=i)
  67. p[v][1]=p[v][0];
  68. p[v][0].first=i;
  69. p[v][0].second=d;
  70. Push(v);
  71. }
  72. else if(p[v][1].second>d&&p[v][0].first!=i)
  73. {
  74. p[v][1].first=i;
  75. p[v][1].second=d;
  76. Push(v);
  77. }
  78. }
  79. void SPFA()
  80. {
  81. int u,v;
  82. while(!q.empty())
  83. {
  84. u=q.front();q.pop();vis[u]=false;
  85. erep(i,e,h[u])
  86. Update(p[u][0].first,p[u][0].second+e[i].v,e[i].node),
  87. Update(p[u][1].first,p[u][1].second+e[i].v,e[i].node);
  88. }
  89. }
  90. void Work()
  91. {
  92. SPFA();
  93. rep(i,1,m)
  94. {
  95. if(lnk[i].first)
  96. ans=min(ans,p[lnk[i].first][p[lnk[i].first][0].first==i].second+lnk[i].second);
  97. }
  98. cout<<ans<<endl;
  99. }
  100. int main()
  101. {
  102. // freopen((name+in).c_str(),"r",stdin);
  103. // freopen((name+out).c_str(),"w",stdout);
  104. Init();
  105. Work();
  106. return 0;
  107. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty