## GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

# [Solution] Optimal Graph Partition for Fast Routing (GP2) Codeforces Solution

GP2. Optimal Graph Partition for Fast Routing (GP2)
time limit per test
5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

In computer networks, the Internet Protocol (IP) uses path routing. With path routing, the packet contains only the destination address; routers decide which "next hop" to forward each packet on to get it closer to its destination. Each router makes this decision locally, based only on the destination address and its local configuration. One way to store this configuration is to use routing tables, which means for each router, we store the "next hop" for every possible destination. However, as the scale of the network grows larger, the size of the router table grows as well. It is a big issue since routers only have limited computing resources. One way to address this issue is to divide the graph into several partitions.

For example, in the above figure, there are $12$ routers in the network. Each edge has a certain weight. Each router has to store $12$ table entries for all possible destinations in the network (including itself). We can merge four nodes in the graph into one large abstract node $R2$. When routers are merged, all existing edges connected to the elements of a large node are preserved. Then all routers outside $R2$ only need to store $9$ table entries. Therefore, in this case, we can assign the same "next hop" for all destination routers in $R2$. The intuition behind this is that router $R1$ does not need to care how to get $R24$ from $R21$ if $R1$ wants to send a packet to $R24$$R1$ just needs to find a path to get its packet to an arbitrary router inside $R2$ and then let the routers inside $R2$ decide how to get $R24$. For routers inside $R2$, however, there are still $12$ table entries, because they need to know the routes to each other.

We can further merge $R31,R32,R33,R34$ into a single node $R3$. Then for $R1$, we need to store only $6$ entries in its routing table. For routers in $R3$ or $R2$, it only needs to store $9$ entries. Your task is to find an optimal partition of the initial graph to minimize the maximum size of the routing table for all routers in the network.

A computer network under consideration is an undirected connected graph $G=\left(V,E\right)$, where $V=\left\{1,2,\dots ,N\right\}$

A partition $P$ of the network $G$ is a set of non-empty subsets of $V$ such that every node $v\in V$ belongs to exactly one of these subsets, and the induced sub-graph for each subset in $P$ is connected. Here, one subset corresponds to one abstract node mentioned above.

Formally, $P$ is a valid partition if and only if $P$ is a family of sets that satisfies:

• $\mathrm{\varnothing }\notin P$
• $\bigcup _{A\in P}A=V$
• $\mathrm{\forall }A,B\in P:A\ne B\phantom{\rule{thickmathspace}{0ex}}⟹\phantom{\rule{thickmathspace}{0ex}}A\cap B=\mathrm{\varnothing }$
• $\mathrm{\forall }A\in P$, the induced subgraph $G\left[A\right]$ is connected

The routing table size of any node $v$ for the corresponding partition $P$ is defined by the following equation:

The "next hop" is defined by the shortest path tree. For each node pair $\left(u,v\right),u\in V,v\in V$, the "next hop" is a node $z\in V$, such that there is an edge $e\in E$ connected from $u$ to $z$ directly, and $dist\left(u,v\right)=w\left(e\right)+dist\left(z,v\right)$, here $dist\left(u,v\right)$ denotes the length of the shortest path from $u$ to $v$, and $w\left(e\right)$ denotes the weight of the edge connecting $u$ and $z$. The valid "next hop" may not be unique. After the graph partitioning, the "next hop" for nodes in the same subset $A\in P$ is calculated via the shortest path in the induced sub-graph $G\left[A\right]$ using the same method above. We compute the "next hop" for two nodes from different subsets of $P$ by calculating the shortest path in the "compressed" graph (we compress all nodes in the same subset into a single node). The new graph only contains edges between nodes from different subsets. And then find the shortest path from the subset containing $u$ to the subset containing $v$. Suppose the first edge in this path is between $\left(x,y\right)$. From the definition, $x$$y$ are in different subsets of $P$, and $u$ is in the same subset of $x$. If $x=u$, then the "next hop" of $\left(u,v\right)$ is $y$, otherwise the "next hop" of $\left(u,v\right)$ is the "next hop" of $\left(u,x\right)$.

The path from $u$ to $v$ may differ from the path in the graph before partition.

For example:

Originally, the packet transmission path from $1$ to $2$ is the shortest in the whole graph, so it is $1\to 3\to 2$, and the total length is 3.

