fork(1) download
  1. // iostream is too mainstream
  2. #include <cstdio>
  3. // bitch please
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <cstdlib>
  7. #include <vector>
  8. #include <set>
  9. #include <map>
  10. #include <queue>
  11. #include <stack>
  12. #include <list>
  13. #include <cmath>
  14. #include <iomanip>
  15. #define dibs reserve
  16. #define OVER9000 123456789
  17. #define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
  18. #define tisic 47
  19. #define soclose 1e-8
  20. #define chocolate win
  21. // so much chocolate
  22. #define patkan 9
  23. #define ff first
  24. #define ss second
  25. #define abs(x) ((x < 0)?-(x):x)
  26. #define uint unsigned int
  27. #define dbl long double
  28. using namespace std;
  29. // mylittledoge
  30.  
  31. int main() {
  32. cin.sync_with_stdio(0);
  33. cin.tie(0);
  34. int N,M;
  35. cin >> N >> M;
  36. vector< vector<int> > G(N,vector<int>(N,OVER9000));
  37. for(int i =0; i < M; i++) {
  38. int a,b,c;
  39. cin >> a >> b >> c;
  40. G[a][b] =G[b][a] =min(G[a][b],c);}
  41. vector< vector<int> > D =G;
  42. for(int i =0; i < N; i++) D[i][i] =0;
  43. for(int k =0; k < N; k++)
  44. for(int i =0; i < N; i++) for(int j =0; j < N; j++)
  45. D[i][j] =min(D[i][j],D[i][k]+D[k][j]);
  46.  
  47. vector< vector<int> > F(2*N,vector<int>(2*N,0));
  48. for(int i =1; i < N-1; i++) F[2*i][2*i+1] =1;
  49. F[0][1] =F[2*N-2][2*N-1] =N+1;
  50. for(int i =0; i < N; i++) for(int j =0; j < N; j++) if(G[i][j] < OVER9000)
  51. if(D[0][i]+D[j][N-1]+G[i][j] == D[0][N-1])
  52. F[2*i+1][2*j] =N+1;
  53.  
  54. int f =0;
  55. while(true) {
  56. vector<int> ako(2*N,-1);
  57. queue<int> q;
  58. q.push(0);
  59. ako[0] =0;
  60. while(!q.empty()) {
  61. for(int i =0; i < 2*N; i++) if(F[q.front()][i] > 0 && ako[i] == -1) {
  62. ako[i] =q.front();
  63. q.push(i);}
  64. q.pop();}
  65. if(ako[2*N-1] == -1) break;
  66. f++;
  67. int akt =2*N-1;
  68. while(akt > 0) {
  69. F[akt][ako[akt]]++;
  70. F[ako[akt]][akt]--;
  71. akt =ako[akt];}
  72. }
  73.  
  74. if(f < N) cout << f << "\n";
  75. else cout << "IMPOSSIBLE\n";
  76. return 0;}
  77.  
  78. // look at my code
  79. // my code is amazing
  80.  
Runtime error #stdin #stdout 0s 3436KB
stdin
Standard input is empty
stdout
Standard output is empty