多校题解

##[1001.Average](http://acm.hdu.edu.cn/showproblem.php?pid=5353)
Summary
$n$ people are sitting around a circle and every two adjacent people can exchange only on candy. The goal is to find a valid exchange order to make everyone have equal amount of candies (or determine it is impossible). Solution

    Find the sum of the candies. If the sum is not a multiple of $n$, such order does not exist.
    let $c_{i}$ be the difference between the candies $i$-th person have and the average candies.
    Enumerate 1-st person’s operation: give a candy to 2, receive a candy from 2, do nothing.
    Then determine next one’s operation according to the value $c_{i}$:

    $c_{i}$= −1: receive a candy from $i$ + 1.
    $c_{i}$ = 0: just do nothing.
    $c_{i}$ = 1: give a candy to $i$ + 1.
    otherwise, we can not find a valid exchange order.

Time complexity: $O(n)$.   
##[1002.Bipartite Graph](http://acm.hdu.edu.cn/showproblem.php?pid=5354) Summary
We are given an undirected graph $G$ with $n$ vertices and $m$ edges. You are to determine for each vertex $i$, after deleting it, whether the graph becomes a bipartite graph. Solution

    For every edge $(u, v)$, set a time interval $[s, t)$, which means the edge is only available from time $s$ to time $t$.
    Every deletion will make some edges become unavailable at some time points. More specifically, if we delete $i$-th vertex, every edge adjacent to vertex $i$ will unavailable at time $i$. Then the number of available time intervals for each edge will be at most 3.

    If we know whether the graph is a bipartite graph at a specific time $i$, we also know whether $i$-th vertex satisfy our will.

  Solve the problem:   You are given an undirected graph $G$ with $n$ vertices and $m$ edges. Each edge will available only in a specific time interval $[st, ed)$. For every time $t$, you are to determine whether the graph is a bipartite graph.

    Let function $Solve(l, r)$ solve all time from $l$ to $r$ and $E(l, r)$ is a set of edges which can exist at some time in interval $[l, r)$.

    Let mid = $\frac{l+r}{2}$, for each edge in $E$, assume the interval is $[x, y)$:

    $x = l$,$ y = r$, add the edge and check if it forms an odd circle with other edges added before.
    $y \leq mid$, add the edge to set $E(l,mid)$.
    $x \geq mid$, add the edge to set $E(mid, r)$.
    otherwise, add the edge to both sets $E(l,mid)$ and $E(mid, r)$.

    We can use disjoint-set to maintain odd circles. After each $Solve(l, r)$, remember to resume the disjoint-set.

  How to resume the disjoint-set:

    Just union by rank and there is no need to use path compression.
    Before every union operation, use a stack to store the state before.
    Resuming the disjoint-set is just to restore the state according to the stack.

Time complexity: $O(m \ log^{2}\  n)$   ##[1003.Cake](http://acm.hdu.edu.cn/showproblem.php?pid=5355) Summary You are given n integers 1, 2, . . . , $n$ and an integer $m$. You need to divide the $n$ integer into $m$ equal sum parts (or determine it is impossible). Solution

    If $m$ is not divisible by $\frac {n(n+1)}{2}$ or $n < 2m - 1$, it is impossible.
    If we have 2$m$ number 1, 2, . . . , 2$m$, we can divide it into $m$ equal sum parts easily.
    If $n$ is large, we can reduce $n$ to $n - 2m$. If $n (n \leq40)$ is small, we can use backtracking to find a possible solution or we can construct the solution by hand.

Time complexity: $O(n)$.   ##[1004.Deal](http://acm.hdu.edu.cn/showproblem.php?pid=5356) Summary Given are a weighted tree with $n$ vertices. There’s an item with weight $w_{i}$ in $i$-th vertex. The cost for transporting an item with weight $w$ from vertex $u$ to vertex $v$ is $w$ multiply the distance from vertex $u$ to vertex $v$. Item will be cached in the vertex it passed. You are going to make $m$ transportations and make the sum of cost maximum. Each item will be transported from a vertex containing it and closet to the destination.   Solution

    Each item is independent, so just consider item by item.
    For item $i$, the first transportation must be the longest, same as the second transportation and so on.
    Using greedy to find the path. Sort all the possible transportations and get the largest $m$.

Time complexity: $O(n^{2}\  log\ n)$.     ##[1005.Easy Sequence](http://acm.hdu.edu.cn/showproblem.php?pid=5357) Summary You are given a parentheses string. For $i$-th character, you need to find the number valid strings starting with it or ending with it. For $i$-th character, you need to find the number valid substrings containing it. Solution

    let $match_{i}$ be the position of matched parenthese for $i$-th character, $a_{i}$ be the number of valid substrings starting with $i$-th character, and $b_{i}$ be the number of valid substrings ending with $i$-th character.

