• 【无标题】斜率优化dp


    1.与dp[j]无关

    dp[i]=k(j)*f(i)+b[j];

    E. Long Way Home

    time limit per test

    3 seconds

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    Stanley lives in a country that consists of nn cities (he lives in city 11). There are bidirectional roads between some of the cities, and you know how long it takes to ride through each of them. Additionally, there is a flight between each pair of cities, the flight between cities uu and vv takes (u−v)2(u−v)2 time.

    Stanley is quite afraid of flying because of watching "Sully: Miracle on the Hudson" recently, so he can take at most kk flights. Stanley wants to know the minimum time of a journey to each of the nn cities from the city 11.

    Input

    In the first line of input there are three integers nn, mm, and kk (2≤n≤1052≤n≤105, 1≤m≤1051≤m≤105, 1≤k≤201≤k≤20) — the number of cities, the number of roads, and the maximal number of flights Stanley can take.

    The following mm lines describe the roads. Each contains three integers uu, vv, ww (1≤u,v≤n1≤u,v≤n, u≠vu≠v, 1≤w≤1091≤w≤109) — the cities the road connects and the time it takes to ride through. Note that some pairs of cities may be connected by more than one road.

    Output

    Print nn integers, ii-th of which is

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. #define int long long
    7. const int inf = 1e18;
    8. const int inf1 = 1e15;
    9. struct CHT {
    10. struct line {
    11. int k, b;
    12. line() {}
    13. line(int k, int b): k(k), b(b) {}
    14. double intersect(line l) {
    15. double db = l.b - b;
    16. double dk = k - l.k;
    17. return db / dk;
    18. }
    19. long long operator () (long long x) {
    20. return k * x + b;
    21. }
    22. };
    23. vector<double> x;
    24. vector ll;
    25. void init(line l) {
    26. x.push_back(-inf);
    27. ll.push_back(l);
    28. }
    29. void addLine(line l) {
    30. while (ll.size() >= 2 && l.intersect(ll[ll.size() - 2]) <= x.back()) {
    31. x.pop_back();
    32. ll.pop_back();
    33. }
    34. if (!ll.empty()) {
    35. x.push_back(l.intersect(ll.back()));
    36. }
    37. ll.push_back(l);
    38. }
    39. long long query(long long qx) {
    40. int id = upper_bound(x.begin(), x.end(), qx) - x.begin();
    41. --id;
    42. return ll[id](qx);
    43. }
    44. };
    45. void dijkstra(vectorint, int>>> &g, vector<int> &dist) {
    46. const int n = g.size();
    47. vector<bool> used(n, false);
    48. priority_queueint, int>> q;
    49. for (int i = 0; i < n; ++i) {
    50. q.push({ -dist[i], i });
    51. }
    52. while (!q.empty()) {
    53. int v = q.top().second;
    54. q.pop();
    55. if (used[v]) {
    56. continue;
    57. }
    58. used[v] = true;
    59. for (auto [u, w] : g[v]) {
    60. if (dist[u] > dist[v] + w) {
    61. dist[u] = dist[v] + w;
    62. q.push({ -dist[u], u });
    63. }
    64. }
    65. }
    66. }
    67. int32_t main() {
    68. ios_base::sync_with_stdio(false);
    69. cin.tie(0);
    70. cout.tie(0);
    71. int n, m, k;
    72. cin >> n >> m >> k;
    73. vectorint, int>>> g(n);
    74. for (int i = 0; i < m; ++i) {
    75. int u, v, w;
    76. cin >> u >> v >> w;
    77. --u; --v;
    78. g[u].push_back({ v, w });
    79. g[v].push_back({ u, w });
    80. }
    81. vector<int> dist(n, inf1);
    82. dist[0] = 0;
    83. dijkstra(g, dist);
    84. for (int i = 0; i < k; ++i) {
    85. CHT cht;
    86. cht.init(CHT::line(0, 0));
    87. for (int i = 1; i < n; ++i) {
    88. cht.addLine(CHT::line(-i * 2, dist[i] + i * i));
    89. }
    90. for (int i = 0; i < n; ++i) {
    91. dist[i] = cht.query(i) + i * i;
    92. }
    93. dijkstra(g, dist);
    94. }
    95. for (int i : dist) {
    96. cout << i << ' ';
    97. }
    98. }

    equal to the minimum time of traveling to city ii.

  • 相关阅读:
    【AGC】云存储如何上传文件?是否可以自行开通?云存储的相关问题,来这里看看!
    【嵌入式模块】再探ESP8266,保姆级教程
    Windows 11 电脑黑屏 + Office软件打不开?
    【巨大的错误】【歌词中找单词】【字符串斐波那契】
    智能合约升级原理01---起源
    IPC机制
    用户的权限
    HashMap与HashTable、HashSet的区别
    .Net DI(Dependency Injection)依赖注入机制
    【PAT甲级】1146 Topological Order
  • 原文地址:https://blog.csdn.net/qq_52004482/article/details/126611730