{"id":761,"date":"2020-08-06T12:36:16","date_gmt":"2020-08-06T04:36:16","guid":{"rendered":"http:\/\/www.zyhcoding.club\/?p=761"},"modified":"2020-10-13T13:53:49","modified_gmt":"2020-10-13T05:53:49","slug":"2020-8","status":"publish","type":"post","link":"http:\/\/www.zyhcoding.club\/index.php\/2020\/08\/06\/2020-8\/","title":{"rendered":"2020-8"},"content":{"rendered":"<h4><a href=\"https:\/\/ac.nowcoder.com\/acm\/problem\/23482\">8-3 \u5c0fA\u7684\u6700\u77ed\u8def<\/a><\/h4>\n<p>\u5206\u6790\uff1a\u4e0d\u5750\u7f06\u8f66\u65f6\uff0c\u5c31\u662fLCA\u6c42\u6811\u4e0a\u4e24\u70b9\u8ddd\u79bb\u7684\u677f\u5b50\u9898\uff0c\u591a\u4e86\u4e00\u4e2a\u7f06\u8f66(U, V)\uff0c\u90a3\u4e48\u4e0d\u5750\u65f6\uff0c\u8ddd\u79bb\u8fd8\u662f\u539f\u6765\u7684\u8ddd\u79bb\uff1b\u5750\u65f6\uff0c\u8981\u4e48\u5148\u4ecex\u5230U\uff0c\u5728\u4eceV\u5230y\uff0c\u8981\u4e48\u5148\u4ecex\u5230V\uff0c\u518d\u4eceU\u5230y\uff0c\u8fd9\u51e0\u79cd\u60c5\u51b5\u53d6\u4e2a\u6700\u4f18\u5373\u53ef\uff01<br \/>\n\u6bd4\u677f\u5b50\u9898\u4e5f\u5c31\u591a\u4e86\u51e0\u884c\u3002\u3002<\/p>\n<pre><code class=\"language-c++\">#include &lt;cstdio&gt;\n#include &lt;algorithm&gt;\nusing namespace std;\nconst int maxn = 3e5+5;\nstruct edge{\n    int from, to, next;\n}e[maxn*2];\n\nint n, head[maxn], tot, U, V, q;\nint lg[maxn], fa[maxn], dp[maxn][25], depth[maxn];\ninline int Min(int a, int b){\n    return a&lt;b?a:b;\n}\ninline int LCA(int u, int v){\n    if (depth[u] &lt; depth[v])    swap(u, v);\n    while (depth[u] &gt; depth[v]) u = dp[u][lg[depth[u]-depth[v]]-1];\n    if (u==v)   return u;\n    for (int k=lg[depth[u]]-1; k&gt;=0; k--)   if (dp[u][k]!=dp[v][k])\n        u = dp[u][k], v = dp[v][k];\n    return dp[u][0];\n}\n\ninline int dist(int u, int v){\n    int tmp = LCA(u, v);\n    return depth[u]+depth[v]-2*depth[tmp];\n}\n\nvoid dfs(int u, int v){\n    fa[u] = v;\n    depth[u] = depth[v] + 1;\n    for (int i=head[u]; i; i=e[i].next) if (!depth[e[i].to]){\n        dfs(e[i].to, u);\n    }\n    return;\n}\ninline void init(){\n    for (int i=1; i&lt;=n; i++)\n        lg[i] = lg[i-1] + (1&lt;&lt;lg[i-1]==i);\n    dfs(1, 0);\n    for (int i=1; i&lt;=n; i++)    dp[i][0] = fa[i];\n    for (int k=1; k&lt;=lg[n]-1; k++)  for (int i=1; i&lt;=n; i++)\n        dp[i][k] = dp[dp[i][k-1]][k-1];\n    return;\n}\ninline void add(int from, int to){\n    tot++;\n    e[tot].from = from, e[tot].to = to, e[tot].next = head[from];\n    head[from] = tot;\n    return;\n}\ninline int read(){\n    int x=0;    char ch = getchar();\n    while (ch&lt;'0' || ch&gt;'9')    ch = getchar();\n    while (ch&gt;='0' &amp;&amp; ch&lt;='9'){\n        x = x*10 + ch-'0';\n        ch = getchar();\n    }\n    return x;\n}\n\nint main(){\n    int from, to;\n    n = read();\n    for (int i=1; i&lt;n; i++){\n        from = read(), to = read();\n        add(from, to);  add(to, from);\n    }\n    init();\n    U = read(), V = read(), q = read();\n    for (int i=1; i&lt;=q; i++){\n        from = read(), to = read();\n        printf (\"%d\\n\", Min(dist(from, to), Min(dist(from, V)+dist(U, to), dist(from, U)+dist(V, to))));\n    }\n    return 0;\n}<\/code><\/pre>\n<h4><a href=\"https:\/\/ac.nowcoder.com\/acm\/problem\/14526\">8-3 \u8d2d\u7269<\/a><\/h4>\n<p>\u4e8c\u7ef4dp\uff0c\u4ee4dp[i][j]\u8868\u793a\u524di\u5929\u5171\u4e70\u4e86j\u5757\u7cd6\u679c\u7684\u6700\u5c0f\u82b1\u8d39\uff0c\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e5f\u5f88\u7b80\u5355\uff1a<br \/>\n<code class=\"katex-inline\">dp[i][j] = min(dp[i][j], dp[i-1][j-k]+c[i][k])<\/code><br \/>\nk\u8868\u793a\u7b2ci\u5929\u4e70k\u5757\u7cd6\u679c\uff0c\u8fd9\u91cc\u5bf9\u539f\u6570\u7ec4c\u8fdb\u884c\u5904\u7406\uff0c\u5148\u5bf9\u7b2ci\u5929\u7684\u7cd6\u679c\u4ef7\u94b1\u6392\u5e8f\uff0c\u7136\u540e\u518d\u6c42\u524d\u7f00\u548c\uff0c\u518d\u52a0\u4e0ak<em>k\uff0c\u4f7f\u5f97c[i][k]\u8868\u793a\u7b2ci\u5929\u4e70k\u5757\u7cd6\u679c\u65f6\u7684\u4ef7\u94b1\u3002<br \/>\n\u7136\u540e\uff0c\u8fd9\u9898\u7684\u9650\u5236\u6761\u4ef6\u633a\u591a\u7684\uff0c\u9996\u5148\u6211\u4eec\u6bcf\u5929\u90fd\u53ea\u54031\u5757\u7cd6\u679c\uff0c\u6240\u4ee5\u5bf9\u4e8e\u4efb\u610fi,j\u90fd\u8981\u6ee1\u8db3j&gt;=i\uff0c\u540c\u65f6j\u8fd8\u8981\u6ee1\u8db3j&lt;=i<\/em>m\uff0c\u5373\u524di\u5929\u6700\u591a\u4e70i*m\u5757\u7cd6\uff0c\u4ee5\u53caj&lt;=n\uff0cn\u5929\u4e0b\u6765\u53ea\u9700\u8981\u4e70n\u5757\u7cd6\u5373\u53ef\u3002k\u6ee1\u8db3\u7684\u6761\u4ef6\u6709\uff1ak&lt;=m, j-k&gt;=i-1\uff0c\u5373\u7b2ci\u5929\u4e70\u4e86k\u5757\uff0c\u524di-1\u5929\u4e70\u4e86j-k\u5757\uff0c\u540c\u65f6j-k&gt;=i-1\uff0c\u8fd9\u4e2a\u6761\u4ef6\u5f88\u91cd\u8981\uff0c\u8fd9\u6837\u5c31\u80fd\u4fdd\u8bc1\u4e4b\u524d\u7684i-1\u5929\u90fd\u80fd\u6ee1\u8db3j&gt;=i\u8fd9\u4e2a\u6761\u4ef6\uff01<br \/>\n\u90a3\u4e48\u6700\u7ec8\u7684\u7b54\u6848\u5c31\u662fdp[n][n]\u4e86\u3002<\/p>\n<pre><code class=\"language-c++\">#include &lt;cstdio&gt;\n#include &lt;algorithm&gt;\nusing namespace std;\ntypedef long long ll;\nint n, m;\nint c[305][305];\nll dp[305][305];\nint main(){\n    scanf (\"%d %d\", &amp;n, &amp;m);\n    for (int i=1; i&lt;=n; i++){\n        for (int j=1; j&lt;=m; j++)    scanf (\"%d\", &amp;c[i][j]);\n        sort (c[i]+1, c[i]+m+1);\n        for (int j=1; j&lt;=m; j++)    c[i][j] += c[i][j-1];\n        for (int j=1; j&lt;=m; j++)    c[i][j] += j*j;\n    }\n    for (int j=1; j&lt;=m; j++)    dp[1][j] = c[1][j];\n    for (int i=2; i&lt;=n; i++){\n        for (int j=i; j&lt;=n &amp;&amp; j&lt;=i*m; j++){\n            for (int k=0; k&lt;=m &amp;&amp; j-k&gt;=i-1; k++){\n                if (dp[i][j]==0)    dp[i][j] = dp[i-1][j-k]+c[i][k];\n                else    dp[i][j] = min(dp[i][j], dp[i-1][j-k]+c[i][k]);\n            }\n        }\n    }\n    printf (\"%lld\", dp[n][n]);\n    return 0;\n}<\/code><\/pre>\n<h4><a href=\"https:\/\/ac.nowcoder.com\/acm\/problem\/14700\">8-6 \u8ffd\u503a\u4e4b\u65c5<\/a><\/h4>\n<p>\u6c42\u6700\u77ed\u8def\uff0c\u5728\u539f\u6709\u8fb9\u6743\u7684\u57fa\u7840\u4e0a\uff0c\u65b0\u52a0\u4e86\u4e00\u4e2a\u5bf9\u6b65\u6570\u7684\u6743\u503c\u548c\u9650\u5236\uff0c\u8981\u6c42\u603b\u6743\u503c\u6700\u5c0f\u3002<br \/>\nDij\u505a\u6cd5\u3002<\/p>\n<pre><code class=\"language-c++\">#include &lt;cstdio&gt;\n#include &lt;vector&gt;\n#include &lt;queue&gt;\nusing namespace std;\nconst int maxn = 1005, maxm = 100005;\n\nstruct edge{\n    int from, to, next;\n    int val;\n}e[maxm*2];\nint n, m, k, tot;\nint head[maxn], dist[maxn], days[maxn];\nint days_cost[15];\nstruct node{\n    int pos;\n    int val;\n    int days;\n    friend bool operator &lt; (const struct node &amp;a, const struct node &amp;b){\n        return a.val+days_cost[a.days] &gt; b.val+days_cost[b.days];\n    }\n}tmp;\npriority_queue&lt;struct node&gt; q;\ninline void add(int from, int to, int value){\n    tot++;\n    e[tot].from = from, e[tot].to = to, e[tot].val = value, e[tot].next = head[from];\n    head[from] = tot;\n}\nint main(){\n    int u, v, w;\n    scanf (\"%d %d %d\", &amp;n, &amp;m, &amp;k);\n    for (int i=1; i&lt;=m; i++){\n        scanf (\"%d %d %d\", &amp;u, &amp;v, &amp;w);\n        add(u, v, w);   add(v, u, w);\n    }\n    for (int i=1; i&lt;=k; i++){\n        scanf (\"%d\", days_cost+i);\n        days_cost[i] += days_cost[i-1];\n    }\n    for (int i=1; i&lt;=n; i++)    dist[i] = 999999999;\n    dist[1] = 0;    q.push(node{1, 0, 0});\n    while (!q.empty()){\n        tmp = q.top();  q.pop();\n        for (int i=head[tmp.pos]; i; i=e[i].next){\n            if (e[i].to==1) continue;\n            if (tmp.days&gt;=k)    continue;\n            if (days[e[i].to]==0){\n                days[e[i].to] = tmp.days + 1;\n                dist[e[i].to] = tmp.val + e[i].val;\n                q.push(node{e[i].to, dist[e[i].to], days[e[i].to]});\n                continue;\n            }\n            if (dist[tmp.pos]+e[i].val+days_cost[tmp.days+1] &lt; dist[e[i].to]+days_cost[days[e[i].to]]){\n                days[e[i].to] = tmp.days + 1;\n                dist[e[i].to] = tmp.val + e[i].val;\n                q.push(node{e[i].to, dist[e[i].to], days[e[i].to]});\n                continue;\n            }\n        }\n    }\n    if (dist[n]!=999999999) printf (\"%d\", dist[n]+days_cost[days[n]]);\n    else    puts(\"-1\");\n    return 0;\n}<\/code><\/pre>\n<p><a href=\"https:\/\/ac.nowcoder.com\/acm\/problem\/14250\">8-18 MMSet2<\/a><\/p>\n<p>\u9898\u610f\u770b\u9898\uff0c\uff0c\u5176\u5b9e\u7b54\u6848\u5c31\u662f\u70b9\u96c6s\u4e2d\uff0c\u8ddd\u79bb\u6700\u5927\u7684\u4e24\u4e2a\u70b9\u7684\u8ddd\u79bb\u7684\u4e00\u534a\uff0c\u5411\u4e0a\u53d6\u6574\uff0c\u5176\u4e2d\u4e00\u4e2a\u70b9\u4e00\u5b9a\u662f\u6df1\u5ea6\u6700\u5927\u7684\u90a3\u4e2a\u70b9\uff0c\u5269\u4e0b\u4e00\u4e2a\u70b9\u679a\u4e3e\u70b9\u96c6s\u7528LCA\u6c42\u6700\u5927\u8ddd\u79bb\u3002<\/p>\n<pre><code class=\"language-c++\">#include &lt;cstdio&gt;\n#include &lt;algorithm&gt;\nusing namespace std;\nconst int maxn = 3e5+5, inf = 3e5+5;\nstruct edge\n{\n    int from, to, next;\n}e[maxn*2];\nint n, tot, q, s;\nint head[maxn], depth[maxn], fa[maxn], lg[maxn], dp[maxn][25];\nint nums[1000005];\n\ninline int LCA(int u, int v){\n    if (depth[u] &lt; depth[v])    swap(u, v);\n    while (depth[u] &gt; depth[v]) u = dp[u][lg[depth[u]-depth[v]]-1];\n    if (u==v)   return u;\n    for (int k=lg[depth[u]]-1; k&gt;=0; k--)   if (dp[u][k]!=dp[v][k])\n        u = dp[u][k], v = dp[v][k];\n    return dp[u][0];\n}\n\nvoid dfs(int u, int v){\n    depth[u] = depth[v] + 1;\n    dp[u][0] = v;\n    for (int i=head[u]; i; i=e[i].next) if (!depth[e[i].to]){\n        dfs(e[i].to, u);\n    }\n    return;\n}\n\nvoid init(){\n    for (int i=1; i&lt;=n; i++)\n        lg[i] = lg[i-1] + (1&lt;&lt;lg[i-1]==i);\n    dfs(1, 0);\n    for (int k=1; k&lt;=lg[n]-1; k++)    for (int i=1; i&lt;=n; i++)\n        dp[i][k] = dp[dp[i][k-1]][k-1];\n}\ninline void add(int from, int to){\n    tot++;\n    e[tot].from = from, e[tot].to = to, e[tot].next = head[from];\n    head[from] = tot;\n    return;\n}\n\nint main(){\n    int from, to;\n    scanf (\"%d\", &amp;n);\n    for (int i=1; i&lt;n; i++){\n        scanf (\"%d %d\", &amp;from, &amp;to);\n        add(from, to);  add(to, from);\n    }\n    init();\n    scanf (\"%d\", &amp;q);\n    for (int i=1; i&lt;=q; i++){\n        scanf (\"%d\", &amp;s);\n        for (int j=1; j&lt;=s; j++)    scanf (\"%d\", nums+j);\n        if (s==1){\n            printf (\"0\\n\");\n            continue;\n        }\n        int maxx = 0, flag, ans = 0;\n        for (int j=1; j&lt;=s; j++)    if (depth[nums[j]]&gt;maxx){\n            maxx = depth[nums[j]];\n            flag = nums[j];\n        }\n        for (int j=1; j&lt;=s; j++)    if (nums[j]!=flag){\n            ans = max(ans, depth[nums[j]]+maxx-2*depth[LCA(flag, nums[j])]);\n        }\n        printf (\"%d\\n\", ans\/2+(ans%2));\n    }\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>8-3 \u5c0fA\u7684\u6700\u77ed\u8def \u5206\u6790\uff1a\u4e0d\u5750\u7f06\u8f66\u65f6\uff0c\u5c31\u662fLCA\u6c42\u6811\u4e0a\u4e24\u70b9\u8ddd\u79bb\u7684\u677f\u5b50\u9898\uff0c\u591a\u4e86\u4e00\u4e2a\u7f06\u8f66(U, V)\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[46],"tags":[],"_links":{"self":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/761"}],"collection":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/comments?post=761"}],"version-history":[{"count":11,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/761\/revisions"}],"predecessor-version":[{"id":864,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/761\/revisions\/864"}],"wp:attachment":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/media?parent=761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/categories?post=761"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/tags?post=761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}