If our partition $P=\left\{\left\{1,2\right\},\left\{3\right\},\left\{4\right\}\right\}$, then the packet transmission path from $1$ to $2$ is the shortest path in the subgraph, so it is $1\to 2$. The total length is $10$, which gets much greater after partition.

Define $dist\left(u,v\right)$ as the length of the shortest path from $u$ to $v$ in graph $G$.

Let $cost\left(u,v,P\right)$ be the length of the optimal transmission path after dividing graph $G$ by partition $P$. The way to determine $cost\left(u,v,P\right)$ is specified later.

For node $u$, we define $B\left(u,P\right)=Q$, where $Q$ is the unique subset in $P$ such that $u\in Q$.

Define two new edge sets:

$GraphZero\left(P\right)=\left(V,EdgeZero\left(P\right)\right)$$GraphInf\left(P\right)=\left(V,EdgeInf\left(P\right)\right)$.

In other words, $GraphZero\left(P\right)=\left(V,EdgeZero\left(P\right)\right)$ is almost a copy of the original graph, except for all edges that have both endpoints in the same subset of $P$ (their weights are set to $0$). $GraphInf\left(P\right)=\left(V,EdgeInf\left(P\right)\right)$ is also almost a copy of the original graph, except for all edges that connect different subsets of $P$ (their weights are set to infinity).

Let $disZero\left(u,v,P\right)$ be the length of the shortest path from $u$ to $v$ in $GraphZero\left(P\right)$.

Let $disInf\left(u,v,P\right)$ be the length of the shortest path from $u$ to $v$ in $GraphInf\left(P\right)$.

For nodes , we define that $middle\left(u,v,P\right)$ be the edge $e=\left(x,y,w\right)\in EdgeZero\left(P\right)$ with the samllest edge index which makes the following equation holds:

We can prove such an edge always exists.

Then $cost\left(u,v,P\right)$ can be calculated recursively:

• For nodes $cost\left(u,v,P\right)=disInf\left(u,v,P\right)$.
• For nodes , let $e=\left(x,y,w\right)=middle\left(u,v,P\right)$, then $cost\left(u,v,P\right)=cost\left(u,x,P\right)+w+cost\left(y,v,P\right)$.

The score for SubTask2 for the ${i}^{th}$ graph is calculated by the formula:

The final score for SubTask2 is calculated according to the following formula:

Input

The first line contains two integers $N$ and $M$ $\left(2\le N\le {10}^{5};N-1\le M\le 2×{10}^{5}\right)$ — the number of nodes and edges, respectively.

Each of the next $M$ lines contains three integers ${u}_{i},{v}_{i}$ and ${w}_{i}$ (), denoting an edge between the node ${u}_{i}$ and ${v}_{i}$ with weight ${w}_{i}$${e}_{i}=\left({u}_{i},{v}_{i},{w}_{i}\right)$ is the edge with index $i$ in the edge set $E$. It is guaranteed that the input graph is connected and there are no multiple edges. That is, any pair (in any order) of nodes appear in this list no more than once.

The next line contains two numbers: one integer $SetSize$ ($1\le SetSize\le min\left(50,N\right)$), the size of node set $S$ and one real number $k$, given with at most six digits after the decimal point $\left(0\le k\le {10}^{6}\right)$.

Each of the next $SetSize$ lines contains one integer $NodeID$ ($0\le NodeID\le N-1$), the node in $S$.

Output

In the first line, output the number of subsets in your partition $P$.

The $\left(i+1\right)$-th line should begin with a single number $cn{t}_{i}$ — the size of the $i$-th subset, followed by $cn{t}_{i}$ numbers, denoting the node IDs in the $i$-th subset.

Scoring

During the coding phase, your solutions are tested against pre-tests only. The first $6$ of them are open and available for download at the link problem-gp2-open-testset.zip in the "contest materials" section in a sidebar.

After the end of the coding phase, the last submission that scored a positive number of points on the pre-tests will be selected. It will be retested in the final tests. The rest of your submissions will be ignored. The final tests are secret and not available to the participants, they are mostly realistic. The result obtained on the final tests is your final official result for this problem.

1. According to the formula, the solution with a larger score wins.
2. If two solutions have the same score, the solution submitted first wins.