$a_{i}=a_{match_{i+1}}+1$, $b_{i}=b_{match_{i-1}}+1$

    assume $s_{i}$ is ’(’, let $up_{i}$ be smallest matched parentheses contain $s_{i}$ and $s_{match_{i}}$

$ans_{i}=ans_{match_{i}}=ans_{up_{i}}+a_{i}*b_{match_{i}}$

    both $match_{i}$ and $up_{i}$ can be computed by using a stack.

Time complexity: $O(n)$.   ##[1006.First One](http://acm.hdu.edu.cn/showproblem.php?pid=5358) Summary For a given sequence $a_{1}$, $a_{2}$, . . . , $a_{n}$, we want to find the value of $\sum_{i=1}^{n} \sum_{j=i}^{n}(\left \lfloor log_{2} S(i ,j)\right \rfloor+1)*(i+j)$ Whrer S(i ,j) is the sum of $a_{i}$,$a_{i+1}$,…,$a_{j}$ Solution

    $\left \lfloor log_{2}x \right \rfloor +1$ is the number of bits in the binary representation of $x$.
    Consider the contribution of $k$-th bit: if $S(i, j)\geq 2^{k}$ ,$ i + j$ is added to the answer.
    Enumerate $k$-th bit, and use two pointers to maintain the contribution.

Time complexity: $O(n\ log\ n)$.     ##[1007.Group](http://acm.hdu.edu.cn/showproblem.php?pid=5359) Summary We are given an directed graph $G$ with $n$ vertices and $m$ edges. You are to determine for each edge $i$,after deleting it, whether the number of strongly connected components increases. Solution Let us first solve the vertex deleting one:

    Find all the strongly connected components, each strongly connected component can be considered independently.
    Let $G = (V,E)$ is a strongly connected graph, $G^{R} = (V,E^{R})$ is the reversal graph of $G$ (if $(u, v)$ in $G$ then $(v, u)$ in $G^{R}$), $G(s) = (V,E, s)$ be the flowgraph with start vertex $s$,$D(s)$ the set of non-trivial dominators in $G(s)$, $G^{R}(s) = (V,E^{R}, s)$ be the flowgraph with start vertex $s$, $D^{R}(s)$ the set of non-trivial dominators in $G^{R}(s)$.
    Then vertex $v\neq s$ is a strong articulation point in $G$ if and only if $v \in D(s)\cup D^{R}(s)$.
    proving it is not easy, you may google it if you have interest.

Now let us solve the edge deleting one: for each edge $x\rightarrow y$, add an extra vertex $z$ between the edge, $x \rightarrow z \rightarrow y$. We can use the mothod above to solve it. Time complexity: $O((n + m) \alpha (n + m))$   ##[1008.Hiking](http://acm.hdu.edu.cn/showproblem.php?pid=5360) Summary There are $n$ people and $i$-th people will go out if the number of people going out if no less than $l_{i} + 1$ and no larger than $r_{i} + 1$. You need to invite them one by one to make the number of people going out as large as possible. Solution

    sort all the people according to $l_{i}$ and $r_{i}$.
    maintain an integer $cur$ as the number of people agree to go out yet. For all the people will agree to go out, pick the one with smallest $r$.

Time complexity: $O(n\ log\ n)$.   ##[1009.In Touch](http://acm.hdu.edu.cn/showproblem.php?pid=5361) Summary $n$ vertices are in a straight line and $i$-th vertex is in position $i$. $i$-th vertex can teleport to other vertices whose distance between $i$-th vertex is no less than $l_{i}$ and no larger than $r_{i}$ with cost $c_{i}$. For each vertex,find the minimum cost from 1-st vertex to it. Solution

    let $d_{i}$ be the cost entering vertex $i$.
    use dijkstra to calculate $d_{i}$ and use disjoint-set to maintain the vertex not visited yet.
    the answer is $d_{i}-c_{i}$.

Time complexity: $O(n\ log\ n)$.   ##[1010.Just A String](http://acm.hdu.edu.cn/showproblem.php?pid=5362) Summary You are given a random string of length $n$ and an alphabet of size $m$. You are to tell the expected number of such substrings that can be rearranged to a palindrome in the random string. Solution

    The answer is obvious after enumerating the length of the substring:

$\sum_{l=1}^{n}(n-l+1)m^{n-l}T(l,m)$ where $T(l,m)$ is the number of such string with length $l$ and alphabet of size $m$.

    $l$ can be odd or even, let’s solve the even case first:

——$T(2n,m)=\sum_{i=0}^{n}(2i,m-1)* \begin{pmatrix}2n\\2i \end{pmatrix}$ ——$\frac {T(2n,m)}{(2n)!}=\sum_{i=0}^{n}\frac {T(2i,m-1)}{(2i)!}*\frac{1}{(2(n-i))!}$ ——Let $G_{m}(x)$ be the generating function for $\frac{T(2n,m)}{(2n)!}$ , F(x) be the generating function for $\frac {1}{(2n)!}$. Then $F(x)=cosh \sqrt {x}=\frac{e^{\sqrt{x}}+e^{-\sqrt{x}}}{2} $and $G_{m}(x)=F^{m}(x)=( \frac{e^{\sqrt{x}}+e^{-\sqrt{x}}}{2})^{m}$. ——$T(2n,m)$ is the coefficient of $n$-th item of $G_{m}(x)$, after doing some calculation, we have $T(2n,m)=\sum_{i=0}^{\frac{m-1}{2}}\frac{\begin{pmatrix}m\\i \end{pmatrix}*(m-2i)^{2n}}{2^{m-1}}$ For a fixed $n$, which can be computed in $O(m)$ time.

    Now it is time for the odd $l$:

$T(2n+1,m)=\sum_{i=0}^{n}T(2i,m-1)* \begin{pmatrix}2n+1\\2i \end{pmatrix}$ If we precompute the value for $T(2i,m - 1)$ then $T(2n + 1,m)$ can be computed in $O(n)$ time. Time complexity: $O(n(m + n))$.   ##[1011.Key Set](http://acm.hdu.edu.cn/showproblem.php?pid=5363) Summary For a given set {1, 2, . . . , $n$}, you are to find the number of nonempty subsets with even sum. Solution Let $a$ be the number of even integers and $b$ be the number of even integers. The answer is: $2^{a}(\begin{pmatrix}b\\0 \end{pmatrix}+\begin{pmatrix}b\\2 \end{pmatrix}+$…$)=2^{a+b-1}=2^{n-1}-1$ Time complexity: $O(log\ n)$

转载于:https://www.cnblogs.com/Patt/p/5836400.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
06-01
这道题是一道典型的费用限制最短路题目,可以使用 Dijkstra 算法或者 SPFA 算法来解决。 具体思路如下: 1. 首先,我们需要读入输入数据。输入数据中包含了道路的数量、起点和终点,以及每条道路的起点、终点、长度和限制费用。 2. 接着,我们需要使用邻接表或邻接矩阵来存储图的信息。对于每条道路,我们可以将其起点和终点作为一个有向边的起点和终点,长度作为边权,限制费用作为边权的上界。 3. 然后,我们可以使用 Dijkstra 算法或 SPFA 算法求解从起点到终点的最短路径。在这个过程中,我们需要记录到每个点的最小费用和最小长度,以及更新每条边的最小费用和最小长度。 4. 最后,我们输出从起点到终点的最短路径长度即可。 需要注意的是,在使用 Dijkstra 算法或 SPFA 算法时,需要对每个点的最小费用和最小长度进行松弛操作。具体来说,当我们从一个点 u 经过一条边 (u,v) 到达另一个点 v 时,如果新的费用和长度比原来的小,则需要更新到达 v 的最小费用和最小长度,并将 v 加入到优先队列(Dijkstra 算法)或队列(SPFA 算法)中。 此外,还需要注意处理边权为 0 或负数的情况,以及处理无法到达终点的情况。 代码实现可以参考以下样例代码: ```c++ #include <cstdio> #include <cstring> #include <queue> #include <vector> using namespace std; const int MAXN = 1005, MAXM = 20005, INF = 0x3f3f3f3f; int n, m, s, t, cnt; int head[MAXN], dis[MAXN], vis[MAXN]; struct Edge { int v, w, c, nxt; } e[MAXM]; void addEdge(int u, int v, int w, int c) { e[++cnt].v = v, e[cnt].w = w, e[cnt].c = c, e[cnt].nxt = head[u], head[u] = cnt; } void dijkstra() { priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q; memset(dis, 0x3f, sizeof(dis)); memset(vis, 0, sizeof(vis)); dis[s] = 0; q.push(make_pair(0, s)); while (!q.empty()) { int u = q.top().second; q.pop(); if (vis[u]) continue; vis[u] = 1; for (int i = head[u]; i != -1; i = e[i].nxt) { int v = e[i].v, w = e[i].w, c = e[i].c; if (dis[u] + w < dis[v] && c >= dis[u] + w) { dis[v] = dis[u] + w; q.push(make_pair(dis[v], v)); } } } } int main() { memset(head, -1, sizeof(head)); scanf("%d %d %d %d", &n, &m, &s, &t); for (int i = 1; i <= m; i++) { int u, v, w, c; scanf("%d %d %d %d", &u, &v, &w, &c); addEdge(u, v, w, c); addEdge(v, u, w, c); } dijkstra(); if (dis[t] == INF) printf("-1\n"); else printf("%d\n", dis[t]); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值