Advertisement
Nickpips

Untitled

Jun 17th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1.  
  2. Probgate View Solution
  3. npip99/logout
  4. Contests
  5. Problems
  6. Settings
  7. demiguo1's solution to Trolley Problem in C++1y
  8.  
  9. #include <cstdio>
  10. #include <cstring>
  11. #include <cstdlib>
  12. #include <algorithm>
  13. #include <vector>
  14. #include <utility>
  15. using namespace std;
  16.  
  17. const int maxn=505;
  18. const int maxm=2050;
  19. const int maxk=13;
  20. typedef long long ll;
  21. const ll inf = 1ll<<60;
  22. int n,k,m;
  23. ll dp[2][maxn][maxk];
  24. int q[maxn*maxk*20][2];
  25. vector<pair<int, int> > edge[maxn];
  26. int go[maxn];
  27. ll tmp[maxk];
  28. bool in[maxn][maxk];
  29. void dw(ll &x, ll y) { if (y<x) x=y; }
  30. void spfa(int t, int st) {
  31. for (int i=0;i<=n;i++)
  32. for (int j=0;j<=k;j++)
  33. dp[t][i][j]=inf;
  34. for (int j=0;j<=k;j++) dp[t][st][j]=0;
  35. int hd=0,tl=0;
  36. memset(in,0,sizeof in);
  37. for (int j=0;j<=k;j++) {
  38. in[st][j]=true;
  39. ++tl;
  40. q[tl][0]=st;
  41. q[tl][1]=j;
  42. }
  43. while (hd!=tl) {
  44. int x=q[++hd][0];
  45. int y=q[hd][1];
  46. for (int i = 0; i < (int)edge[x].size(); i ++) {
  47. int dy = go[x]!=i;
  48. if (dp[t][x][y]+edge[x][i].second<dp[t][edge[x][i].first][y+dy]) {
  49. dp[t][edge[x][i].first][y+dy]=dp[t][x][y]+edge[x][i].second;
  50. int nx=edge[x][i].first;
  51. int ny=y+dy;
  52. if (!in[nx][ny]) in[nx][ny]=true,q[++tl][0]=nx,q[tl][1]=ny;
  53. }
  54. }
  55. in[x][y]=false;
  56. }
  57. }
  58. int main() {
  59. scanf("%d%d%d",&n,&m,&k);
  60. for (int i=1;i<=m;i++) {
  61. int a,b,c;
  62. scanf("%d%d%d",&a,&b,&c);
  63. edge[a].push_back(make_pair(b,c));
  64. if (i<=n) go[a]=edge[a].size()-1;
  65. }
  66. ll ans = inf;
  67. spfa(0, 1);
  68. for (int i=1;i<=n;i++) {
  69. spfa(1, i);
  70. for (int j=0;j<=k;j++)
  71. tmp[j]=inf;
  72. for (int j=1;j<=n;j++)
  73. for (int t=0;t<(int)edge[j].size();t++)
  74. if (edge[j][t].first==i) {
  75. int dk = t==go[j];
  76. dk=1-dk;
  77. for (int z=0;z<=k;z++)
  78. dw(tmp[z+dk], dp[1][j][z]+edge[j][t].second);
  79. }
  80. for (int j=0;j<=k;j++)
  81. dw(ans, tmp[j]+dp[0][i][k-j]);
  82. }
  83. printf("%lld\n",ans);
  84.  
  85. return 0;
  86. }
  87. Jun 18, 2016, 12:15 am EDT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement